Skip to content

Commit

Permalink
Move save disease outbreak logic to new hook
Browse files Browse the repository at this point in the history
  • Loading branch information
anagperal committed Jan 3, 2025
1 parent 879ddbc commit d29bf6a
Show file tree
Hide file tree
Showing 2 changed files with 210 additions and 136 deletions.
Original file line number Diff line number Diff line change
@@ -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,
};
}
154 changes: 18 additions & 136 deletions src/webapp/pages/form-page/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,6 +88,14 @@ export function useForm(formType: FormType, id?: Id): State {
const [openModal, setOpenModal] = useState(false);
const [modalData, setModalData] = useState<ModalData>();

const { onSaveDiseaseOutbreakEvent } = useDiseaseOutbreakEventForm({
editMode: !!id,
setIsLoading,
setGlobalMessage,
setOpenModal,
setModalData,
});

const allDataPerformanceEvents = dataPerformanceOverview?.map(
event => event.hazardType || event.suspectedDisease
);
Expand Down Expand Up @@ -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(
Expand All @@ -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, {
Expand Down Expand Up @@ -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(() => {
Expand Down

0 comments on commit d29bf6a

Please sign in to comment.