From cb14d825b6365eade8568a9e29f693fc2572f882 Mon Sep 17 00:00:00 2001 From: sultanofcardio Date: Wed, 24 Apr 2024 15:41:19 -0500 Subject: [PATCH] M2-6042: Add function to check if we're in a MI flow --- .../applet/model/hooks/useMultiInformantState.ts | 6 ++++++ src/features/TakeNow/ui/ValidateTakeNowParams.tsx | 13 +++---------- src/widgets/ActivityGroups/ui/ActivityGroups.tsx | 9 ++------- 3 files changed, 11 insertions(+), 17 deletions(-) diff --git a/src/entities/applet/model/hooks/useMultiInformantState.ts b/src/entities/applet/model/hooks/useMultiInformantState.ts index fc309e1b4..24b340add 100644 --- a/src/entities/applet/model/hooks/useMultiInformantState.ts +++ b/src/entities/applet/model/hooks/useMultiInformantState.ts @@ -9,6 +9,7 @@ import { useAppDispatch, useAppSelector } from '~/shared/utils'; type Return = { getMultiInformantState: () => MultiInformantState; + isInMultiInformantFlow: () => boolean; initiateTakeNow: (payload: MultiInformantPayload) => void; resetMultiInformantState: () => void; }; @@ -19,6 +20,10 @@ export const useMultiInformantState = (): Return => { const getMultiInformantState = useCallback(() => multiInformantState, [multiInformantState]); + const isInMultiInformantFlow = useCallback(() => { + return !!multiInformantState.sourceSubjectId && !!multiInformantState.targetSubjectId; + }, [multiInformantState]); + const initiateTakeNow = useCallback( (payload: MultiInformantPayload) => { dispatch(actions.initiateTakeNow(payload)); @@ -32,6 +37,7 @@ export const useMultiInformantState = (): Return => { return { getMultiInformantState, + isInMultiInformantFlow, initiateTakeNow, resetMultiInformantState, }; diff --git a/src/features/TakeNow/ui/ValidateTakeNowParams.tsx b/src/features/TakeNow/ui/ValidateTakeNowParams.tsx index b284f7e1c..aa83d1849 100644 --- a/src/features/TakeNow/ui/ValidateTakeNowParams.tsx +++ b/src/features/TakeNow/ui/ValidateTakeNowParams.tsx @@ -12,7 +12,7 @@ function ValidateTakeNowParams({ respondentId, }: TakeNowParams) { const { showErrorNotification } = useNotification(); - const { initiateTakeNow, getMultiInformantState } = appletModel.hooks.useMultiInformantState(); + const { initiateTakeNow, isInMultiInformantFlow } = appletModel.hooks.useMultiInformantState(); const { isError, isLoading, isSuccess, error, data } = useTakeNowValidation({ appletId, @@ -38,17 +38,10 @@ function ValidateTakeNowParams({ } if (isSuccess && data) { - const multiInformantState = getMultiInformantState(); const { sourceSubjectId, targetSubjectId } = data; - if ( - !multiInformantState || - !multiInformantState.sourceSubjectId || - !multiInformantState.targetSubjectId - ) { - if (sourceSubjectId !== targetSubjectId) { - initiateTakeNow({ sourceSubjectId, targetSubjectId }); - } + if (!isInMultiInformantFlow()) { + initiateTakeNow({ sourceSubjectId, targetSubjectId }); } return ( diff --git a/src/widgets/ActivityGroups/ui/ActivityGroups.tsx b/src/widgets/ActivityGroups/ui/ActivityGroups.tsx index d52adf31e..2ebf846ec 100644 --- a/src/widgets/ActivityGroups/ui/ActivityGroups.tsx +++ b/src/widgets/ActivityGroups/ui/ActivityGroups.tsx @@ -41,17 +41,12 @@ export const ActivityGroups = (props: Props) => { const { flags: featureFlags } = useLaunchDarkly(); - const { getMultiInformantState, resetMultiInformantState } = + const { isInMultiInformantFlow, resetMultiInformantState } = appletModel.hooks.useMultiInformantState(); useOnceEffect(() => { if (featureFlags.enableMultiInformant) { - const multiInformantState = getMultiInformantState(); - if ( - multiInformantState && - (multiInformantState.sourceSubjectId || multiInformantState.targetSubjectId) && - !props.startActivityOrFlow - ) { + if (isInMultiInformantFlow() && !props.startActivityOrFlow) { resetMultiInformantState(); } }