Skip to content

Commit

Permalink
when the action of adding or removing a project is cancelled the sele…
Browse files Browse the repository at this point in the history
…cted projects should stay as it was
  • Loading branch information
hakifran committed Nov 27, 2024
1 parent 5776449 commit 3320f39
Showing 1 changed file with 37 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ export const OrgUnitsTypesDialog: FunctionComponent<Props> = ({

const formStateUpdated = useRef(null);
const projectsEmptyUpdated = useRef(null);
const prevProjectIds = useRef(formState.project_ids.value);

const { formatMessage } = useSafeIntl();

const [referenceFormsMessage, setReferenceFormsMessage] = useState(
Expand All @@ -110,6 +112,7 @@ export const OrgUnitsTypesDialog: FunctionComponent<Props> = ({
const [projectsEmpty, setProjectsEmpty] = useState(
!!isEmpty(formState.project_ids.value),
);
const [selectedProjectIds, setSelectedProjectIds] = useState(null);

const { data: allProjects } = useGetProjectsDropdownOptions();
const { data: allOrgUnitTypes, isLoading: isLoadingOrgUitTypes } =
Expand Down Expand Up @@ -176,6 +179,30 @@ export const OrgUnitsTypesDialog: FunctionComponent<Props> = ({

const currentUser = useCurrentUser();

const handleOpenConfirmModal = useCallback(
newProjectIds => {
prevProjectIds.current = formState.project_ids.value;
setSelectedProjectIds(newProjectIds);
setConfirmCancelDialogOpen(true);
},
[formState.project_ids.value],
);

const handleDialogConfirm = useCallback(() => {
if (selectedProjectIds) {
setAllForms(getFormPerProjects(selectedProjectIds));
setFieldValue('project_ids', selectedProjectIds);
}
setSelectedProjectIds(null);
setConfirmCancelDialogOpen(false);
}, [getFormPerProjects, selectedProjectIds, setFieldValue]);

const handleDialogCancel = useCallback(() => {
setFieldValue('project_ids', prevProjectIds.current);
setSelectedProjectIds(null);
setConfirmCancelDialogOpen(false);
}, [setFieldValue]);

const onChange = useCallback(
(keyValue, value) => {
if (
Expand All @@ -186,14 +213,14 @@ export const OrgUnitsTypesDialog: FunctionComponent<Props> = ({
) {
setFieldValue(keyValue, commaSeparatedIdsToArray(value));
if (keyValue === 'project_ids') {
if (formState.reference_forms_ids.value.length > 0) {
setConfirmCancelDialogOpen(true);
}

const projectIds = value
?.split(',')
.map((val: string) => parseInt(val, 10));
setAllForms(getFormPerProjects(projectIds));
.map(val => parseInt(val, 10));
if (formState.reference_forms_ids.value.length > 0) {
handleOpenConfirmModal(projectIds);
} else {
setAllForms(getFormPerProjects(projectIds));
}
}
} else {
setFieldValue(keyValue, value);
Expand All @@ -208,6 +235,7 @@ export const OrgUnitsTypesDialog: FunctionComponent<Props> = ({
[
setFieldValue,
formState.reference_forms_ids.value.length,
handleOpenConfirmModal,
getFormPerProjects,
setFieldErrors,
formatMessage,
Expand Down Expand Up @@ -278,13 +306,7 @@ export const OrgUnitsTypesDialog: FunctionComponent<Props> = ({
}, [allProjects, orgUnitType.projects]);
const [confirmCancelDialogOpen, setConfirmCancelDialogOpen] =
useState(false);
const handleCustomDialogConfirm = () => {
setConfirmCancelDialogOpen(false);
};

const handleCustomDialogCancel = () => {
setConfirmCancelDialogOpen(false);
};
return (
// @ts-ignore
<>
Expand Down Expand Up @@ -405,15 +427,15 @@ export const OrgUnitsTypesDialog: FunctionComponent<Props> = ({

{/* @ts-ignore */}
<ConfirmCancelModal
onConfirm={() => handleCustomDialogConfirm()}
onConfirm={() => handleDialogConfirm()}
cancelMessage={MESSAGES.cancel}
confirmMessage={MESSAGES.confirm}
maxWidth="md"
open={confirmCancelDialogOpen}
closeDialog={() => null}
onClose={() => handleCustomDialogCancel()}
onClose={() => handleDialogCancel()}
onCancel={() => {
handleCustomDialogCancel();
handleDialogCancel();
}}
id="confirm-cancel-dialog"
dataTestId="confirm-cancel-dialog"
Expand Down

0 comments on commit 3320f39

Please sign in to comment.