From 508da0510473a599f1803d290001c4c4b904e667 Mon Sep 17 00:00:00 2001 From: Coro Date: Thu, 24 Oct 2024 16:55:01 +0000 Subject: [PATCH 1/4] Added new phrasalTemplate compatible reponse type --- src/abstract/lib/constants.ts | 1 + src/entities/activity/ui/items/ActionPlan/phrasalData.ts | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/abstract/lib/constants.ts b/src/abstract/lib/constants.ts index ae04607f1..d394aabfd 100644 --- a/src/abstract/lib/constants.ts +++ b/src/abstract/lib/constants.ts @@ -32,4 +32,5 @@ export const phrasalTemplateCompatibleResponseTypes = [ 'multiSelectRows', 'singleSelectRows', 'sliderRows', + 'paragraphText', ]; diff --git a/src/entities/activity/ui/items/ActionPlan/phrasalData.ts b/src/entities/activity/ui/items/ActionPlan/phrasalData.ts index 50ba98406..b268ba39a 100644 --- a/src/entities/activity/ui/items/ActionPlan/phrasalData.ts +++ b/src/entities/activity/ui/items/ActionPlan/phrasalData.ts @@ -84,7 +84,8 @@ export const extractActivitiesPhrasalData = (items: ItemRecord[]): ActivitiesPhr } else if ( item.responseType === 'numberSelect' || item.responseType === 'slider' || - item.responseType === 'text' + item.responseType === 'text' || + item.responseType === 'paragraphText' ) { const dateFieldData: ActivityPhrasalArrayFieldData = { type: 'array', From cddd05ed5cf3b385d06b6deb914d2b693031f30c Mon Sep 17 00:00:00 2001 From: Coro Date: Wed, 30 Oct 2024 21:32:35 +0000 Subject: [PATCH 2/4] Enabling breaklines when displaying paragraphs with multiple breaklinesa cross the text --- .../ui/items/ActionPlan/ResponseSegment.tsx | 80 +++++++++++++------ .../ui/items/ActionPlan/phrasalData.ts | 16 +++- 2 files changed, 67 insertions(+), 29 deletions(-) diff --git a/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx b/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx index 38594c018..357726f87 100644 --- a/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx +++ b/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx @@ -65,6 +65,14 @@ export const ResponseSegment = ({ phrasalData, field, isAtStart }: ResponseSegme 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((sentence) => sentence.trim()) // Trim whitespace around each sentence + .filter((sentence) => sentence.length > 0) // Filter out any empty strings + .map(transformValue); // Transform each sentence individually } else if (fieldPhrasalDataType === 'indexed-array') { const indexedAnswers = fieldPhrasalData.values[field.itemIndex] || []; words = isAnswersSkipped(indexedAnswers) @@ -118,32 +126,52 @@ export const ResponseSegment = ({ phrasalData, field, isAtStart }: ResponseSegme return ( - {fieldDisplayMode === 'bullet_list' || - fieldDisplayMode === 'bullet_list_option_row' || - fieldDisplayMode === 'bullet_list_text_row' ? ( - <> - {isAtStart ? null : ( - -   -
-
- )} - - {words.map((item, index) => ( -
  • {item}
  • - ))} -
    - - ) : ( - <> - {isAtStart ? '' : ' '} - {joinSentenceWords(words)} - - )} + {(() => { + if ( + fieldDisplayMode === 'bullet_list' || + fieldDisplayMode === 'bullet_list_option_row' || + fieldDisplayMode === 'bullet_list_text_row' + ) { + return ( + <> + {isAtStart ? null : ( + +   +
    +
    + )} + + {words.map((item, index) => ( +
  • {item}
  • + ))} +
    + + ); + } else if (fieldPhrasalDataType === 'paragraph') { + return ( + <> + {isAtStart ? '' : ' '} + {words.map((item, index) => ( + + {item} + {index < words.length - 1 &&
    }{' '} +
    + ))} + + ); + } else { + return ( + <> + {isAtStart ? '' : ' '} + {joinSentenceWords(words)} + + ); + } + })()}
    ); }; diff --git a/src/entities/activity/ui/items/ActionPlan/phrasalData.ts b/src/entities/activity/ui/items/ActionPlan/phrasalData.ts index b268ba39a..cb8cba5f2 100644 --- a/src/entities/activity/ui/items/ActionPlan/phrasalData.ts +++ b/src/entities/activity/ui/items/ActionPlan/phrasalData.ts @@ -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'; @@ -23,6 +24,8 @@ type ActivityPhrasalBaseData< type ActivityPhrasalArrayFieldData = ActivityPhrasalBaseData<'array', string[]>; +type ActivityPhrasalParagraphFieldData = ActivityPhrasalBaseData<'paragraph', string[]>; + type ActivityPhrasalItemizedArrayValue = Record; type ActivityPhrasalIndexedArrayFieldData = ActivityPhrasalBaseData< @@ -45,7 +48,8 @@ type ActivityPhrasalMatrixFieldData = ActivityPhrasalBaseData<'matrix', Activity type ActivityPhrasalData = | ActivityPhrasalArrayFieldData | ActivityPhrasalIndexedArrayFieldData - | ActivityPhrasalMatrixFieldData; + | ActivityPhrasalMatrixFieldData + | ActivityPhrasalParagraphFieldData; export type ActivitiesPhrasalData = Record; @@ -84,8 +88,7 @@ export const extractActivitiesPhrasalData = (items: ItemRecord[]): ActivitiesPhr } else if ( item.responseType === 'numberSelect' || item.responseType === 'slider' || - item.responseType === 'text' || - item.responseType === 'paragraphText' + item.responseType === 'text' ) { const dateFieldData: ActivityPhrasalArrayFieldData = { type: 'array', @@ -93,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', From 585ccaecebef904b8309d4583739983ac4859f72 Mon Sep 17 00:00:00 2001 From: Coro Date: Mon, 4 Nov 2024 17:42:13 +0000 Subject: [PATCH 3/4] Reverting changes and addng conditionto redefine joinSentenceWords for paragraphText --- .../ui/items/ActionPlan/ResponseSegment.tsx | 83 +++++++------------ 1 file changed, 32 insertions(+), 51 deletions(-) diff --git a/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx b/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx index 357726f87..91c456aa5 100644 --- a/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx +++ b/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx @@ -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 = { @@ -58,6 +58,9 @@ 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) =>
    {item || ' '}
    ); } let words: string[]; @@ -69,10 +72,8 @@ export const ResponseSegment = ({ phrasalData, field, isAtStart }: ResponseSegme words = isAnswersSkipped(fieldPhrasalData.values) ? [t('questionSkipped')] : fieldPhrasalData.values - .flatMap((value) => value.split(/\r?\n+/)) // Split each paragraph by newlines - .map((sentence) => sentence.trim()) // Trim whitespace around each sentence - .filter((sentence) => sentence.length > 0) // Filter out any empty strings - .map(transformValue); // Transform each sentence individually + .flatMap((value) => value.split(/\r?\n+/)) + .map((sentence) => sentence.trim()); } else if (fieldPhrasalDataType === 'indexed-array') { const indexedAnswers = fieldPhrasalData.values[field.itemIndex] || []; words = isAnswersSkipped(indexedAnswers) @@ -126,52 +127,32 @@ export const ResponseSegment = ({ phrasalData, field, isAtStart }: ResponseSegme return ( - {(() => { - if ( - fieldDisplayMode === 'bullet_list' || - fieldDisplayMode === 'bullet_list_option_row' || - fieldDisplayMode === 'bullet_list_text_row' - ) { - return ( - <> - {isAtStart ? null : ( - -   -
    -
    - )} - - {words.map((item, index) => ( -
  • {item}
  • - ))} -
    - - ); - } else if (fieldPhrasalDataType === 'paragraph') { - return ( - <> - {isAtStart ? '' : ' '} - {words.map((item, index) => ( - - {item} - {index < words.length - 1 &&
    }{' '} -
    - ))} - - ); - } else { - return ( - <> - {isAtStart ? '' : ' '} - {joinSentenceWords(words)} - - ); - } - })()} + {fieldDisplayMode === 'bullet_list' || + fieldDisplayMode === 'bullet_list_option_row' || + fieldDisplayMode === 'bullet_list_text_row' ? ( + <> + {isAtStart ? null : ( + +   +
    +
    + )} + + {words.map((item, index) => ( +
  • {item}
  • + ))} +
    + + ) : ( + <> + {isAtStart ? '' : ' '} + {joinSentenceWords(words)} + + )}
    ); }; From 29905d0aeedd857eeb47f655bb3cba9fc7f043b7 Mon Sep 17 00:00:00 2001 From: Coro Date: Mon, 4 Nov 2024 18:54:04 +0000 Subject: [PATCH 4/4] Fixing joinSentenceWords for itemResponseType paragraph --- src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx b/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx index 91c456aa5..c0ed1e306 100644 --- a/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx +++ b/src/entities/activity/ui/items/ActionPlan/ResponseSegment.tsx @@ -72,8 +72,8 @@ export const ResponseSegment = ({ phrasalData, field, isAtStart }: ResponseSegme words = isAnswersSkipped(fieldPhrasalData.values) ? [t('questionSkipped')] : fieldPhrasalData.values - .flatMap((value) => value.split(/\r?\n+/)) - .map((sentence) => sentence.trim()); + .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)