-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Allow "Take Now" to start when respondent is not explicitly assi…
…gned (M2-7796) (#536) * Update `useStartSurvey` hook to make `applet` optional. - This allows unconditional usage of the hook in higher-level components where `applet` is not guaranteed. - If the properties used by the applet are not available (ie, the applet is not provided), then the hook will simply not navigate when `startSurvey` is called. * Remove existing "Take Now redirect" behaviour in ActivityCard. - These cards may not be rendered if not explicitly assigned. This prevented "Take Now" from reliably starting. * Add `useTakeNowRedirect` hook. - This hook accepts a number of parameters required to initialize a "Take Now" activity/flow. - It will return without taking action when called with insufficiently defined arguments. - It will call `startSurvey`, thereby redirecting the use to the appropriate activity/flow when called with appropriate arguments. * Add `useTakeNowRedirect` hook to `ActivityGroups` component. * Address QA findings. - Avoid displaying multi-informant/target subject banner UI.
- Loading branch information
1 parent
cfac41b
commit 16dedf3
Showing
4 changed files
with
107 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
80 changes: 80 additions & 0 deletions
80
src/widgets/ActivityGroups/model/hooks/useTakeNowRedirect.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { useMemo } from 'react'; | ||
|
||
import { openStoreLink } from '~/abstract/lib'; | ||
import { isSupportedActivity } from '~/entities/activity'; | ||
import { useStartSurvey } from '~/features/PassSurvey'; | ||
import { AppletBaseDTO, AppletEventsResponse, HydratedAssignmentDTO } from '~/shared/api'; | ||
|
||
export function useTakeNowRedirect({ | ||
activityOrFlowId, | ||
applet, | ||
assignments = [], | ||
events = [], | ||
isPublic = false, | ||
publicAppletKey = null, | ||
}: { | ||
activityOrFlowId?: string | null; | ||
applet?: AppletBaseDTO; | ||
assignments?: HydratedAssignmentDTO[] | null; | ||
events?: AppletEventsResponse['events']; | ||
isPublic?: boolean; | ||
publicAppletKey?: string | null; | ||
}) { | ||
const { startSurvey } = useStartSurvey({ applet, isPublic, publicAppletKey }); | ||
|
||
const handleTakeNowRedirect = useMemo( | ||
() => | ||
({ | ||
activityId, | ||
eventId, | ||
flowId = null, | ||
isSupported = true, | ||
}: { | ||
activityId: string; | ||
eventId: string; | ||
flowId?: string | null; | ||
isSupported?: boolean; | ||
}) => { | ||
if (!isSupported) { | ||
openStoreLink(); | ||
return; | ||
} | ||
|
||
startSurvey({ activityId, eventId, flowId, shouldRestart: true, targetSubjectId: null }); | ||
}, | ||
[startSurvey], | ||
); | ||
|
||
if (assignments && applet && events && activityOrFlowId) { | ||
const activity = applet.activities.find(({ id }) => id === activityOrFlowId); | ||
const flow = applet.activityFlows.find(({ id }) => id === activityOrFlowId); | ||
const event = events.find(({ entityId }) => entityId === (activity?.id || flow?.id)); | ||
|
||
if (!event) { | ||
return; | ||
} | ||
|
||
if (activity) { | ||
return handleTakeNowRedirect({ | ||
activityId: activity.id, | ||
isSupported: isSupportedActivity(activity.containsResponseTypes), | ||
eventId: event.id, | ||
}); | ||
} | ||
|
||
if (flow) { | ||
const flowActivities = flow.activityIds.map((activityId) => | ||
applet.activities.find(({ id }) => activityId === id), | ||
); | ||
|
||
return handleTakeNowRedirect({ | ||
activityId: flow.activityIds[0], | ||
isSupported: flowActivities.every( | ||
(activity) => !!activity && isSupportedActivity(activity.containsResponseTypes), | ||
), | ||
eventId: event.id, | ||
flowId: flow.id, | ||
}); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters