Skip to content

Commit

Permalink
Merge branch 'DHIS2-17192' into DHIS2-18018
Browse files Browse the repository at this point in the history
  • Loading branch information
simonadomnisoru committed Jan 14, 2025
2 parents f8bcf40 + e4a16ca commit 55aa99f
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 130 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, relatedStageLinkedEvent } =
const { teiWithEnrollment, formHasError, redirect } =
buildTeiWithEnrollment(relatedStageRef);
!formHasError && onSave(teiWithEnrollment, relatedStageLinkedEvent);
!formHasError && onSave(teiWithEnrollment, redirect);
};

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// @flow
import { ProgramStage } from '../../../../metaData';
import { relatedStageActions } from '../../../WidgetRelatedStages';

/**
* An event can be
* - created during first stage registration
* - created and linked during first stage registration
* - autogenerated
* When the event will be created redirect to enrollmentEventEdit
* When the event will not be created redirect to enrollmentEventNew
*/
export const getRedirectIds = ({
stages,
relatedStageLinkedEvent,
linkMode,
firstStageDuringRegistrationEvent,
autoGenerateEvents,
}: {
stages: Map<string, ProgramStage>,
linkMode: ?string,
relatedStageLinkedEvent: ?{
programStage: string,
event: string,
},
firstStageDuringRegistrationEvent: ?{
programStage: string,
event: string,
},
autoGenerateEvents: Array<{
programStage: string,
event: string,
orgUnit: string,
}>,
}): { programStageId?: string, eventId?: string } => {
if (relatedStageLinkedEvent && linkMode === relatedStageActions.ENTER_DATA) {
return { eventId: relatedStageLinkedEvent.event };
}

const [firstStageWithOpenAfterEnrollment] = [...stages.values()].filter(
({ openAfterEnrollment }) => openAfterEnrollment,
);

if (firstStageWithOpenAfterEnrollment) {
// event will be created during first stage registration
if (
firstStageDuringRegistrationEvent &&
firstStageDuringRegistrationEvent.programStage === firstStageWithOpenAfterEnrollment.id
) {
return { eventId: firstStageDuringRegistrationEvent.event };
}

// event will be autogenerated
const autoGenerateEvent = autoGenerateEvents.find(e => e.programStage === firstStageWithOpenAfterEnrollment.id);
if (autoGenerateEvent) {
return { eventId: autoGenerateEvent.event };
}

return { programStageId: firstStageWithOpenAfterEnrollment.id };
}

return {};
};
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ import {
} from '../helpers';
import type { EnrollmentPayload } from '../EnrollmentRegistrationEntry.types';
import { geometryType, getPossibleTetFeatureTypeKey, buildGeometryProp } from '../../common/TEIAndEnrollment/geometry';
import { relatedStageActions } from '../../../WidgetRelatedStages';
import type { RelatedStageRefPayload } from '../../../WidgetRelatedStages';
import { getRedirectIds } from './getRedirectIds';

