Skip to content

Commit

Permalink
M2-6109: [FE] [Web App] Header - Update to Include Subject and Respon…
Browse files Browse the repository at this point in the history
…dent (#442)

This PR creates a `MultiInformantBadge` component that is shown during the completion of an activity in a multi-informant context. The component displays itself when the following are true:
- The `enableMultiInformant` feature flag is turned on
- The multi-informant state has been set. This state is set during the validation of multi-informant parameters in the `ValidateTakeNowParams` component.
  • Loading branch information
sultanofcardio authored Apr 30, 2024
1 parent 3b70529 commit d7653cb
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 18 deletions.
10 changes: 8 additions & 2 deletions src/abstract/lib/types/multiInformant.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export type MultiInformantSubject = {
id: string;
secretId: string | null;
nickname: string | null;
};

export type MultiInformantState = {
sourceSubjectId?: string;
targetSubjectId?: string;
sourceSubject?: MultiInformantSubject;
targetSubject?: MultiInformantSubject;
};
2 changes: 1 addition & 1 deletion src/entities/applet/model/hooks/useMultiInformantState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useMultiInformantState = (): Return => {
const getMultiInformantState = useCallback(() => multiInformantState, [multiInformantState]);

const isInMultiInformantFlow = useCallback(() => {
return !!multiInformantState.sourceSubjectId && !!multiInformantState.targetSubjectId;
return !!multiInformantState.sourceSubject?.id && !!multiInformantState.targetSubject?.id;
}, [multiInformantState]);

const initiateTakeNow = useCallback(
Expand Down
6 changes: 2 additions & 4 deletions src/entities/applet/model/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GroupProgress } from '~/abstract/lib';
import { MultiInformantState } from '~/abstract/lib/types/multiInformant';
import {
Answer,
AudioPlayerItem,
Expand Down Expand Up @@ -174,7 +175,4 @@ export type InProgressFlow = {
pipelineActivityOrder: number;
};

export type MultiInformantPayload = {
sourceSubjectId: string;
targetSubjectId: string;
};
export type MultiInformantPayload = Required<MultiInformantState>;
21 changes: 18 additions & 3 deletions src/features/TakeNow/lib/useTakeNowValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,9 @@ export const useTakeNowValidation = ({
return errorState(t('takeNow.invalidSubject'));
}

const { nickname: targetSubjectNickname, secretUserId: targetSecretUserId } =
subjectData.data.result;

if (isActivityError) {
return errorState(t('takeNow.invalidActivity'));
}
Expand Down Expand Up @@ -161,15 +164,27 @@ export const useTakeNowValidation = ({
return errorState(null);
}

const { subjectId: sourceSubjectId } = respondentData.data.result;
const {
subjectId: sourceSubjectId,
nickname: sourceSubjectNickname,
secretUserId: sourceSecretUserId,
} = respondentData.data.result;

return {
isLoading: false,
isError: false,
isSuccess: true,
data: {
sourceSubjectId,
targetSubjectId,
sourceSubject: {
id: sourceSubjectId,
nickname: sourceSubjectNickname,
secretId: sourceSecretUserId,
},
targetSubject: {
id: targetSubjectId,
nickname: targetSubjectNickname,
secretId: targetSecretUserId,
},
},
};
};
37 changes: 37 additions & 0 deletions src/features/TakeNow/ui/MultiInformantBadge.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { useMultiInformantState } from '~/entities/applet/model/hooks';
import { MultiInformantBadgeTile } from '~/features/TakeNow/ui/MultiInformantBadgeTile';
import { Theme } from '~/shared/constants';
import { Box } from '~/shared/ui';
import { useLaunchDarkly } from '~/shared/utils/hooks/useLaunchDarkly';

const borderSize = 1;

export const MultiInformantBadge = () => {
const { getMultiInformantState, isInMultiInformantFlow } = useMultiInformantState();
const { flags: featureFlags } = useLaunchDarkly();

if (!isInMultiInformantFlow() || !featureFlags.enableMultiInformant) {
return null;
}

const { sourceSubject, targetSubject } = getMultiInformantState();

if (!sourceSubject || !targetSubject) {
return null;
}

return (
<Box
display="flex"
padding={`${4 - borderSize}px`}
alignItems="center"
gap="4px"
width="192px"
borderRadius="8px"
border={`${borderSize}px solid ${Theme.colors.light.surfaceVariant}`}
>
<MultiInformantBadgeTile type="Respondent" subject={sourceSubject} />
<MultiInformantBadgeTile type="Subject" subject={targetSubject} />
</Box>
);
};
55 changes: 55 additions & 0 deletions src/features/TakeNow/ui/MultiInformantBadgeTile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import Tooltip from '@mui/material/Tooltip';

import { MultiInformantSubject } from '~/abstract/lib/types/multiInformant';
import { Theme } from '~/shared/constants';
import { Box } from '~/shared/ui';

type MultiInformantBadgeTileProps = {
type: 'Subject' | 'Respondent';
subject: MultiInformantSubject;
};

export const MultiInformantBadgeTile = ({ subject, type }: MultiInformantBadgeTileProps) => {
const { id, secretId, nickname } = subject;
let text = secretId ?? id;
if (nickname) {
text += `, ${nickname}`;
}

const tooltipText = `${type}: ${text}`;

const backgroundColor = type === 'Subject' ? Theme.colors.light.inverseOnSurface : 'transparent';

return (
<Box
display="flex"
padding="8px"
alignItems="center"
gap="8px"
flex="1 0 0"
borderRadius="4px"
width="78px"
height="100%"
sx={{ backgroundColor, cursor: 'default' }}
>
<Tooltip title={tooltipText}>
<p
style={{
overflow: 'hidden',
color: Theme.colors.light.onSurface,
textOverflow: 'ellipsis',

fontSize: '16px',
fontStyle: 'normal',
fontWeight: 400,
lineHeight: '24px',
letterSpacing: '0.5px',
whiteSpace: 'nowrap',
}}
>
{text}
</p>
</Tooltip>
</Box>
);
};
8 changes: 4 additions & 4 deletions src/features/TakeNow/ui/ValidateTakeNowParams.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ function ValidateTakeNowParams({

useEffect(() => {
if (isSuccess && data) {
const { sourceSubjectId, targetSubjectId } = data;
const { sourceSubject, targetSubject } = data;

const multiInformantState = getMultiInformantState();
if (
!isInMultiInformantFlow() ||
sourceSubjectId !== multiInformantState.sourceSubjectId ||
targetSubjectId !== multiInformantState.targetSubjectId
sourceSubject.id !== multiInformantState.sourceSubject?.id ||
targetSubject.id !== multiInformantState.targetSubject?.id
) {
initiateTakeNow({ sourceSubjectId, targetSubjectId });
initiateTakeNow({ sourceSubject, targetSubject });
}
}
}, [data, initiateTakeNow, isInMultiInformantFlow, isSuccess]);
Expand Down
2 changes: 2 additions & 0 deletions src/features/TakeNow/ui/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './ValidateTakeNowParams';
export * from './MultiInformantBadge';
export * from './MultiInformantBadgeTile';
4 changes: 2 additions & 2 deletions src/widgets/ActivityDetails/model/hooks/useAnswers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export const useAnswer = (props: Props) => {
if (featureFlags.enableMultiInformant) {
const multiInformantState = getMultiInformantState();
if (isInMultiInformantFlow()) {
answer.sourceSubjectId = multiInformantState.sourceSubjectId;
answer.targetSubjectId = multiInformantState.targetSubjectId;
answer.sourceSubjectId = multiInformantState.sourceSubject?.id;
answer.targetSubjectId = multiInformantState.targetSubject?.id;
}
}

Expand Down
7 changes: 5 additions & 2 deletions src/widgets/ActivityDetails/ui/AssessmentLayoutHeader.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { SaveAndExitButton } from '~/features/SaveAssessmentAndExit';
import { MultiInformantBadge } from '~/features/TakeNow';
import { ROUTES, Theme } from '~/shared/constants';
import { Box } from '~/shared/ui';
import { BaseProgressBar, Text } from '~/shared/ui';
import { BaseProgressBar, Box, Text } from '~/shared/ui';
import { useCustomMediaQuery, useCustomNavigation } from '~/shared/utils';

type Props = {
Expand Down Expand Up @@ -40,12 +40,15 @@ export const AssessmentLayoutHeader = (props: Props) => {
justifyContent="center"
gridTemplateColumns="1fr minmax(300px, 900px) 1fr"
padding={greaterThanSM ? '19px 24px' : '15px 16px'}
height={87}
gap={1.5}
sx={{
backgroundColor: Theme.colors.light.surface,
borderBottom: `1px solid ${Theme.colors.light.surfaceVariant}`,
}}
>
<MultiInformantBadge />

<Box sx={{ gridColumn: '2 / 3' }}>
<Box
display="flex"
Expand Down

0 comments on commit d7653cb

Please sign in to comment.