Skip to content

Commit

Permalink
Merge pull request #1973 from ChildMindInstitute/release/1.44.0
Browse files Browse the repository at this point in the history
Release/1.44.0 [dev]
  • Loading branch information
mbanting authored Nov 7, 2024
2 parents 0dc4674 + 1587872 commit 5f0ab15
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ItemResponseType } from 'shared/consts';
export const ItemTypesToPrint = [
ItemResponseType.SingleSelection,
ItemResponseType.MultipleSelection,
ItemResponseType.NumberSelection,
ItemResponseType.Slider,
ItemResponseType.Text,
ItemResponseType.ParagraphText,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ export const getTableScoreItems = (items?: ItemFormValues[]) =>
},
];
}, []);

export const reportIsScore = (report: ScoreOrSection): report is ScoreReport =>
report.type === ScoreReportType.Score;

export const reportIsSection = (report: ScoreOrSection): report is SectionReport =>
report.type === ScoreReportType.Section;
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getEntityKey, isSystemItem } from 'shared/utils';
import { ActivitySettingsSubscale, AgeFieldType, ScoreOrSection, ScoreReport } from 'shared/state';
import { LookupTableItems } from 'shared/consts';
import { REACT_HOOK_FORM_KEY_NAME } from 'modules/Builder/consts';
import { reportIsScore } from 'modules/Builder/features/ActivitySettings/ScoresAndReports/ScoresAndReports.utils';

import { ageDropdownItem, ageTextItem, genderItem } from './SubscalesConfiguration.const';

Expand All @@ -19,18 +20,105 @@ export const useSubscalesSystemItemsSetup = (
const itemsFieldName = `${activityFieldName}.items`;
const items: ItemFormValues[] = watch(itemsFieldName) ?? [];

const { control: appletFormControl } = useFormContext();
const reportsField = `${activityFieldName}.scoresAndReports.reports`;
const { fields: scoreOrSectionArray, update: updateReport } = useFieldArray<
Record<string, ScoreOrSection[]>,
string,
typeof REACT_HOOK_FORM_KEY_NAME
>({
control: appletFormControl,
name: reportsField,
keyName: REACT_HOOK_FORM_KEY_NAME,
});

const appendSystemItems = (newItems: ItemFormValues[]) =>
setValue(itemsFieldName, [...items, ...newItems]);
const removeSystemItems = () =>

const updateReports = (
mapItemsPrint: (itemsPrint: string[] | undefined) => string[] | undefined,
) => {
scoreOrSectionArray.forEach((report, i) => {
const updatedReport = {
...report,
itemsPrint: mapItemsPrint(report.itemsPrint),
};

if (reportIsScore(updatedReport)) {
updatedReport.conditionalLogic = updatedReport?.conditionalLogic?.map((condition) => ({
...condition,
itemsPrint: mapItemsPrint(condition.itemsPrint),
}));
}

updateReport(i, updatedReport);
});
};

const removeSystemItems = () => {
const systemItemKeys: string[] = [];

setValue(
itemsFieldName,
items.filter((item) => !isSystemItem(item)),
items.filter((item) => {
if (isSystemItem(item)) {
systemItemKeys.push(getEntityKey(item));

return false;
}

return true;
}),
);

// Remove system items from report scores that include them
updateReports(
(itemsPrint: string[] | undefined) =>
itemsPrint?.filter((item) => !systemItemKeys.includes(item)),
);
};

const replaceSystemItems = (newSystemItems: ItemFormValues[]) => {
const oldSystemItems: { key: string; name: string }[] = [];

setValue(
itemsFieldName,
[
...items.filter((item) => {
if (isSystemItem(item)) {
oldSystemItems.push({ key: getEntityKey(item), name: item.name });

return false;
}

return true;
}),
...newSystemItems,
],
{
shouldDirty: true,
},
);

// Replace system items in report scores that include them
updateReports(
(itemsPrint: string[] | undefined) =>
itemsPrint
?.map((item) => {
const oldSystemItem = oldSystemItems.find((old) => old.key === item);

if (oldSystemItem) {
const newItem = newSystemItems.find((newItem) => newItem.name === oldSystemItem.name);

return newItem ? getEntityKey(newItem) : null;
}

return item;
})
.filter((it): it is string => it !== null),
);
const replaceSystemItems = (newItems: ItemFormValues[]) => {
setValue(itemsFieldName, [...items.filter((item) => !isSystemItem(item)), ...newItems], {
shouldDirty: true,
});
};

useEffect(() => {
const hasSubscaleLookupTable = subscales?.some((subscale) => !!subscale.subscaleTableData);
const hasSystemItems = items?.some((item) => isSystemItem(item));
Expand Down Expand Up @@ -128,17 +216,19 @@ export const useLinkedScoreReports = (): UseLinkedScoreReportsReturn => {
*/
const removeReportScoreLink = useCallback(
(subscale: ActivitySettingsSubscale<string>) => {
linkedScores.forEach((scoreReport, index) => {
if (scoreReport.subscaleName === subscale.name) {
scoreOrSectionArray.forEach((report, index) => {
if (!reportIsScore(report) || !linkedScores.includes(report)) return;

if (report.subscaleName === subscale.name) {
const updatedReport: ScoreReport = {
...scoreReport,
...report,
subscaleName: '',
};
updateReport(index, updatedReport);
}
});
},
[linkedScores, updateReport],
[linkedScores, updateReport, scoreOrSectionArray],
);

const updateSubscaleNameInReports = useCallback(
Expand All @@ -148,34 +238,38 @@ export const useLinkedScoreReports = (): UseLinkedScoreReportsReturn => {
);
if (!isEligibleSubscale) return;

linkedScores.forEach((scoreReport, index) => {
if (scoreReport.subscaleName === oldSubscaleName) {
scoreOrSectionArray.forEach((report, index) => {
if (!reportIsScore(report) || !linkedScores.includes(report)) return;

if (report.subscaleName === oldSubscaleName) {
const updatedReport: ScoreReport = {
...scoreReport,
...report,
subscaleName: newSubscaleName,
};
updateReport(index, updatedReport);
}
});
},
[eligibleSubscales, linkedScores, updateReport],
[eligibleSubscales, linkedScores, updateReport, scoreOrSectionArray],
);

useEffect(() => {
if (eligibleSubscales.length === 0) {
// If there are no more eligible subscales, then these linked scores should be reset to
// 'raw_score' instead of 'score'. Otherwise, the admin will see an error reported in the UI
// but there will be nothing to fix when they go back to the reports screen
linkedScores.forEach((scoreReport, index) => {
scoreOrSectionArray.forEach((report, index) => {
if (!reportIsScore(report) || !linkedScores.includes(report)) return;

const updatedReport: ScoreReport = {
...scoreReport,
...report,
subscaleName: '',
scoringType: 'raw_score',
};
updateReport(index, updatedReport);
});
}
}, [eligibleSubscales, linkedScores, updateReport]);
}, [eligibleSubscales, linkedScores, updateReport, scoreOrSectionArray]);

return { removeReportScoreLink, updateSubscaleNameInReports, hasNonSubscaleItems };
};
2 changes: 1 addition & 1 deletion src/shared/utils/exportData/getSubscales.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export const calcScores = <T>(
if (ageAnswer) {
if (typeof ageAnswer === 'string') {
reportedAge = ageAnswer;
} else if ('value' in ageAnswer && typeof ageAnswer.value === 'number') {
} else if ('value' in ageAnswer && ['number', 'string'].includes(typeof ageAnswer.value)) {
reportedAge = String(ageAnswer.value);
}
}
Expand Down

0 comments on commit 5f0ab15

Please sign in to comment.