Skip to content

Commit

Permalink
fix: remove duplicate current edited event
Browse files Browse the repository at this point in the history
  • Loading branch information
simonadomnisoru committed Feb 29, 2024
1 parent c75205f commit 48bf7f9
Show file tree
Hide file tree
Showing 9 changed files with 31 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { Props } from './completeModal.types';

export const CompleteModal = ({
programId,
eventId,
enrollment,
events,
onCancel,
Expand Down Expand Up @@ -58,7 +59,8 @@ export const CompleteModal = ({
const updatedAt = moment(nowServer).format('YYYY-MM-DDTHH:mm:ss');
const eventsToComplete = events.reduce((acc, event) => {
const { access } = programStages.find(p => p.id === event.programStage) || {};
if (event.status === eventStatuses.ACTIVE && access.data.write) {
const isCurrentEvent = eventId && event.event === eventId;
if (event.status === eventStatuses.ACTIVE && access.data.write && !isCurrentEvent) {
return [...acc, { ...event, status: eventStatuses.COMPLETED, updatedAt }];
}
return acc;
Expand All @@ -70,7 +72,7 @@ export const CompleteModal = ({
events: eventsToComplete,
};
onCompleteEnrollment(enrollmentWithCompletedEvents);
}, [events, programStages, enrollment, onCompleteEnrollment, fromClientDate]);
}, [events, programStages, enrollment, onCompleteEnrollment, fromClientDate, eventId]);

return hasActiveEvents ? (
<CompleteEnrollmentAndEventsModalComponent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export type Props = {|
programId: string,
eventId?: ?string,
enrollment: ApiEnrollment,
events: Array<ApiEnrollmentEvent>,
hasActiveEvents: boolean,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type Props = {
onSave: (eventId: string, dataEntryId: string, formFoundation: RenderFoundation, saveType?: ?string) => void,
askCompleteEnrollmentOnEventComplete?: ?boolean,
isCompleted?: boolean,
eventId?: ?string,
formFoundation: RenderFoundation,
onSaveAndCompleteEnrollment: (
eventId: string,
Expand All @@ -21,8 +22,14 @@ type Props = {
};

const getAskToCompleteEnrollment = (InnerComponent: ComponentType<any>) => (props: Props) => {
const { askCompleteEnrollmentOnEventComplete, onSave, isCompleted, onSaveAndCompleteEnrollment, ...passOnProps } =
props;
const {
askCompleteEnrollmentOnEventComplete,
onSave,
isCompleted,
onSaveAndCompleteEnrollment,
eventId,
...passOnProps
} = props;
const enrollment = useSelector(({ enrollmentDomain }) => enrollmentDomain?.enrollment);
const events = enrollment.events;
const hasActiveEvents = useMemo(() => events.some(event => event.status === eventStatuses.ACTIVE), [events]);
Expand All @@ -31,27 +38,27 @@ const getAskToCompleteEnrollment = (InnerComponent: ComponentType<any>) => (prop

const handleOnSaveEvent = () => {
setOpenCompleteModal(false);
const { eventId, dataEntryId, formFoundation, saveType } = eventDataToSave.current;
onSave(eventId, dataEntryId, formFoundation, saveType);
const { itemId, dataEntryId, formFoundation, saveType } = eventDataToSave.current;
onSave(itemId, dataEntryId, formFoundation, saveType);
};

const handleCompleteEnrollment = (updatedEnrollment) => {
setOpenCompleteModal(false);
const { eventId, dataEntryId, formFoundation } = eventDataToSave.current;
onSaveAndCompleteEnrollment(eventId, dataEntryId, formFoundation, updatedEnrollment);
const { itemId, dataEntryId, formFoundation } = eventDataToSave.current;
onSaveAndCompleteEnrollment(itemId, dataEntryId, formFoundation, updatedEnrollment);
};

const handleOnSave = (
eventId: string,
itemId: string,
dataEntryId: string,
formFoundation: RenderFoundation,
saveType?: string,
) => {
eventDataToSave.current = { eventId, dataEntryId, formFoundation, saveType };
eventDataToSave.current = { itemId, dataEntryId, formFoundation, saveType };
if (askCompleteEnrollmentOnEventComplete && (isCompleted || saveType === addEventSaveTypes.COMPLETE)) {
setOpenCompleteModal(true);
} else {
onSave(eventId, dataEntryId, formFoundation, saveType);
onSave(itemId, dataEntryId, formFoundation, saveType);
}
};

Expand All @@ -66,6 +73,7 @@ const getAskToCompleteEnrollment = (InnerComponent: ComponentType<any>) => (prop
{isOpenCompleteModal && (
<CompleteModal
programId={enrollment?.program}
eventId={eventId}
onCancel={handleOnSaveEvent}
onCompleteEnrollment={handleCompleteEnrollment}
enrollment={enrollment}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const EnrollmentEditEventPageComponent = ({
programStage,
teiId,
enrollmentId,
eventId,
trackedEntityTypeId,
program,
enrollmentsAsOptions,
Expand Down Expand Up @@ -74,6 +75,7 @@ export const EnrollmentEditEventPageComponent = ({
orgUnitId={orgUnitId}
teiId={teiId}
enrollmentId={enrollmentId}
eventId={eventId}
eventStatus={eventStatus}
initialScheduleDate={scheduleDate}
onCancelEditEvent={onCancelEditEvent}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,7 @@ const EnrollmentEditEventPageWithContextPlain = ({
hideWidgets={hideWidgets}
teiId={teiId}
enrollmentId={enrollmentId}
eventId={eventId}
trackedEntityTypeId={trackedEntityTypeId}
enrollmentsAsOptions={enrollmentsAsOptions}
teiDisplayName={teiDisplayName}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export type PlainProps = {|
hideWidgets: HideWidgets,
teiId: string,
enrollmentId: string,
eventId: string,
program: Program,
trackedEntityTypeId: string,
mode: string,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export const EditEventWorkspace: WidgetConfig = {
orgUnitId,
teiId,
enrollmentId,
eventId,
eventStatus,
onCancelEditEvent,
onHandleScheduleSave,
Expand All @@ -217,6 +218,7 @@ export const EditEventWorkspace: WidgetConfig = {
orgUnitId,
teiId,
enrollmentId,
eventId,
eventStatus,
onCancelEditEvent,
onHandleScheduleSave,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export const WidgetEventEditPlain = ({
programId,
orgUnitId,
enrollmentId,
eventId,
teiId,
assignee,
onSaveAndCompleteEnrollment,
Expand Down Expand Up @@ -140,6 +141,7 @@ export const WidgetEventEditPlain = ({
stageId={programStage.id}
teiId={teiId}
enrollmentId={enrollmentId}
eventId={eventId}
eventStatus={eventStatus}
onCancelEditEvent={onCancelEditEvent}
hasDeleteButton
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type Props = {|
orgUnitId: string,
programId: string,
enrollmentId: string,
eventId: string,
teiId: string,
initialScheduleDate?: string,
assignee?: UserFormField | null,
Expand Down

0 comments on commit 48bf7f9

Please sign in to comment.