diff --git a/src/app/components/Editor.tsx b/src/app/components/Editor.tsx index f14faa8..d50f0df 100644 --- a/src/app/components/Editor.tsx +++ b/src/app/components/Editor.tsx @@ -3,7 +3,7 @@ import PubliccodeYmlLanguages from "./PubliccodeYmlLanguages"; import { Col, Container, notify, Row } from "design-react-kit"; import { set } from "lodash"; -import { useCallback, useState } from "react"; +import { useCallback, useEffect, useState } from "react"; import { useTranslation } from "react-i18next"; import YAML from "yaml"; import licenses from "../../generated/licenses.json"; @@ -104,6 +104,7 @@ export default function Editor() { const configCountrySections = countrySection.parse(DEFAULT_COUNTRY_SECTIONS); const [currentPublicodeYmlVersion, setCurrentPubliccodeYmlVersion] = useState(''); const [isYamlModalVisible, setYamlModalVisibility] = useState(false); + const [isPublicCodeImported, setPublicCodeImported] = useState(false); const getNestedValue = (obj: PublicCodeWithDeprecatedFields, path: string) => { return path.split('.').reduce((acc, key) => (acc as never)?.[key], obj); @@ -140,6 +141,8 @@ export default function Editor() { const methods = useForm({ defaultValues, resolver, + mode: 'onTouched', + reValidateMode: 'onChange' }); const { getValues, handleSubmit, watch, setValue, reset } = methods; @@ -167,6 +170,33 @@ export default function Editor() { storage: window?.localStorage, // default window.sessionStorage exclude: [], }); + + const resetMaintenance = useCallback((value: Partial) => { + const maintenanceType = (value as PublicCode).maintenance.type; + + if (maintenanceType === "none") { + setValue('maintenance.contacts', []) + setValue('maintenance.contractors', []) + } + + if (maintenanceType === "community" || maintenanceType === "internal") { + setValue('maintenance.contractors', []) + } + + if (maintenanceType === "contract") { + setValue('maintenance.contacts', []) + } + }, [setValue]) + + useEffect(() => { + const subscription = watch((value, { name }) => { + if (name === 'maintenance.type') { + resetMaintenance(value as PublicCode); + } + } + ) + return () => subscription.unsubscribe() + }, [watch, resetMaintenance]) //#endregion //#region form action handlers @@ -191,6 +221,7 @@ export default function Editor() { dispatch(resetPubliccodeYmlLanguages()); reset({ ...defaultValues }); checkPubliccodeYmlVersion(getValues() as PublicCode); + setPublicCodeImported(false); }; const setFormDataAfterImport = async ( @@ -210,6 +241,8 @@ export default function Editor() { checkPubliccodeYmlVersion(publicCode); + setPublicCodeImported(true); + const res = await checkWarnings(values) if (res.warnings.size) { @@ -252,7 +285,7 @@ export default function Editor() {
- {currentPublicodeYmlVersion && + {isPublicCodeImported && currentPublicodeYmlVersion && @@ -455,7 +488,7 @@ export default function Editor() { loadFileYaml={(file) => loadFileYamlHandler(file)} trigger={() => submitHandler()} languages={languages} - yamlLoaded + yamlLoaded={isPublicCodeImported} /> {t(`publiccodeyml.${fieldName}.label`)} - {field.value?.length === 0 ? null : ( - - - - - - - - - - - - - - {fields.map(({ id }, index) => ( - - - {subfields.map((subfield) => { - const { ref, ...reg } = register( - `${fieldName}.${index}.${subfield}` - ); - - return ( - - ); - })} - + {field.value?.length === 0 + ? (

Nessun contatto presente

) + : ( +
{t(`publiccodeyml.${fieldName}.affiliation.description`)}
# - {t(`publiccodeyml.${fieldName}.name.label`)} * - - {t(`publiccodeyml.${fieldName}.email.label`)} - - {t(`publiccodeyml.${fieldName}.phone.label`)} - - {t(`publiccodeyml.${fieldName}.affiliation.label`)} -
{index + 1} - - - -
+ + + + + + + + + - ))} - -
{t(`publiccodeyml.${fieldName}.affiliation.description`)}
# + {t(`publiccodeyml.${fieldName}.name.label`)} * + + {t(`publiccodeyml.${fieldName}.email.label`)} + + {t(`publiccodeyml.${fieldName}.phone.label`)} + + {t(`publiccodeyml.${fieldName}.affiliation.label`)} +
- )} + + + {fields.map(({ id }, index) => ( + + {index + 1} + {subfields.map((subfield) => { + const { ref, ...reg } = register( + `${fieldName}.${index}.${subfield}` + ); + + return ( + + + + ); + })} + + + + + ))} + + + )} + + + ))} + + + )}