Skip to content

Commit

Permalink
Add function to remove repeat IDs, fix an issue where repeating group…
Browse files Browse the repository at this point in the history
…s will refresh instances due to the mismatch of internal IDs
  • Loading branch information
fongsean committed Sep 20, 2024
1 parent 1195ee0 commit 893983c
Show file tree
Hide file tree
Showing 21 changed files with 528 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function RepeatGroup(props: RepeatGroupProps) {

const mutateRepeatEnableWhenItems = useQuestionnaireStore.use.mutateRepeatEnableWhenItems();

const initialRepeatGroups = useInitialiseRepeatGroups(qItem, qrItems);
const initialRepeatGroups = useInitialiseRepeatGroups(qItem.linkId, qrItems);

const [repeatGroups, setRepeatGroups] = useRepeatGroups(initialRepeatGroups);

Expand Down Expand Up @@ -104,7 +104,7 @@ function RepeatGroup(props: RepeatGroupProps) {
setRepeatGroups([
...repeatGroups,
{
nanoId: generateNewRepeatId(qItem.linkId),
id: generateNewRepeatId(qItem.linkId),
qrItem: null
}
]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,15 +76,15 @@ function RepeatGroupView(props: RepeatGroupViewProps) {
return (
<QGroupContainerBox key={qItem.linkId} cardElevation={groupCardElevation} isRepeated={true}>
<Card elevation={groupCardElevation} sx={{ p: 2 }}>
{repeatGroups.map(({ nanoId, qrItem: nullableQrItem }, index) => {
{repeatGroups.map(({ id, qrItem: nullableQrItem }, index) => {
const answeredQrItem = createEmptyQrItem(qItem, undefined);
if (nullableQrItem) {
answeredQrItem.item = nullableQrItem.item;
}

return (
<RepeatGroupItem
key={nanoId}
key={id}
qItem={qItem}
repeatGroupIndex={index}
answeredQrItem={answeredQrItem}
Expand Down Expand Up @@ -127,14 +127,14 @@ function RepeatGroupView(props: RepeatGroupViewProps) {
<AccordionDetails sx={{ pt: 0 }}>
{qItem.text ? <Divider sx={{ mb: 1.5 }} light /> : null}
<TransitionGroup>
{repeatGroups.map(({ nanoId, qrItem: nullableQrItem }, index) => {
{repeatGroups.map(({ id, qrItem: nullableQrItem }, index) => {
const answeredQrItem = createEmptyQrItem(qItem, undefined);
if (nullableQrItem) {
answeredQrItem.item = nullableQrItem.item;
}

return (
<Collapse key={nanoId} timeout={200}>
<Collapse key={id} timeout={200}>
<RepeatGroupItem
qItem={qItem}
repeatGroupIndex={index}
Expand Down Expand Up @@ -169,14 +169,14 @@ function RepeatGroupView(props: RepeatGroupViewProps) {
</>
) : null}
<TransitionGroup>
{repeatGroups.map(({ nanoId, qrItem: nullableQrItem }, index) => {
{repeatGroups.map(({ id, qrItem: nullableQrItem }, index) => {
const answeredQrItem = createEmptyQrItem(qItem, undefined);
if (nullableQrItem) {
answeredQrItem.item = nullableQrItem.item;
}

return (
<Collapse key={nanoId} timeout={200}>
<Collapse key={id} timeout={200}>
<RepeatGroupItem
qItem={qItem}
repeatGroupIndex={index}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ function GroupTable(props: GroupTableProps) {
const updatedTableRows = [...tableRows];

const rowToRemove = updatedTableRows[index];
const updatedSelectedIds = selectedIds.filter((id) => id !== rowToRemove.nanoId);
const updatedSelectedIds = selectedIds.filter((id) => id !== rowToRemove.id);

updatedTableRows.splice(index, 1);

Expand All @@ -117,34 +117,34 @@ function GroupTable(props: GroupTableProps) {
}

function handleAddRow() {
const newRowNanoId = generateNewRepeatId(qItem.linkId);
const newRowId = generateNewRepeatId(qItem.linkId);
setTableRows([
...tableRows,
{
nanoId: newRowNanoId,
id: newRowId,
qrItem: null
}
]);
setSelectedIds([...selectedIds, newRowNanoId]);
setSelectedIds([...selectedIds, newRowId]);
}

function handleSelectAll() {
// deselect all if all are selected, otherwise select all
const updatedTableIds =
selectedIds.length === tableRows.length ? [] : tableRows.map((tableRow) => tableRow.nanoId);
selectedIds.length === tableRows.length ? [] : tableRows.map((tableRow) => tableRow.id);
setSelectedIds(updatedTableIds);
onQrRepeatGroupChange({
linkId: qItem.linkId,
qrItems: getGroupTableItemsToUpdate(tableRows, updatedTableIds)
});
}

function handleSelectRow(nanoId: string) {
function handleSelectRow(rowId: string) {
const updatedSelectedIds = [...selectedIds];

const index = updatedSelectedIds.indexOf(nanoId);
const index = updatedSelectedIds.indexOf(rowId);
if (index === -1) {
updatedSelectedIds.push(nanoId);
updatedSelectedIds.push(rowId);
} else {
updatedSelectedIds.splice(index, 1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ interface GroupTableBodyProps
qItemsIndexMap: Record<string, number>;
onRowChange: (newQrRow: QuestionnaireResponseItem, index: number) => void;
onRemoveRow: (index: number) => void;
onSelectRow: (nanoId: string) => void;
onSelectRow: (rowId: string) => void;
onReorderRows: (newTableRows: GroupTableRowModel[]) => void;
}

Expand Down Expand Up @@ -80,18 +80,18 @@ function GroupTableBody(props: GroupTableBodyProps) {
<Droppable droppableId="gtable_rows" direction="vertical">
{(droppableProvided, snapshot) => (
<TableBody ref={droppableProvided.innerRef} {...droppableProvided.droppableProps}>
{tableRows.map(({ nanoId, qrItem: nullableQrItem }, index) => {
const itemIsSelected = selectedIds.indexOf(nanoId) !== -1;
{tableRows.map(({ id, qrItem: nullableQrItem }, index) => {
const itemIsSelected = selectedIds.indexOf(id) !== -1;
const answeredQrItem = createEmptyQrItem(tableQItem, undefined);
if (nullableQrItem) {
answeredQrItem.item = nullableQrItem.item;
}

return (
<GroupTableRow
key={nanoId}
key={id}
index={index}
nanoId={nanoId}
rowId={id}
tableQItem={tableQItem}
answeredQrItem={answeredQrItem}
nullableQrItem={nullableQrItem}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface GroupTableRowProps
PropsWithShowMinimalViewAttribute,
PropsWithParentIsReadOnlyAttribute,
TableRowProps {
nanoId: string;
rowId: string;
index: number;
tableQItem: QuestionnaireItem;
answeredQrItem: QuestionnaireResponseItem;
Expand All @@ -58,7 +58,7 @@ interface GroupTableRowProps

function GroupTableRow(props: GroupTableRowProps) {
const {
nanoId,
rowId,
index,
tableQItem,
answeredQrItem,
Expand All @@ -78,7 +78,7 @@ function GroupTableRow(props: GroupTableRowProps) {

if (isRepeated) {
return (
<Draggable draggableId={nanoId} index={index}>
<Draggable draggableId={rowId} index={index}>
{(draggableProvided, snapshot) => (
<StyledGroupTableRow
itemIsDragged={snapshot.isDragging}
Expand Down Expand Up @@ -106,7 +106,7 @@ function GroupTableRow(props: GroupTableRowProps) {
<SelectRowButton
isSelected={itemIsSelected}
readOnly={readOnly}
onSelectItem={() => onSelectRow(nanoId)}
onSelectItem={() => onSelectRow(rowId)}
/>
</>
)}
Expand Down Expand Up @@ -150,7 +150,7 @@ function GroupTableRow(props: GroupTableRowProps) {
<SelectRowButton
isSelected={itemIsSelected}
readOnly={readOnly}
onSelectItem={() => onSelectRow(nanoId)}
onSelectItem={() => onSelectRow(rowId)}
/>
</>
)}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ interface GroupTableViewProps
onAddRow: () => void;
onRowChange: (newQrRow: QuestionnaireResponseItem, index: number) => void;
onRemoveRow: (index: number) => void;
onSelectRow: (nanoId: string) => void;
onSelectRow: (rowId: string) => void;
onSelectAll: () => void;
onReorderRows: (newTableRows: GroupTableRowModel[]) => void;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ function useGroupTableRows(linkId: string, qrItems: QuestionnaireResponseItem[])

const [tableRows, setTableRows] = useState(initialisedGroupTableRows);
const [selectedIds, setSelectedIds] = useState<string[]>(
initialisedGroupTableRows.map((row) => row.nanoId)
initialisedGroupTableRows.map((row) => row.id)
);

return { tableRows, selectedIds, setTableRows, setSelectedIds };
Expand Down
23 changes: 8 additions & 15 deletions packages/smart-forms-renderer/src/hooks/useInitialiseGroupTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,16 @@ function useInitialiseGroupTable(
linkId: string,
qrItems: QuestionnaireResponseItem[]
): GroupTableRowModel[] {
let initialGroupTableRows: GroupTableRowModel[] = [
{
nanoId: generateNewRepeatId(linkId),
qrItem: null
}
];

if (qrItems.length > 0) {
initialGroupTableRows = qrItems.map((qrItem, index) => {
return {
nanoId: generateExistingRepeatId(linkId, index),
qrItem
};
});
if (qrItems.length === 0) {
return [{ id: generateNewRepeatId(linkId), qrItem: null }];
}

return initialGroupTableRows;
return qrItems.map((qrItem, index) => {
return {
id: generateExistingRepeatId(linkId, index),
qrItem
};
});
}

export default useInitialiseGroupTable;
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,27 @@
* limitations under the License.
*/

import type { QuestionnaireItem, QuestionnaireResponseItem } from 'fhir/r4';
import type { QuestionnaireResponseItem } from 'fhir/r4';
import type { RepeatGroupSingle } from '../interfaces/repeatGroup.interface';
import { useMemo } from 'react';
import { generateExistingRepeatId, generateNewRepeatId } from '../utils/repeatId';

function useInitialiseRepeatGroups(
qItem: QuestionnaireItem,
linkId: string,
qrItems: QuestionnaireResponseItem[]
): RepeatGroupSingle[] {
return useMemo(
() => {
let initialRepeatGroupAnswers: RepeatGroupSingle[] = [
{
nanoId: generateNewRepeatId(qItem.linkId),
qrItem: null
}
];
return useMemo(() => {
if (qrItems.length === 0) {
return [{ id: generateNewRepeatId(linkId), qrItem: null }];
}

if (qrItems.length > 0) {
initialRepeatGroupAnswers = qrItems.map((qrItem, index) => {
return {
nanoId: generateExistingRepeatId(qItem.linkId, index),
qrItem
};
});
}
return initialRepeatGroupAnswers;
},
// Requires checking of both qItem and qrItems
// eslint-disable-next-line react-hooks/exhaustive-deps
[qItem, qrItems]
);
return qrItems.map((qrItem, index) => {
return {
id: generateExistingRepeatId(linkId, index),
qrItem
};
});
}, [linkId, qrItems]);
}

export default useInitialiseRepeatGroups;
12 changes: 8 additions & 4 deletions packages/smart-forms-renderer/src/hooks/useRepeatGroups.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type { Dispatch, SetStateAction } from 'react';
import { useEffect, useState } from 'react';
import type { RepeatGroupSingle } from '../interfaces/repeatGroup.interface';
import _isEqual from 'lodash/isEqual';
import type { QuestionnaireResponseItem } from 'fhir/r4';

function useRepeatGroups(
valueFromProps: RepeatGroupSingle[]
Expand All @@ -27,10 +28,13 @@ function useRepeatGroups(

useEffect(
() => {
const valueFromPropsQRItems = valueFromProps.map(
(repeatGroupSingle) => repeatGroupSingle.qrItem
);
const repeatGroupsQRItems = repeatGroups.map((repeatGroupSingle) => repeatGroupSingle.qrItem);
const valueFromPropsQRItems = valueFromProps
.map((repeatGroupSingle) => repeatGroupSingle.qrItem)
.filter((qrItem): qrItem is QuestionnaireResponseItem => qrItem !== null);

const repeatGroupsQRItems = repeatGroups
.map((repeatGroupSingle) => repeatGroupSingle.qrItem)
.filter((qrItem): qrItem is QuestionnaireResponseItem => qrItem !== null);

if (!_isEqual(valueFromPropsQRItems, repeatGroupsQRItems)) {
setRepeatGroups(valueFromProps);
Expand Down
1 change: 1 addition & 0 deletions packages/smart-forms-renderer/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export {
destroyForm,
getResponse,
removeEmptyAnswersFromResponse,
removeInternalIdsFromResponse,
isSpecificItemControl,
isRepeatItemAndNotCheckbox,
initialiseQuestionnaireResponse,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
import type { QuestionnaireResponseItem } from 'fhir/r4';

export interface GroupTableRowModel {
nanoId: string;
id: string;
qrItem: QuestionnaireResponseItem | null;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export interface QrRepeatGroup {
}

export interface RepeatGroupSingle {
nanoId: string;
id: string;
qrItem: QuestionnaireResponseItem | null;
}
Loading

0 comments on commit 893983c

Please sign in to comment.