type DataEntryReduxConverterProps = {
programId: string;
Expand Down Expand Up @@ -94,9 +94,9 @@ export const useBuildEnrollmentPayload = ({
const buildTeiWithEnrollment = (relatedStageRef?: {current: ?RelatedStageRefPayload}): {
teiWithEnrollment: EnrollmentPayload,
formHasError: boolean,
relatedStageLinkedEvent?: {
programStageId: string,
eventId: string,
redirect: {
programStageId?: string,
eventId?: string,
},
} => {
if (!formFoundation) throw Error('form foundation object not found');
Expand Down Expand Up @@ -156,6 +156,14 @@ export const useBuildEnrollmentPayload = ({
serverMinorVersion: minor,
});

const redirect = getRedirectIds({
stages,
relatedStageLinkedEvent,
linkMode,
firstStageDuringRegistrationEvent,
autoGenerateEvents,
});

const allEventsToBeCreated = [firstStageDuringRegistrationEvent, relatedStageLinkedEvent, ...autoGenerateEvents]
.filter(Boolean);

Expand Down Expand Up @@ -186,13 +194,7 @@ export const useBuildEnrollmentPayload = ({
relationships: relationship ? [relationship] : undefined,
},
formHasError,
relatedStageLinkedEvent:
relatedStageLinkedEvent && linkMode === relatedStageActions.ENTER_DATA
? {
programStageId: relatedStageLinkedEvent.programStage,
eventId: relatedStageLinkedEvent.event,
}
: undefined,
redirect,
};
};

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

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

export const saveNewTrackedEntityInstanceWithEnrollment = ({
candidateForRegistration,
pageToRedirectTo,
uid,
stageId,
eventId,
redirect,
}: {
candidateForRegistration: any,
pageToRedirectTo: string,
uid: string,
stageId?: string,
eventId?: string,
redirect: {
programStageId?: string,
eventId?: string,
}
}) =>
actionCreator(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE)(
{ ...candidateForRegistration },
Expand All @@ -84,7 +83,7 @@ export const saveNewTrackedEntityInstanceWithEnrollment = ({
},
commit: {
type: registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_COMPLETED,
meta: { pageToRedirectTo, stageId, uid, eventId },
meta: { redirect, uid },
},
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, relatedStageLinkedEvent) => {
(enrollmentPayload, redirect) => {
const uid = uuid();
dispatch(
startSavingNewTrackedEntityInstanceWithEnrollment(
enrollmentPayload,
uid,
relatedStageLinkedEvent,
redirect,
),
);
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,10 @@ import {
saveNewTrackedEntityInstance,
saveNewTrackedEntityInstanceWithEnrollment,
} from './RegistrationDataEntry.actions';
import { getTrackerProgramThrowIfNotFound } from '../../../../metaData';
import {
navigateToEnrollmentOverview,
} from '../../../../actions/navigateToEnrollmentOverview/navigateToEnrollmentOverview.actions';
import { buildUrlQueryString } from '../../../../utils/routing';
import {
getPageToRedirectTo,
PAGES,
} from './helpers';
import { cleanUpUid } from '../NewPage.actions';

export const startSavingNewTrackedEntityInstanceEpic: Epic = (action$: InputObservable) =>
Expand Down Expand Up @@ -48,30 +43,19 @@ export const completeSavingNewTrackedEntityInstanceEpic: Epic = (action$: InputO

export const startSavingNewTrackedEntityInstanceWithEnrollmentEpic: Epic = (
action$: InputObservable,
store: ReduxStore,
) =>
action$.pipe(
ofType(registrationFormActionTypes.NEW_TRACKED_ENTITY_INSTANCE_WITH_ENROLLMENT_SAVE_START),
map((action) => {
const { currentSelections: { programId } } = store.value;
const { enrollmentPayload, uid, relatedStageLinkedEvent } = action.payload;
const { stages, useFirstStageDuringRegistration } = getTrackerProgramThrowIfNotFound(programId);
const { stageId, eventId, pageToRedirectTo } = getPageToRedirectTo({
stages,
events: enrollmentPayload.enrollments[0]?.events,
useFirstStageDuringRegistration,
relatedStageLinkedEvent,
});
const { enrollmentPayload, uid, redirect } = action.payload;

return saveNewTrackedEntityInstanceWithEnrollment({
candidateForRegistration: {
trackedEntities: [
enrollmentPayload,
],
},
pageToRedirectTo,
eventId,
stageId,
redirect,
uid,
});
}),
Expand All @@ -89,7 +73,7 @@ export const completeSavingNewTrackedEntityInstanceWithEnrollmentEpic = (
payload: {
bundleReport: { typeReportMap },
},
meta: { uid, pageToRedirectTo, stageId, eventId },
meta: { uid, redirect },
} = action;
const {
currentSelections: { orgUnitId, programId },
Expand All @@ -103,23 +87,23 @@ export const completeSavingNewTrackedEntityInstanceWithEnrollmentEpic = (
return EMPTY;
}

if (pageToRedirectTo === PAGES.enrollmentEventNew) {
if (redirect.programStageId) {
history.push(
`/${pageToRedirectTo}?${buildUrlQueryString({
`/enrollmentEventNew?${buildUrlQueryString({
programId,
orgUnitId,
teiId,
enrollmentId,
stageId,
stageId: redirect.programStageId,
})}`,
);
return EMPTY;
}

if (pageToRedirectTo === PAGES.enrollmentEventEdit) {
if (redirect.eventId) {
history.push(
`/${pageToRedirectTo}?${buildUrlQueryString({
eventId,
`/enrollmentEventEdit?${buildUrlQueryString({
eventId: redirect.eventId,
orgUnitId,
initMode: dataEntryKeys.EDIT,
})}`,
Expand Down

This file was deleted.

This file was deleted.

0 comments on commit 55aa99f

Please sign in to comment.