Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: [DHIS2-17192] show related stages widget on registration page #3880

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
ba81d9c
feat: show related stages Widget on registration page
simonadomnisoru Nov 14, 2024
293c359
feat: add enableLinkExistingEvent prop
simonadomnisoru Nov 18, 2024
0bd5fa9
Merge branch 'master' into DHIS2-17192
simonadomnisoru Nov 18, 2024
d88b9c2
Merge branch 'master' into DHIS2-17192
simonadomnisoru Nov 20, 2024
ddb3f0e
chore: improve wording
simonadomnisoru Nov 27, 2024
3eb3c93
feat: show related stages widget when adding a new relationship
simonadomnisoru Dec 2, 2024
c1a1edb
Merge branch 'master' into DHIS2-17192
simonadomnisoru Dec 2, 2024
32c824f
Merge branch 'master' into DHIS2-17192
simonadomnisoru Dec 5, 2024
9b2d205
Merge branch 'master' into DHIS2-17192
simonadomnisoru Dec 9, 2024
2a27f57
chore: typo
simonadomnisoru Dec 9, 2024
9ecf999
chore: fix PR comments
simonadomnisoru Dec 11, 2024
28e26b8
chore: correct the variable name to match its format
simonadomnisoru Dec 17, 2024
d137e8e
chore: move helpers files to DataEntries/EnrollmentRegistrationEntry
simonadomnisoru Dec 17, 2024
0dc2f01
chore: use action naming throughout the code
simonadomnisoru Dec 17, 2024
6b97817
chore: rename helper file to getRelationshipNewTeiName
simonadomnisoru Dec 17, 2024
d643dd6
Merge branch 'master' into DHIS2-17192
simonadomnisoru Dec 17, 2024
743bcbf
feat: simplify how we determine what event to redirect to
simonadomnisoru Dec 18, 2024
8366d23
Merge branch 'master' into DHIS2-17192
simonadomnisoru Jan 7, 2025
e4a16ca
fix: simplify how we determine what event to redirect to
simonadomnisoru Jan 13, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions i18n/en.pot
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ msgstr ""
"Content-Type: text/plain; charset=utf-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1)\n"
"POT-Creation-Date: 2024-12-03T10:58:18.077Z\n"
"PO-Revision-Date: 2024-12-03T10:58:18.077Z\n"
"POT-Creation-Date: 2024-12-09T15:48:11.192Z\n"
"PO-Revision-Date: 2024-12-09T15:48:11.192Z\n"

msgid "Choose one or more dates..."
msgstr "Choose one or more dates..."
Expand Down Expand Up @@ -774,6 +774,9 @@ msgstr "Save {{trackedEntityTypeName}}"
msgid "Save {{trackedEntityName}}"
msgstr "Save {{trackedEntityName}}"

msgid "Enter details now is not available when creating a relationship"
msgstr "Enter details now is not available when creating a relationship"

msgid "Save new {{trackedEntityTypeName}} and link"
msgstr "Save new {{trackedEntityTypeName}} and link"

Expand Down Expand Up @@ -1376,8 +1379,8 @@ msgstr "{{trackedEntityTypeName}} profile"
msgid "tracked entity instance"
msgstr "tracked entity instance"

msgid "Link to an existing {{linkableStageLabel}}"
msgstr "Link to an existing {{linkableStageLabel}}"
msgid "Choose a {{linkableStageLabel}} event"
msgstr "Choose a {{linkableStageLabel}} event"

msgid "Choose a {{linkableStageLabel}}"
msgstr "Choose a {{linkableStageLabel}}"
Expand All @@ -1388,14 +1391,17 @@ msgstr "{{ linkableStageLabel }} is not repeatable"
msgid "{{ linkableStageLabel }} has no linkable events"
msgstr "{{ linkableStageLabel }} has no linkable events"

msgid "Actions - {{relationshipName}}"
msgstr "Actions - {{relationshipName}}"

msgid "Ambiguous relationships, contact system administrator"
msgstr "Ambiguous relationships, contact system administrator"

msgid "Enter details now"
msgstr "Enter details now"

msgid "Link to an existing"
msgstr "Link to an existing"
msgid "Link to an existing event"
msgstr "Link to an existing event"

msgid "Scheduled date"
msgstr "Scheduled date"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import {
withAOCFieldBuilder,
withDataEntryFields,
} from '../../DataEntryDhis2Helpers';
import type { RelatedStageRefPayload } from '../../WidgetRelatedStages';

