From 2777506991dd37e892600dd64aa128b3a60eefe0 Mon Sep 17 00:00:00 2001 From: Nicola Squartini Date: Mon, 22 Apr 2024 19:38:19 +0200 Subject: [PATCH] feat: add component for maintenance contacts --- src/app/components/Editor.tsx | 4 +- src/app/components/EditorContacts.tsx | 106 ++++++++++++++++++++++++++ 2 files changed, 109 insertions(+), 1 deletion(-) create mode 100644 src/app/components/EditorContacts.tsx diff --git a/src/app/components/Editor.tsx b/src/app/components/Editor.tsx index f825fec..ba7d354 100644 --- a/src/app/components/Editor.tsx +++ b/src/app/components/Editor.tsx @@ -27,6 +27,7 @@ import EditorFeatures from "./EditorFeatures"; import EditorDate from "./EditorDate"; import YAML from "yaml"; import { Col, Container, Row } from "design-react-kit"; +import EditorContacts from "./EditorContacts"; const resolver: Resolver = async (values) => { const res = await validator(JSON.stringify(values), "main"); @@ -201,9 +202,10 @@ export default function Editor() { data={maintenanceTypes} required /> + -
+

Italia

diff --git a/src/app/components/EditorContacts.tsx b/src/app/components/EditorContacts.tsx new file mode 100644 index 0000000..5e58131 --- /dev/null +++ b/src/app/components/EditorContacts.tsx @@ -0,0 +1,106 @@ +import { Button, Icon, Input, Table } from "design-react-kit"; +import { get } from "lodash"; +import { useController, useFieldArray, useFormContext } from "react-hook-form"; +import { useTranslation } from "react-i18next"; + +import PublicCode from "../contents/publiccode"; + +const fieldName = "maintenance.contacts"; +const subfields = ["name", "email", "phone", "affiliation"] as const; + +export default function EditorContacts(): JSX.Element { + const { control, register } = useFormContext(); + const { append, fields, remove } = useFieldArray< + PublicCode, + typeof fieldName + >({ + control, + name: fieldName, + }); + const { + field, + formState: { errors }, + } = useController({ + control, + name: fieldName, + }); + const { t } = useTranslation(); + + return ( +
+ {t(`publiccodeyml.${fieldName}.label`)} + {field.value?.length === 0 ? null : ( + + + + + + + + + + + + + {fields.map(({ id }, index) => ( + + + {subfields.map((subfield) => { + const { ref, ...reg } = register( + `${fieldName}.${index}.${subfield}` + ); + + return ( + + ); + })} + + + ))} + +
# + {t(`publiccodeyml.${fieldName}.name.label`)} + + {t(`publiccodeyml.${fieldName}.email.label`)} + + {t(`publiccodeyml.${fieldName}.phone.label`)} + +
{t(`publiccodeyml.${fieldName}.affiliation.label`)}
+ + {t(`publiccodeyml.${fieldName}.affiliation.description`)} + +
{index + 1} + + + +
+ )} + +
+ ); +}