Skip to content

Commit

Permalink
feat: simplify how we determine what event to redirect to
Browse files Browse the repository at this point in the history
  • Loading branch information
simonadomnisoru committed Dec 18, 2024
1 parent d643dd6 commit 743bcbf
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,9 @@ export const EnrollmentRegistrationEntry: ComponentType<OwnProps> = ({
}

const onSaveWithEnrollment = () => {
const { teiWithEnrollment, formHasError, programStageIdLinkedEventToRedirectTo } =
const { teiWithEnrollment, formHasError, relatedStageLinkedEvent } =
buildTeiWithEnrollment(relatedStageRef);
!formHasError && onSave(teiWithEnrollment, programStageIdLinkedEventToRedirectTo);
!formHasError && onSave(teiWithEnrollment, relatedStageLinkedEvent);
};

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { dataElementTypes, ProgramStage } from '../../../../metaData';
import { convertClientToServer } from '../../../../converters';
import { convertCategoryOptionsToServer } from '../../../../converters/clientToServer';
import type { RequestEvent, LinkedRequestEvent } from '../../../DataEntries';
import { generateUID } from '../../../../utils/uid/generateUID';

const ignoreAutoGenerateIfApplicable = (stage, stageToSkip) =>
!stageToSkip || stageToSkip.programStage !== stage.id;
Expand Down Expand Up @@ -75,6 +76,7 @@ export const deriveAutoGenerateEvents = ({
return {
...eventInfo,
...eventAttributeCategoryOptions,
event: generateUID(),
programStage,
program: programId,
orgUnit: orgUnitId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ export const useBuildEnrollmentPayload = ({
const buildTeiWithEnrollment = (relatedStageRef?: {current: ?RelatedStageRefPayload}): {
teiWithEnrollment: EnrollmentPayload,
formHasError: boolean,
programStageIdLinkedEventToRedirectTo?: string,
relatedStageLinkedEvent?: {
programStageId: string,
eventId: string,
},
} => {
if (!formFoundation) throw Error('form foundation object not found');
const firstStage = firstStageMetaData && firstStageMetaData.stage;
Expand Down Expand Up @@ -183,10 +186,13 @@ export const useBuildEnrollmentPayload = ({
relationships: relationship ? [relationship] : undefined,
},
formHasError,
programStageIdLinkedEventToRedirectTo:
relatedStageLinkedEvent && linkMode === relatedStageActions.ENTER_DATA
? relatedStageLinkedEvent.programStage
: undefined,
relatedStageLinkedEvent:
relatedStageLinkedEvent && linkMode === relatedStageActions.ENTER_DATA
? {
programStageId: relatedStageLinkedEvent.programStage,
eventId: relatedStageLinkedEvent.event,
}
: undefined,
};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,8 @@ import type { TeiPayload } from '../../../../../Pages/common/TEIRelationshipsWid

export type SaveForDuplicateCheck = (
teiWithEnrollment: EnrollmentPayload | TeiPayload,
programStageIdLinkedEventToRedirectTo?: string,
relatedStageLinkedEvent?: {
programStageId: string,
eventId: string,
},
) => void;
Original file line number Diff line number Diff line change
Expand Up @@ -49,26 +49,29 @@ export const saveNewTrackedEntityInstance = (candidateForRegistration: any) =>
export const startSavingNewTrackedEntityInstanceWithEnrollment = (
enrollmentPayload: EnrollmentPayload,
uid: string,
programStageIdLinkedEventToRedirectTo?: string,
relatedStageLinkedEvent?: {
programStageId: string,
eventId: string,
},
) =>
actionCreator(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_START)({
enrollmentPayload,
uid,
programStageIdLinkedEventToRedirectTo,
relatedStageLinkedEvent,
});

export const saveNewTrackedEntityInstanceWithEnrollment = ({
candidateForRegistration,
pageToRedirectTo,
uid,
stageId,
eventIndex,
eventId,
}: {
candidateForRegistration: any,
pageToRedirectTo: string,
uid: string,
stageId?: string,
eventIndex: number,
eventId?: string,
}) =>
actionCreator(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE)(
{ ...candidateForRegistration },
Expand All @@ -81,7 +84,7 @@ export const saveNewTrackedEntityInstanceWithEnrollment = ({
},
commit: {
type: registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_COMPLETED,
meta: { pageToRedirectTo, stageId, uid, eventIndex },
meta: { pageToRedirectTo, stageId, uid, eventId },
},
rollback: {
type: registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_FAILED,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ export const RegistrationDataEntry: ComponentType<OwnProps> = ({
[dispatch]);

const dispatchOnSaveWithEnrollment = useCallback(
(enrollmentPayload, programStageIdLinkedEventToRedirectTo) => {
(enrollmentPayload, relatedStageLinkedEvent) => {
const uid = uuid();
dispatch(
startSavingNewTrackedEntityInstanceWithEnrollment(
enrollmentPayload,
uid,
programStageIdLinkedEventToRedirectTo,
relatedStageLinkedEvent,
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,14 @@ export const startSavingNewTrackedEntityInstanceWithEnrollmentEpic: Epic = (
ofType(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_START),
map((action) => {
const { currentSelections: { programId } } = store.value;
const { enrollmentPayload, uid, programStageIdLinkedEventToRedirectTo } = action.payload;
const { enrollmentPayload, uid, relatedStageLinkedEvent } = action.payload;
const { stages, useFirstStageDuringRegistration } = getTrackerProgramThrowIfNotFound(programId);
const { programStageIdToRedirectTo, pageToRedirectTo } = getPageToRedirectTo(
const { stageId, eventId, pageToRedirectTo } = getPageToRedirectTo({
stages,
events: enrollmentPayload.enrollments[0]?.events,
useFirstStageDuringRegistration,
programStageIdLinkedEventToRedirectTo,
);

const eventIndex = enrollmentPayload.enrollments[0]?.events.findIndex(
eventsToBeCreated => eventsToBeCreated.programStage === programStageIdToRedirectTo,
);
relatedStageLinkedEvent,
});

return saveNewTrackedEntityInstanceWithEnrollment({
candidateForRegistration: {
Expand All @@ -73,8 +70,8 @@ export const startSavingNewTrackedEntityInstanceWithEnrollmentEpic: Epic = (
],
},
pageToRedirectTo,
eventIndex,
stageId: programStageIdToRedirectTo,
eventId,
stageId,
uid,
});
}),
Expand All @@ -92,7 +89,7 @@ export const completeSavingNewTrackedEntityInstanceWithEnrollmentEpic = (
payload: {
bundleReport: { typeReportMap },
},
meta: { uid, pageToRedirectTo, stageId, eventIndex },
meta: { uid, pageToRedirectTo, stageId, eventId },
} = action;
const {
currentSelections: { orgUnitId, programId },
Expand All @@ -101,7 +98,6 @@ export const completeSavingNewTrackedEntityInstanceWithEnrollmentEpic = (
const { uid: stateUid } = newPage || {};
const teiId = typeReportMap.TRACKED_ENTITY.objectReports[0].uid;
const enrollmentId = typeReportMap.ENROLLMENT.objectReports[0].uid;
const eventId = typeReportMap.EVENT.objectReports?.[eventIndex]?.uid;

if (stateUid !== uid) {
return EMPTY;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,40 +14,61 @@ export const PAGES = {
* When the event will be created redirect to enrollmentEventEdit
* When the event will not be created redirect to enrollmentEventNew
*/
export const getPageToRedirectTo = (
export const getPageToRedirectTo = ({
stages,
events,
useFirstStageDuringRegistration,
relatedStageLinkedEvent,
}: {
stages: Map<string, ProgramStage>,
events: any,
useFirstStageDuringRegistration: boolean,
programStageIdLinkedEventToRedirectTo?: string,
) => {
relatedStageLinkedEvent?: {
programStageId: string,
eventId: string,
},
}): { stageId?: string, eventId?: string, pageToRedirectTo: string } => {
const stagesArray = [...stages.values()];
const [firstStageWithOpenAfterEnrollment] = stagesArray.filter(({ openAfterEnrollment }) => openAfterEnrollment);

const pageToRedirectTo = (() => {
// event will be created and linked during first stage registration
if (programStageIdLinkedEventToRedirectTo) {
return PAGES.enrollmentEventEdit;
// event will be created and linked during first stage registration
if (relatedStageLinkedEvent?.eventId) {
const { programStageId, eventId } = relatedStageLinkedEvent;
return {
stageId: programStageId,
eventId,
pageToRedirectTo: PAGES.enrollmentEventEdit,
};
}

if (firstStageWithOpenAfterEnrollment) {
// event will be created during first stage registration
if (useFirstStageDuringRegistration && stagesArray[0].id === firstStageWithOpenAfterEnrollment.id) {
const eventId = events.find(e => e.programStage === firstStageWithOpenAfterEnrollment.id).event;

return {
programStageIdToRedirectTo: firstStageWithOpenAfterEnrollment.id,
eventId,
pageToRedirectTo: PAGES.enrollmentEventEdit,
};
}
if (firstStageWithOpenAfterEnrollment) {
// event will be created during first stage registration
if (
useFirstStageDuringRegistration
&& stagesArray[0].id === firstStageWithOpenAfterEnrollment.id
) {
return PAGES.enrollmentEventEdit;
}
// event will be autogenerated
if (
stagesArray.find(stage => stage.autoGenerateEvent && stage.id === firstStageWithOpenAfterEnrollment.id)
) {
return PAGES.enrollmentEventEdit;
}
return PAGES.enrollmentEventNew;

// event will be autogenerated
if (stagesArray.find(stage => stage.autoGenerateEvent && stage.id === firstStageWithOpenAfterEnrollment.id)) {
const eventId = events.find(e => e.programStage === firstStageWithOpenAfterEnrollment.id).event;

return {
programStageIdToRedirectTo: firstStageWithOpenAfterEnrollment.id,
eventId,
pageToRedirectTo: PAGES.enrollmentEventEdit,
};
}
return PAGES.enrollmentDashboard;
})();

return {
programStageIdToRedirectTo: programStageIdLinkedEventToRedirectTo || firstStageWithOpenAfterEnrollment?.id,
pageToRedirectTo,
};
return {
programStageIdToRedirectTo: firstStageWithOpenAfterEnrollment.id,
pageToRedirectTo: PAGES.enrollmentEventNew,
};
}

return { pageToRedirectTo: PAGES.enrollmentDashboard };
};
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import i18n from '@dhis2/d2-i18n';
import { isValidOrgUnit } from '../../../../capture-core-utils/validators/form';
import { isValidDate } from '../../../utils/validation/validators/form';
import { relatedStageActions } from '../index';
import { relatedStageActions } from '../constants';

type Props = {
scheduledAt: ?string,
Expand Down

0 comments on commit 743bcbf

Please sign in to comment.