BoardForm.jsx 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. import React from "react";
  2. import { useTranslation } from "react-i18next";
  3. import { nanoid } from "nanoid";
  4. import { Field } from "react-final-form";
  5. import { useBoardConfig } from "react-sync-board";
  6. import Hint from "../../ui/formUtils/Hint";
  7. import Label from "../../ui/formUtils/Label";
  8. import SliderRange from "../../ui/SliderRange";
  9. import { ImageField } from "../../mediaLibrary";
  10. import { backgrounds } from "../../gameComponents";
  11. const BoardConfigForm = () => {
  12. const { t } = useTranslation();
  13. const [boardConfig, setBoardConfig] = useBoardConfig();
  14. const [defaultPlayerCount] = React.useState([]);
  15. const addTranslation = React.useCallback(() => {
  16. setBoardConfig((prev) => ({
  17. ...prev,
  18. translations: [...(prev.translations || []), { id: nanoid() }],
  19. }));
  20. }, [setBoardConfig]);
  21. const removeTranslation = (idToRemove) => {
  22. setBoardConfig((prev) => ({
  23. ...prev,
  24. translations: prev.translations.filter(({ id }) => id !== idToRemove),
  25. }));
  26. };
  27. const CurrentBgForm = backgrounds.find(
  28. ({ type }) => type === (boardConfig.bgType || "default")
  29. )?.form;
  30. return (
  31. <>
  32. <Label>
  33. {t("Publish")}
  34. <Field
  35. name="published"
  36. component="input"
  37. type="checkbox"
  38. initialValue={boardConfig.published}
  39. />
  40. <Hint>{t("Check it to make your board publicly visible")}</Hint>
  41. </Label>
  42. <fieldset style={{ marginBottom: "2em", paddingBottom: "1em" }}>
  43. <legend>{t("Background")}</legend>
  44. <Label>{t("Type")}</Label>
  45. <Field
  46. name="bgType"
  47. component="select"
  48. initialValue={boardConfig.bgType || "default"}
  49. >
  50. {backgrounds.map((bg) => {
  51. return (
  52. <option key={bg.type} value={bg.type}>
  53. {bg.name}
  54. </option>
  55. );
  56. })}
  57. </Field>
  58. <div style={{ paddingTop: "1em" }}>
  59. {CurrentBgForm && (
  60. <Field name="bgConf" initialValue={boardConfig.bgConf}>
  61. {({ input: { onChange, value } }) => {
  62. return <CurrentBgForm value={value} onChange={onChange} />;
  63. }}
  64. </Field>
  65. )}
  66. </div>
  67. </fieldset>
  68. <Label>
  69. {t("Number of players")}
  70. <Field
  71. name="playerCount"
  72. initialValue={boardConfig.playerCount || defaultPlayerCount}
  73. >
  74. {({ input: { onChange, value } }) => {
  75. return (
  76. <SliderRange
  77. defaultValue={[2, 4]}
  78. value={value}
  79. min={1}
  80. max={9}
  81. step={1}
  82. onChange={onChange}
  83. />
  84. );
  85. }}
  86. </Field>
  87. </Label>
  88. <Label>
  89. {t("Duration (mins)")}
  90. <Field
  91. name="duration"
  92. initialValue={
  93. Array.isArray(boardConfig.duration)
  94. ? boardConfig.duration
  95. : defaultPlayerCount
  96. }
  97. >
  98. {({ input: { onChange, value } }) => {
  99. return (
  100. <SliderRange
  101. defaultValue={[15, 90]}
  102. value={value}
  103. min={15}
  104. max={90}
  105. step={15}
  106. onChange={onChange}
  107. />
  108. );
  109. }}
  110. </Field>
  111. </Label>
  112. <Label>
  113. {t("Minimal age (years)")}
  114. <Field
  115. name="minAge"
  116. component="input"
  117. initialValue={boardConfig.minAge}
  118. style={{ width: "5em", textAlign: "right" }}
  119. />
  120. </Label>
  121. <Label>
  122. {t("Magnetic Grid size")}
  123. <Field
  124. name="gridSize"
  125. component="input"
  126. initialValue={boardConfig.gridSize || 1}
  127. style={{ width: "5em", textAlign: "right" }}
  128. />
  129. </Label>
  130. <Label>
  131. {t("Main image")}
  132. <Field name="imageUrl" initialValue={boardConfig.imageUrl}>
  133. {({ input: { value, onChange } }) => {
  134. return <ImageField value={value} onChange={onChange} />;
  135. }}
  136. </Field>
  137. </Label>
  138. <Label>
  139. {t("Material language")}
  140. <Field
  141. name="materialLanguage"
  142. component="select"
  143. initialValue={boardConfig.materialLanguage}
  144. style={{ width: "10em" }}
  145. >
  146. <option />
  147. <option value="Multi-lang">🌍 {t("Multi-lang")}</option>
  148. <option value="en">🇬🇧 {t("English")}</option>
  149. <option value="fr">🇫🇷 {t("French")}</option>
  150. </Field>
  151. </Label>
  152. <fieldset style={{ marginBottom: "2em", paddingBottom: "1em" }}>
  153. <legend>{t("Informations")}</legend>
  154. <Label>
  155. {t("Default language")}
  156. <Field
  157. name="defaultLanguage"
  158. component="select"
  159. initialValue={boardConfig.defaultLanguage}
  160. style={{ width: "15em" }}
  161. >
  162. <option />
  163. <option value="en">🇬🇧 {t("English")}</option>
  164. <option value="fr">🇫🇷 {t("French")}</option>
  165. </Field>
  166. </Label>
  167. <Label>
  168. {t("Game name")}
  169. <Field
  170. name="defaultName"
  171. component="input"
  172. initialValue={boardConfig.defaultName || boardConfig.name}
  173. style={{ width: "15em" }}
  174. />
  175. </Label>
  176. <Label>
  177. {t("Keep title")}
  178. <Field
  179. name="keepTitle"
  180. component="input"
  181. type="checkbox"
  182. initialValue={boardConfig.keepTitle}
  183. />
  184. <Hint>
  185. {t("Check it to keep the game title above the game image.")}
  186. </Hint>
  187. </Label>
  188. <Label>
  189. {t("Baseline")}
  190. <Field
  191. name="defaultBaseline"
  192. component="input"
  193. initialValue={boardConfig.defaultBaseline}
  194. style={{ width: "100%" }}
  195. />
  196. </Label>
  197. <Label>
  198. {t("Description")}
  199. <Field
  200. name="defaultDescription"
  201. component="textarea"
  202. initialValue={boardConfig.defaultDescription || boardConfig.info}
  203. style={{ minHeight: "10em" }}
  204. />
  205. </Label>
  206. </fieldset>
  207. <div>
  208. {(boardConfig.translations || []).map(({ id }, index) => {
  209. return (
  210. <fieldset
  211. style={{ marginBottom: "2em", paddingBottom: "1em" }}
  212. key={id}
  213. >
  214. <legend>
  215. {t("Translation")} {index}
  216. </legend>
  217. <button
  218. className="button clear icon-only trash"
  219. onClick={() => removeTranslation(id)}
  220. >
  221. <img
  222. src="https://icongr.am/feather/trash.svg?size=25&color=ffffff"
  223. alt={t("Remove")}
  224. />
  225. </button>
  226. <Label>
  227. {t("Language")}
  228. <Field
  229. name={`translations[${index}].language`}
  230. component="select"
  231. initialValue={boardConfig.translations[index].language}
  232. style={{ width: "15em" }}
  233. >
  234. <option />
  235. <option value="en">🇬🇧 {t("English")}</option>
  236. <option value="fr">🇫🇷 {t("French")}</option>
  237. </Field>
  238. </Label>
  239. <Label>
  240. {t("Game name")}
  241. <Field
  242. name={`translations[${index}].name`}
  243. component="input"
  244. initialValue={boardConfig.translations[index].name}
  245. style={{ width: "15em" }}
  246. />
  247. </Label>
  248. <Label>
  249. {t("Baseline")}
  250. <Field
  251. name={`translations[${index}].baseline`}
  252. component="input"
  253. initialValue={boardConfig.translations[index].baseline}
  254. style={{ width: "100%" }}
  255. />
  256. </Label>
  257. <Label>
  258. {t("Description")}
  259. <Field
  260. name={`translations[${index}].description`}
  261. component="textarea"
  262. initialValue={boardConfig.translations[index].description}
  263. style={{ minHeight: "10em" }}
  264. />
  265. </Label>
  266. </fieldset>
  267. );
  268. })}
  269. <button onClick={addTranslation}>{t("Add translation")}</button>
  270. </div>
  271. </>
  272. );
  273. };
  274. export default BoardConfigForm;