Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: paragraph ItemResponseType for phrase builder response options WEB (M2-7762) #542

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/abstract/lib/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,5 @@ export const phrasalTemplateCompatibleResponseTypes = [
'multiSelectRows',
'singleSelectRows',
'sliderRows',
'paragraphText',
];
11 changes: 10 additions & 1 deletion src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const isAnswersSkipped = (answers: string[]): boolean => {
type FieldValueTransformer = (value: string) => string;
const identity: FieldValueTransformer = (value) => value;

type FieldValuesJoiner = (values: string[]) => string;
type FieldValuesJoiner = (values: string[]) => string | JSX.Element[];
const joinWithComma: FieldValuesJoiner = (values) => values.join(', ');

type ResponseSegmentProps = {
Expand Down Expand Up @@ -58,13 +58,22 @@ export const ResponseSegment = ({ phrasalData, field, isAtStart }: ResponseSegme
};
} else if (fieldPhrasalData.context.itemResponseType === 'timeRange') {
joinSentenceWords = (values) => values.join(' - ');
} else if (fieldPhrasalData.context.itemResponseType === 'paragraphText') {
joinSentenceWords = (values) =>
values.map((item, index) => <div key={index}>{item || ' '}</div>);
}

let words: string[];
if (fieldPhrasalDataType === 'array') {
words = isAnswersSkipped(fieldPhrasalData.values)
? [t('questionSkipped')]
: fieldPhrasalData.values.map(transformValue);
} else if (fieldPhrasalDataType === 'paragraph') {
words = isAnswersSkipped(fieldPhrasalData.values)
? [t('questionSkipped')]
: fieldPhrasalData.values
.flatMap((value) => value.split(/\r?\n/)) // Split each paragraph by newlines
.map(transformValue);
} else if (fieldPhrasalDataType === 'indexed-array') {
const indexedAnswers = fieldPhrasalData.values[field.itemIndex] || [];
words = isAnswersSkipped(indexedAnswers)
Expand Down
13 changes: 12 additions & 1 deletion src/entities/activity/ui/items/ActionPlan/phrasalData.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable prettier/prettier */
import { phrasalTemplateCompatibleResponseTypes } from '~/abstract/lib/constants';
import { ActivityItemType } from '~/entities/activity/lib';
import { ItemRecord } from '~/entities/applet/model';
Expand All @@ -23,6 +24,8 @@ type ActivityPhrasalBaseData<

type ActivityPhrasalArrayFieldData = ActivityPhrasalBaseData<'array', string[]>;

type ActivityPhrasalParagraphFieldData = ActivityPhrasalBaseData<'paragraph', string[]>;

type ActivityPhrasalItemizedArrayValue = Record<number, string[]>;

type ActivityPhrasalIndexedArrayFieldData = ActivityPhrasalBaseData<
Expand All @@ -45,7 +48,8 @@ type ActivityPhrasalMatrixFieldData = ActivityPhrasalBaseData<'matrix', Activity
type ActivityPhrasalData =
| ActivityPhrasalArrayFieldData
| ActivityPhrasalIndexedArrayFieldData
| ActivityPhrasalMatrixFieldData;
| ActivityPhrasalMatrixFieldData
| ActivityPhrasalParagraphFieldData;

export type ActivitiesPhrasalData = Record<string, ActivityPhrasalData>;

Expand Down Expand Up @@ -92,6 +96,13 @@ export const extractActivitiesPhrasalData = (items: ItemRecord[]): ActivitiesPhr
context: fieldDataContext,
};
fieldData = dateFieldData;
} else if (item.responseType === 'paragraphText') {
const dateFieldData: ActivityPhrasalParagraphFieldData = {
type: 'paragraph',
values: item.answer.map((value) => value || ''),
context: fieldDataContext,
};
fieldData = dateFieldData;
} else if (item.responseType === 'singleSelect' || item.responseType === 'multiSelect') {
const dateFieldData: ActivityPhrasalArrayFieldData = {
type: 'array',
Expand Down
Loading