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 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 11 commits
Commits
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}
Copy link
Member

Choose a reason for hiding this comment

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

Would it be better to stick with either "action" or "mode" throughout? For this particular one, if we stick with actions: actionsOptions={relatedStageActionsOptions}

(I see that both mode and action are being used in code not introduced by this PR, would be great to fix those as well)

I do like the generic approach you have chosen here though 👍 Nice!

/>
</>
);
};
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';
Copy link
Member

Choose a reason for hiding this comment

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

Can we use the opportunity to move these files to DataEntries/EnrollmentRegistrationEntry/helpers? From what I can see they belong in the DataEntries/EnrollmentRegistrationEntry folder and are only invoked from this file.

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.

};
};

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);
Copy link
Member

Choose a reason for hiding this comment

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

getRelationshipNewTei was dead code I presume? (yes, we need a clean up of relationships)

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