Skip to content

Commit

Permalink
M2-6042: Add function to check if we're in a MI flow
Browse files Browse the repository at this point in the history
  • Loading branch information
sultanofcardio committed Apr 24, 2024
1 parent 1e65a82 commit cb14d82
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 17 deletions.
6 changes: 6 additions & 0 deletions src/entities/applet/model/hooks/useMultiInformantState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useAppDispatch, useAppSelector } from '~/shared/utils';

type Return = {
getMultiInformantState: () => MultiInformantState;
isInMultiInformantFlow: () => boolean;
initiateTakeNow: (payload: MultiInformantPayload) => void;
resetMultiInformantState: () => void;
};
Expand All @@ -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));
Expand All @@ -32,6 +37,7 @@ export const useMultiInformantState = (): Return => {

return {
getMultiInformantState,
isInMultiInformantFlow,
initiateTakeNow,
resetMultiInformantState,
};
Expand Down
13 changes: 3 additions & 10 deletions src/features/TakeNow/ui/ValidateTakeNowParams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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 (
Expand Down
9 changes: 2 additions & 7 deletions src/widgets/ActivityGroups/ui/ActivityGroups.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}
Expand Down

0 comments on commit cb14d82

Please sign in to comment.