From a9037dd77fb209f97756f0a1c964d76995101b4a Mon Sep 17 00:00:00 2001 From: Shreeyash Shrestha Date: Mon, 16 Dec 2024 15:58:19 +0545 Subject: [PATCH] Show latest form values changes for local unit validation --- .../domain/BaseMapPointInput/index.tsx | 13 + .../BaseMapPointInput/styles.module.css | 4 + .../LocalUnitsForm/index.tsx | 284 ++++++++++++++++++ .../LocalUnitsForm/styles.module.css | 5 + .../LocalUnitsFormModal/index.tsx | 7 + .../LocalUnitsMap/index.tsx | 27 +- .../LocalUnitTableActions/index.tsx | 28 +- packages/ui/src/index.css | 1 + 8 files changed, 363 insertions(+), 6 deletions(-) diff --git a/app/src/components/domain/BaseMapPointInput/index.tsx b/app/src/components/domain/BaseMapPointInput/index.tsx index e342e0d7f2..a55ec5ba80 100644 --- a/app/src/components/domain/BaseMapPointInput/index.tsx +++ b/app/src/components/domain/BaseMapPointInput/index.tsx @@ -56,6 +56,10 @@ interface Props extends BaseMapProps { readOnly?: boolean; required?: boolean; error?: ObjectError; + baseMapFormFieldsChanges: { + lat: boolean | undefined; + lng: boolean | undefined; + } | undefined; } function BaseMapPointInput(props: Props) { @@ -74,6 +78,7 @@ function BaseMapPointInput(props: Props) { country, required, error, + baseMapFormFieldsChanges, ...otherProps } = props; @@ -183,6 +188,10 @@ function BaseMapPointInput(props: Props) {
(props: Props) { required={required} /> = {}; + + Object.keys(flattenedNewValues).forEach((key) => { + const newValue = flattenedNewValues[key]; + const oldValue = flattenedOldValues[key]; + + if (Array.isArray(newValue) && Array.isArray(oldValue)) { + if (!doArraysContainSameElements(newValue, oldValue)) { + changedKeys[key] = true; + } + } else if (newValue !== oldValue) { + changedKeys[key] = true; + } + }); + + return changedKeys; +} + interface FormColumnContainerProps { children: React.ReactNode; } @@ -233,6 +261,7 @@ interface Props { actionsContainerRef: RefObject; headingDescriptionRef?: RefObject; headerDescriptionRef: RefObject; + previousData?: LocalUnitResponse; } function LocalUnitsForm(props: Props) { @@ -245,6 +274,7 @@ function LocalUnitsForm(props: Props) { headingDescriptionRef, headerDescriptionRef, onDeleteActionSuccess, + previousData, } = props; const [showChangesModal, { @@ -345,6 +375,7 @@ function LocalUnitsForm(props: Props) { english_branch_name, level, focal_person_en, + focal_person_loc, date_of_data, source_loc, source_en, @@ -369,6 +400,7 @@ function LocalUnitsForm(props: Props) { english_branch_name, level, focal_person_en, + focal_person_loc, date_of_data, source_loc, source_en, @@ -585,6 +617,21 @@ function LocalUnitsForm(props: Props) { [revertChanges], ); + const latestChangesFormFields = useMemo(() => { + if (isDefined(localUnitDetailsResponse) && isDefined(previousData)) { + return getLatestChangesFormFields( + localUnitDetailsResponse, + previousData, + ); + } + return undefined; + }, [localUnitDetailsResponse, previousData]); + + const baseMapFormFieldsChanges = useMemo(() => ({ + lat: latestChangesFormFields?.['location_json.lat'], + lng: latestChangesFormFields?.['location_json.lng'], + }), [latestChangesFormFields]); + const onDoneButtonClick = useCallback( () => { if (isDefined(localUnitDetailsResponse) && isDefined(localUnitsOptions)) { @@ -692,6 +739,9 @@ function LocalUnitsForm(props: Props) { {value.type !== TYPE_HEALTH_CARE && ( @@ -979,6 +1091,10 @@ function LocalUnitsForm(props: Props) { spacing="comfortable" >