const overrideMessagePropNames = {
errorMessage: 'validationError',
Expand Down Expand Up @@ -333,6 +334,7 @@ type FinalTeiDataEntryProps = {
onUpdateFormFieldAsync: Function,
onUpdateFormField: Function,
firstStageMetaData?: ?{ stage: ProgramStage },
relatedStageRef?: { current: ?RelatedStageRefPayload },
formFoundation: RenderFoundation,
};
// final step before the generic dataEntry is inserted
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ import type { Props } from './EnrollmentWithFirstStageDataEntry.types';
import { FirstStageDataEntry } from './EnrollmentWithFirstStageDataEntry.component';
import { useDataEntrySections } from './hooks';
import { Section } from '../../../../metaData';
import { WidgetRelatedStages } from '../../../WidgetRelatedStages';

const getSectionId = sectionId =>
(sectionId === Section.MAIN_SECTION_ID ? `${Section.MAIN_SECTION_ID}-stage` : sectionId);

export const EnrollmentWithFirstStageDataEntry = (props: Props) => {
const { firstStageMetaData, ...passOnProps } = props;
const { firstStageMetaData, relatedStageRef, relatedStageModesOptions, ...passOnProps } = props;
const {
stage: { stageForm: firstStageFormFoundation, name: stageName },
} = firstStageMetaData;
Expand All @@ -19,10 +20,18 @@ export const EnrollmentWithFirstStageDataEntry = (props: Props) => {
const dataEntrySections = useDataEntrySections(stageName, beforeSectionId);

return (
<FirstStageDataEntry
{...passOnProps}
firstStageMetaData={firstStageMetaData}
dataEntrySections={dataEntrySections}
/>
<>
<FirstStageDataEntry
{...passOnProps}
firstStageMetaData={firstStageMetaData}
dataEntrySections={dataEntrySections}
/>
<WidgetRelatedStages
ref={relatedStageRef}
programId={passOnProps.programId}
programStageId={firstStageMetaData.stage?.id}
actionTypesOptions={relatedStageModesOptions}
JoakimSM marked this conversation as resolved.
Show resolved Hide resolved
/>
</>
);
};
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
// @flow
import type { ProgramStage, RenderFoundation } from '../../../../metaData';
import type { RelatedStageRefPayload } from '../../../WidgetRelatedStages';
import { RelatedStageModes } from '../../../WidgetRelatedStages';

export type Props = {
firstStageMetaData: {
stage: ProgramStage,
},
formFoundation: RenderFoundation,
programId: string,
relatedStageRef?: { current: ?RelatedStageRefPayload },
relatedStageModesOptions?: {
[key: $Keys<typeof RelatedStageModes>]: {
hidden?: boolean,
disabled?: boolean,
disabledMessage?: string
},
},
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// @flow
import React from 'react';
import React, { useRef } from 'react';
import type { ComponentType } from 'react';
import { useSelector } from 'react-redux';
import { EnrollmentRegistrationEntryComponent } from './EnrollmentRegistrationEntry.component';
Expand All @@ -10,6 +10,7 @@ import { dataEntryHasChanges } from '../../DataEntry/common/dataEntryHasChanges'
import {
useBuildEnrollmentPayload,
} from './hooks/useBuildEnrollmentPayload';
import type { RelatedStageRefPayload } from '../../WidgetRelatedStages';

export const EnrollmentRegistrationEntry: ComponentType<OwnProps> = ({
selectedScopeId,
Expand All @@ -22,6 +23,7 @@ export const EnrollmentRegistrationEntry: ComponentType<OwnProps> = ({
onCancel,
...passOnProps
}) => {
const relatedStageRef = useRef<?RelatedStageRefPayload>(null);
const { orgUnit, error } = useCoreOrgUnit(orgUnitId);
const {
ready,
Expand Down Expand Up @@ -57,13 +59,15 @@ export const EnrollmentRegistrationEntry: ComponentType<OwnProps> = ({
}

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

return (
<EnrollmentRegistrationEntryComponent
{...passOnProps}
relatedStageRef={relatedStageRef}
firstStageMetaData={firstStageMetaData}
selectedScopeId={selectedScopeId}
formId={formId}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import type { SaveForDuplicateCheck } from '../common/TEIAndEnrollment/Duplicate
import type { ExistingUniqueValueDialogActionsComponent } from '../withErrorMessagePostProcessor';
import type { InputAttribute } from './hooks/useFormValues';
import { RenderFoundation, ProgramStage } from '../../../metaData';
import type { RelatedStageRefPayload } from '../../WidgetRelatedStages';
import { RelatedStageModes } from '../../WidgetRelatedStages';

type TrackedEntityAttributes = Array<{
attribute: string,
Expand All @@ -32,6 +34,21 @@ export type EnrollmentPayload = {|
attributes: TrackedEntityAttributes,
geometry: any,
|}
],
relationships?: [
{
relationshipType: string,
from: {
event: {
event: string,
},
},
to: {
event: {
event: string,
},
},
}
]
|}

Expand All @@ -51,6 +68,14 @@ export type OwnProps = $ReadOnly<{|
trackedEntityInstanceAttributes?: Array<InputAttribute>,
saveButtonText: (trackedEntityName: string) => string,
firstStageMetaData?: ?{ stage: ?ProgramStage },
relatedStageRef?: { current: ?RelatedStageRefPayload },
relatedStageModesOptions?: {
[key: $Keys<typeof RelatedStageModes>]: {
hidden?: boolean,
disabled?: boolean,
disabledMessage?: string
},
},
|}>;

type ContainerProps = {|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ import {
import {
deriveAutoGenerateEvents,
deriveFirstStageDuringRegistrationEvent,
deriveRelatedStageEvent,
} from '../../../Pages/New/RegistrationDataEntry/helpers';
JoakimSM marked this conversation as resolved.
Show resolved Hide resolved
import type { EnrollmentPayload } from '../EnrollmentRegistrationEntry.types';
import { geometryType, getPossibleTetFeatureTypeKey, buildGeometryProp } from '../../common/TEIAndEnrollment/geometry';
import { RelatedStageModes } from '../../../WidgetRelatedStages';
import type { RelatedStageRefPayload } from '../../../WidgetRelatedStages';

type DataEntryReduxConverterProps = {
programId: string;
Expand Down Expand Up @@ -88,7 +91,11 @@ export const useBuildEnrollmentPayload = ({
const { firstStageMetaData } = useBuildFirstStageRegistration(programId);
const { formFoundation } = useMergeFormFoundationsIfApplicable(scopeFormFoundation, firstStageMetaData);

const buildTeiWithEnrollment = (): EnrollmentPayload => {
const buildTeiWithEnrollment = (relatedStageRef?: {current: ?RelatedStageRefPayload}): {
teiWithEnrollment: EnrollmentPayload,
formHasError: boolean,
programStageIdLinkedEventToRedirectTo?: string,
} => {
if (!formFoundation) throw Error('form foundation object not found');
const firstStage = firstStageMetaData && firstStageMetaData.stage;
const clientValues = getClientValuesForFormData(formValues, formFoundation);
Expand Down Expand Up @@ -126,8 +133,17 @@ export const useBuildEnrollmentPayload = ({
serverMinorVersion: minor,
});

const { formHasError, linkedEvent: relatedStageLinkedEvent, relationship, linkMode } = deriveRelatedStageEvent({
clientRequestEvent: firstStageDuringRegistrationEvent,
relatedStageRef,
firstStageMetaData,
programId,
teiId,
});

const autoGenerateEvents = deriveAutoGenerateEvents({
firstStageMetadata: firstStage,
firstStageDuringRegistrationEvent,
relatedStageLinkedEvent,
stages,
enrolledAt,
occurredAt,
Expand All @@ -137,9 +153,8 @@ export const useBuildEnrollmentPayload = ({
serverMinorVersion: minor,
});

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

const attributes = deriveAttributesFromFormValues(formServerValues);

Expand All @@ -155,17 +170,23 @@ export const useBuildEnrollmentPayload = ({
};

const tetFeatureTypeKey = getPossibleTetFeatureTypeKey(formServerValues);
const tetGeometry = tetFeatureTypeKey ?
buildGeometryProp(tetFeatureTypeKey, formValues)
: undefined;
const tetGeometry = tetFeatureTypeKey ? buildGeometryProp(tetFeatureTypeKey, formValues) : undefined;

return {
trackedEntity: teiId || generateUID(),
orgUnit: orgUnitId,
trackedEntityType: trackedEntityTypeId,
attributes,
geometry: tetGeometry,
enrollments: [enrollment],
teiWithEnrollment: {
trackedEntity: teiId || generateUID(),
orgUnit: orgUnitId,
trackedEntityType: trackedEntityTypeId,
attributes,
geometry: tetGeometry,
enrollments: [enrollment],
relationships: relationship ? [relationship] : undefined,
},
formHasError,
programStageIdLinkedEventToRedirectTo:
relatedStageLinkedEvent && linkMode === RelatedStageModes.ENTER_DATA
? relatedStageLinkedEvent.programStage
: undefined,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a good time to simplify how we determine what event to redirect to. My suggestion would be that we in this function (buildTeiWithEnrollment) get the eventId for the event. For related stages we are generating the eventId on the client, we will have to do this for auto generated events too. Then we should be able to write the logic to determine the eventId (that we will redirect to). Having the related stage take precedence makes sense to me (like you already did).

This way we can simplify the epics in the chain quite a bit and the code would be easier to reason about.

Might be missing something of course. Happy to talk about it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To elaborate: On line 159 in useBuildEnrollmentPayload add something like: const redirectEvent = getEventToOpen(.......);.

  1. Check for ENTER_DATA for related stages.
  2. Check for "Open data entry form after enrollment" on the stages. If a stage is found, Get an event for that stage among the auto-generated ones (and probably the "first stage appears on registration page" one).

Let's discuss this one.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JoakimSM the changes have been implemented. Can you have another look? Thank you!

};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from '../../../../../DataEntry/actions/dataEntry.actions';
import { getDataEntryKey } from '../../../../../DataEntry/common/getDataEntryKey';
import { convertClientRelationshipToServer } from '../../../../../../relationships/convertClientToServer';
import { getRelationshipNewTei } from '../../../../../Pages/NewRelationship/RegisterTei';
import { getRelationshipNewTeiName } from '../../../../../Pages/NewRelationship/RegisterTei';

const dataEntryId = 'singleEvent';
const itemId = 'newEvent';
Expand All @@ -47,13 +47,8 @@ export const addRelationshipForNewSingleEventEpic = (action$: InputObservable, s
const state = store.value;
const existingRelationships = state.dataEntriesRelationships[dataEntryKey] || [];
const payload = action.payload;
const entity = payload.entity;
const toEntity = payload.entity;

const toEntity = entity.id ? entity : getRelationshipNewTei(entity.dataEntryId, entity.itemId, state);
JoakimSM marked this conversation as resolved.
Show resolved Hide resolved
const toEntityIsNew = !entity.id;
const newToEntity = toEntityIsNew ? {
dataEntryId: entity.dataEntryId,
} : null;

const newRelationship = {
clientId: uuid(),
Expand All @@ -64,6 +59,7 @@ export const addRelationshipForNewSingleEventEpic = (action$: InputObservable, s
},
to: {
...toEntity,
name: toEntity.name || getRelationshipNewTeiName(toEntity.dataEntryId, toEntity.itemId, state),
type: payload.entityType,
},
relationshipType: { ...payload.relationshipType },
Expand All @@ -87,7 +83,7 @@ export const addRelationshipForNewSingleEventEpic = (action$: InputObservable, s

return batchActions([
recentlyAddedRelationship(newRelationship.clientId),
addRelationship(dataEntryId, itemId, newRelationship, newToEntity),
addRelationship(dataEntryId, itemId, newRelationship, toEntity),
], newEventNewRelationshipBatchActionTypes.ADD_RELATIONSHIP_BATCH);
}));

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

export type SaveForDuplicateCheck = (
teiWithEnrollment: EnrollmentPayload | TeiPayload,
programStageIdLinkedEventToRedirectTo?: string,
) => void;
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { CompleteModal } from './CompleteModal';
import { statusTypes as eventStatuses } from '../../../../../events/statusTypes';
import { type RenderFoundation } from '../../../../../metaData';
import { addEventSaveTypes } from '../../../../WidgetEnrollmentEventNew/DataEntry/addEventSaveTypes';
import { actions as LinkModes } from '../../../../WidgetRelatedStages/constants';
import type { RelatedStageRefPayload } from '../../../../WidgetEnrollmentEventNew/Validated/validated.types';
import { RelatedStageModes } from '../../../../WidgetRelatedStages';
import type { RelatedStageRefPayload } from '../../../../WidgetRelatedStages';

type Props = {
onSave: (eventId: string, dataEntryId: string, formFoundation: RenderFoundation, saveType?: ?string) => void,
askCompleteEnrollmentOnEventComplete?: ?boolean,
isCompleted?: boolean,
eventId?: ?string,
formFoundation: RenderFoundation,
relatedStageRef: { current?: ?RelatedStageRefPayload },
relatedStageRef?: { current: ?RelatedStageRefPayload },
onSaveAndCompleteEnrollment: (
eventId: string,
dataEntryId: string,
Expand Down Expand Up @@ -60,7 +60,11 @@ const getAskToCompleteEnrollment = (InnerComponent: ComponentType<any>) => (prop
) => {
const { linkMode } = relatedStageRef?.current?.getLinkedStageValues() ?? {};
eventDataToSave.current = { itemId, dataEntryId, formFoundation, saveType };
if (askCompleteEnrollmentOnEventComplete && (isCompleted || saveType === addEventSaveTypes.COMPLETE) && linkMode !== LinkModes.ENTER_DATA) {
if (
askCompleteEnrollmentOnEventComplete &&
(isCompleted || saveType === addEventSaveTypes.COMPLETE) &&
linkMode !== RelatedStageModes.ENTER_DATA
) {
setOpenCompleteModal(true);
} else {
onSave(itemId, dataEntryId, formFoundation, saveType);
Expand Down
Loading
Loading