Skip to content

Commit

Permalink
feat: pre select
Browse files Browse the repository at this point in the history
  • Loading branch information
henrikmv committed Sep 10, 2024
1 parent 325c638 commit 381bce4
Showing 1 changed file with 27 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// @flow
import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useState } from 'react';
import { useDataQuery } from '@dhis2/app-runtime';
import { useRelatedStages } from './useRelatedStages';
import type { Props, RelatedStageDataValueStates } from './WidgetRelatedStages.types';
import { RelatedStagesActions } from './RelatedStagesActions';
Expand Down Expand Up @@ -28,6 +29,16 @@ const WidgetRelatedStagesPlain = ({
occurredLabel,
enrollmentId,
});

const { loading: orgUnitLoading, data: orgUnitData } = useDataQuery({
orgUnits: {
resource: 'me',
params: {
fields: ['organisationUnits[id,path,displayName,children::isNotEmpty]'],
},
},
});

const [saveAttempted, setSaveAttempted] = useState(false);
const [errorMessages, setErrorMessages] = useState({});
const [relatedStageDataValues, setRelatedStageDataValues] = useState<RelatedStageDataValueStates>({
Expand All @@ -37,6 +48,17 @@ const WidgetRelatedStagesPlain = ({
linkedEventId: undefined,
});

useEffect(() => {
const orgUnits = orgUnitData?.orgUnits?.organisationUnits || [];
if (orgUnits.length === 1 && !orgUnits[0]?.children) {
const { displayName, ...rest } = orgUnits[0];
setRelatedStageDataValues(prev => ({
...prev,
orgUnit: { ...rest, name: displayName },
}));
}
}, [orgUnitData]);

const addErrorMessage = (message: ErrorMessagesForRelatedStages) => {
setErrorMessages((prevMessages: Object) => ({
...prevMessages,
Expand Down Expand Up @@ -81,7 +103,7 @@ const WidgetRelatedStagesPlain = ({
}
}, [formIsValid, relatedStageDataValues]);

if (!currentRelatedStagesStatus || !selectedRelationshipType || isLoadingEvents) {
if (!currentRelatedStagesStatus || !selectedRelationshipType || isLoadingEvents || orgUnitLoading) {
return null;
}

Expand All @@ -103,8 +125,8 @@ const WidgetRelatedStagesPlain = ({
);
};

export const WidgetRelatedStages = forwardRef<Props, {|
export const WidgetRelatedStages = forwardRef < Props, {|
eventHasLinkableStageRelationship: Function,
formIsValidOnSave: Function,
getLinkedStageValues: Function
|}>(WidgetRelatedStagesPlain);
formIsValidOnSave: Function,
getLinkedStageValues: Function
|}>(WidgetRelatedStagesPlain);

0 comments on commit 381bce4

Please sign in to comment.