From d29bf6a8cffdff2f0a1c7c7b6201e5283049a976 Mon Sep 17 00:00:00 2001 From: Ana Garcia Date: Fri, 3 Jan 2025 09:28:32 +0100 Subject: [PATCH] Move save disease outbreak logic to new hook --- .../useDiseaseOutbreakEventForm.ts | 192 ++++++++++++++++++ src/webapp/pages/form-page/useForm.ts | 154 ++------------ 2 files changed, 210 insertions(+), 136 deletions(-) create mode 100644 src/webapp/pages/form-page/disease-outbreak-event/useDiseaseOutbreakEventForm.ts diff --git a/src/webapp/pages/form-page/disease-outbreak-event/useDiseaseOutbreakEventForm.ts b/src/webapp/pages/form-page/disease-outbreak-event/useDiseaseOutbreakEventForm.ts new file mode 100644 index 00000000..c130a560 --- /dev/null +++ b/src/webapp/pages/form-page/disease-outbreak-event/useDiseaseOutbreakEventForm.ts @@ -0,0 +1,192 @@ +import { useCallback } from "react"; + +import i18n from "../../../../utils/i18n"; +import { useAppContext } from "../../../contexts/app-context"; +import { Id } from "../../../../domain/entities/Ref"; +import { RouteName, useRoutes } from "../../../hooks/useRoutes"; +import { ConfigurableForm } from "../../../../domain/entities/ConfigurableForm"; +import { ModalData } from "../../../components/form/Form"; +import { + CasesDataSource, + DiseaseOutbreakEvent, +} from "../../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent"; +import { GlobalMessage } from "../useForm"; + +type State = { + onSaveDiseaseOutbreakEvent: (formDataWithEntityData: ConfigurableForm) => void; +}; + +export function useDiseaseOutbreakEventForm(params: { + editMode: boolean; + setIsLoading: (isLoading: boolean) => void; + setGlobalMessage: (message: GlobalMessage) => void; + setOpenModal: (openModal: boolean) => void; + setModalData: (modalData: ModalData) => void; +}): State { + const { editMode, setIsLoading, setGlobalMessage, setOpenModal, setModalData } = params; + + const { compositionRoot, configurations } = useAppContext(); + const { goTo } = useRoutes(); + + const onMapDiseaseOutbreakEventToAlerts = useCallback( + (diseaseOutbreakEventId: Id, entity: DiseaseOutbreakEvent) => { + const { eventTrackerConfigurations } = configurations.selectableOptions; + + compositionRoot.diseaseOutbreakEvent.mapDiseaseOutbreakEventToAlerts + .execute( + diseaseOutbreakEventId, + entity, + eventTrackerConfigurations.hazardTypes, + eventTrackerConfigurations.suspectedDiseases + ) + .run( + () => {}, + err => { + console.error({ err }); + } + ); + goTo(RouteName.EVENT_TRACKER, { + id: diseaseOutbreakEventId, + }); + setGlobalMessage({ + text: i18n.t(`Disease Outbreak saved successfully`), + type: "success", + }); + }, + [ + compositionRoot.diseaseOutbreakEvent.mapDiseaseOutbreakEventToAlerts, + configurations.selectableOptions, + goTo, + setGlobalMessage, + ] + ); + + const onSaveDiseaseOutbreakEventWithCaseData = useCallback( + (formDataWithEntityData: ConfigurableForm) => { + if ( + formDataWithEntityData.type === "disease-outbreak-event" || + formDataWithEntityData.type === "disease-outbreak-event-case-data" + ) { + setIsLoading(true); + compositionRoot.save.execute(formDataWithEntityData, configurations, editMode).run( + diseaseOutbreakEventId => { + setIsLoading(false); + + if ( + diseaseOutbreakEventId && + formDataWithEntityData.entity && + formDataWithEntityData.type === "disease-outbreak-event" + ) { + onMapDiseaseOutbreakEventToAlerts( + diseaseOutbreakEventId, + formDataWithEntityData.entity + ); + } else if ( + diseaseOutbreakEventId && + formDataWithEntityData.type === "disease-outbreak-event-case-data" + ) { + goTo(RouteName.EVENT_TRACKER, { + id: diseaseOutbreakEventId, + }); + setGlobalMessage({ + text: i18n.t(`Disease outbreak case data saved successfully`), + type: "success", + }); + } + }, + err => { + setGlobalMessage({ + text: i18n.t( + formDataWithEntityData.type === "disease-outbreak-event-case-data" + ? `Error saving disease outbreak case data: ${err.message}` + : `Error saving disease outbreak: ${err.message}` + ), + type: "error", + }); + } + ); + } + }, + [ + compositionRoot.save, + configurations, + editMode, + goTo, + onMapDiseaseOutbreakEventToAlerts, + setGlobalMessage, + setIsLoading, + ] + ); + + const onSaveDiseaseOutbreakEvent = useCallback( + (formDataWithEntityData: ConfigurableForm) => { + if ( + !formDataWithEntityData.entity || + (formDataWithEntityData.type !== "disease-outbreak-event-case-data" && + formDataWithEntityData.type !== "disease-outbreak-event") + ) + return; + + const haveChangedCasesDataInDiseaseOutbreak = + editMode && + formDataWithEntityData.type === "disease-outbreak-event" && + !formDataWithEntityData.uploadedCasesDataFileId && + !!formDataWithEntityData.uploadedCasesDataFile && + formDataWithEntityData.entity?.casesDataSource === + CasesDataSource.RTSL_ZEB_OS_CASE_DATA_SOURCE_USER_DEF; + + if ( + haveChangedCasesDataInDiseaseOutbreak || + formDataWithEntityData.type === "disease-outbreak-event-case-data" + ) { + setOpenModal(true); + setModalData({ + title: i18n.t("Warning"), + content: i18n.t( + "You have uploaded a new data cases file. This action will replace the current data of this disease outbreak event with the data of the file. Are you sure you want to continue?" + ), + cancelLabel: i18n.t("Cancel"), + confirmLabel: i18n.t("Save"), + onConfirm: () => { + onSaveDiseaseOutbreakEventWithCaseData(formDataWithEntityData); + }, + }); + } else { + setIsLoading(true); + compositionRoot.save.execute(formDataWithEntityData, configurations, editMode).run( + diseaseOutbreakEventId => { + setIsLoading(false); + + if (diseaseOutbreakEventId && formDataWithEntityData.entity) { + onMapDiseaseOutbreakEventToAlerts( + diseaseOutbreakEventId, + formDataWithEntityData.entity + ); + } + }, + err => { + setGlobalMessage({ + text: i18n.t(`Error saving disease outbreak: ${err.message}`), + type: "error", + }); + } + ); + } + }, + [ + compositionRoot.save, + configurations, + editMode, + onMapDiseaseOutbreakEventToAlerts, + onSaveDiseaseOutbreakEventWithCaseData, + setGlobalMessage, + setIsLoading, + setModalData, + setOpenModal, + ] + ); + + return { + onSaveDiseaseOutbreakEvent, + }; +} diff --git a/src/webapp/pages/form-page/useForm.ts b/src/webapp/pages/form-page/useForm.ts index 061939ed..d1fab7b1 100644 --- a/src/webapp/pages/form-page/useForm.ts +++ b/src/webapp/pages/form-page/useForm.ts @@ -28,7 +28,7 @@ import { usePerformanceOverview } from "../dashboard/usePerformanceOverview"; import { useIncidentActionPlan } from "../incident-action-plan/useIncidentActionPlan"; import { RiskAssessmentQuestionnaire } from "../../../domain/entities/risk-assessment/RiskAssessmentQuestionnaire"; import { ModalData } from "../../components/form/Form"; -import { CasesDataSource } from "../../../domain/entities/disease-outbreak-event/DiseaseOutbreakEvent"; +import { useDiseaseOutbreakEventForm } from "./disease-outbreak-event/useDiseaseOutbreakEventForm"; export type GlobalMessage = { text: string; @@ -88,6 +88,14 @@ export function useForm(formType: FormType, id?: Id): State { const [openModal, setOpenModal] = useState(false); const [modalData, setModalData] = useState(); + const { onSaveDiseaseOutbreakEvent } = useDiseaseOutbreakEventForm({ + editMode: !!id, + setIsLoading, + setGlobalMessage, + setOpenModal, + setModalData, + }); + const allDataPerformanceEvents = dataPerformanceOverview?.map( event => event.hazardType || event.suspectedDisease ); @@ -342,89 +350,7 @@ export function useForm(formType: FormType, id?: Id): State { [configurableForm] ); - const onSaveDiseaseOutbreakEvent = useCallback(() => { - const { eventTrackerConfigurations } = configurations.selectableOptions; - - if (formState.kind !== "loaded" || !configurableForm || !formState.data.isValid) return; - - const formData = mapFormStateToEntityData( - formState.data, - currentUser.username, - configurableForm - ); - - if ( - formData.type === "disease-outbreak-event" || - formData.type === "disease-outbreak-event-case-data" - ) { - setIsLoading(true); - compositionRoot.save.execute(formData, configurations, !!id, formSectionsToDelete).run( - diseaseOutbreakEventId => { - setIsLoading(false); - - if ( - diseaseOutbreakEventId && - formData.entity && - formData.type === "disease-outbreak-event" - ) { - compositionRoot.diseaseOutbreakEvent.mapDiseaseOutbreakEventToAlerts - .execute( - diseaseOutbreakEventId, - formData.entity, - eventTrackerConfigurations.hazardTypes, - eventTrackerConfigurations.suspectedDiseases - ) - .run( - () => {}, - err => { - console.error({ err }); - } - ); - goTo(RouteName.EVENT_TRACKER, { - id: diseaseOutbreakEventId, - }); - setGlobalMessage({ - text: i18n.t(`Disease Outbreak saved successfully`), - type: "success", - }); - } else if ( - diseaseOutbreakEventId && - formData.type === "disease-outbreak-event-case-data" - ) { - goTo(RouteName.EVENT_TRACKER, { - id: diseaseOutbreakEventId, - }); - setGlobalMessage({ - text: i18n.t(`Disease outbreak case data saved successfully`), - type: "success", - }); - } - }, - err => { - setGlobalMessage({ - text: i18n.t( - formData.type === "disease-outbreak-event-case-data" - ? `Error saving disease outbreak case data: ${err.message}` - : `Error saving disease outbreak: ${err.message}` - ), - type: "error", - }); - } - ); - } - }, [ - compositionRoot, - configurableForm, - configurations, - currentUser.username, - formSectionsToDelete, - formState, - goTo, - id, - ]); - const onPrimaryButtonClick = useCallback(() => { - const { eventTrackerConfigurations } = configurations.selectableOptions; if (formState.kind !== "loaded" || !configurableForm || !formState.data.isValid) return; const formData = mapFormStateToEntityData( @@ -433,60 +359,17 @@ export function useForm(formType: FormType, id?: Id): State { configurableForm ); - const haveChangedCasesDataInDiseaseOutbreak = - !!id && - formData.type === "disease-outbreak-event" && - !formData.uploadedCasesDataFileId && - !!formData.uploadedCasesDataFile && - formData.entity?.casesDataSource === - CasesDataSource.RTSL_ZEB_OS_CASE_DATA_SOURCE_USER_DEF; - if ( - haveChangedCasesDataInDiseaseOutbreak || + formData.type === "disease-outbreak-event" || formData.type === "disease-outbreak-event-case-data" ) { - setOpenModal(true); - setModalData({ - title: i18n.t("Warning"), - content: i18n.t( - "You have uploaded a new data cases file. This action will replace the current data of this disease outbreak event with the data of the file. Are you sure you want to continue?" - ), - cancelLabel: i18n.t("Cancel"), - confirmLabel: i18n.t("Save"), - onConfirm: onSaveDiseaseOutbreakEvent, - }); + onSaveDiseaseOutbreakEvent(formData); } else { setIsLoading(true); compositionRoot.save.execute(formData, configurations, !!id, formSectionsToDelete).run( - diseaseOutbreakEventId => { + _diseaseOutbreakEventId => { setIsLoading(false); - switch (formData.type) { - case "disease-outbreak-event": - if (diseaseOutbreakEventId && formData.entity) { - compositionRoot.diseaseOutbreakEvent.mapDiseaseOutbreakEventToAlerts - .execute( - diseaseOutbreakEventId, - formData.entity, - eventTrackerConfigurations.hazardTypes, - eventTrackerConfigurations.suspectedDiseases - ) - .run( - () => {}, - err => { - console.error({ err }); - } - ); - goTo(RouteName.EVENT_TRACKER, { - id: diseaseOutbreakEventId, - }); - setGlobalMessage({ - text: i18n.t(`Disease Outbreak saved successfully`), - type: "success", - }); - } - break; - case "risk-assessment-grading": if (currentEventTracker?.id) goTo(RouteName.EVENT_TRACKER, { @@ -565,17 +448,16 @@ export function useForm(formType: FormType, id?: Id): State { ); } }, [ - configurations, - formState, + compositionRoot.save, configurableForm, + configurations, + currentEventTracker?.id, currentUser.username, - id, - onSaveDiseaseOutbreakEvent, - compositionRoot.save, - compositionRoot.diseaseOutbreakEvent.mapDiseaseOutbreakEventToAlerts, formSectionsToDelete, - currentEventTracker?.id, + formState, goTo, + id, + onSaveDiseaseOutbreakEvent, ]); const onCancelForm = useCallback(() => {