Skip to content

Commit 1f5dc1e

Browse files
authored
Merge pull request #389 from CivicDataLab/Usecase-selected-state-fix
Fixed Usecase selected state for collaborative
2 parents 7b5b66d + c9a59c3 commit 1f5dc1e

1 file changed

Lines changed: 18 additions & 18 deletions

File tree

  • app/[locale]/dashboard/[entityType]/[entitySlug]/collaboratives/edit/[id]/usecases

app/[locale]/dashboard/[entityType]/[entitySlug]/collaboratives/edit/[id]/usecases/page.tsx

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,16 +76,6 @@ const UseCases = () => {
7676
}
7777
);
7878

79-
const formattedData = (data: any) =>
80-
data.map((item: any) => {
81-
return {
82-
title: item.title,
83-
id: item.id,
84-
category: item.sectors[0]?.name || 'N/A', // Safeguard in case of missing category
85-
modified: formatDate(item.modified) || '',
86-
};
87-
});
88-
8979
useEffect(() => {
9080
fetchData('usecase', '?size=1000&page=1')
9181
.then((res) => {
@@ -104,16 +94,28 @@ const UseCases = () => {
10494

10595
const generateTableData = (list: Array<any>) => {
10696
return list.map((item) => {
97+
const sector = item.sectors?.[0];
10798
return {
10899
title: item.title,
109-
id: item.id,
110-
category: item.sectors[0],
100+
id: String(item.id),
101+
category:
102+
typeof sector === 'string'
103+
? sector
104+
: sector?.name || 'N/A',
111105
modified: formatDate(item.modified) || '',
112106
};
113107
});
114108
};
115109

116-
const { mutate, isLoading: mutationLoading } = useMutation(
110+
const rows = generateTableData(data);
111+
const assignedUseCaseIds = new Set(
112+
(CollaborativeDetails?.data?.collaboratives[0]?.useCases ?? []).map(
113+
(item: any) => String(item.id)
114+
)
115+
);
116+
const defaultSelectedRows = rows.filter((row) => assignedUseCaseIds.has(row.id));
117+
118+
const { mutate } = useMutation(
117119
() =>
118120
GraphQL(
119121
AssignCollaborativeUseCases,
@@ -128,7 +130,7 @@ const UseCases = () => {
128130
}
129131
),
130132
{
131-
onSuccess: (data: any) => {
133+
onSuccess: () => {
132134
toast('Use Cases Assigned Successfully', { id: COLLAB_USECASES_TOAST_ID });
133135
CollaborativeDetails.refetch();
134136
router.push(
@@ -162,10 +164,8 @@ const UseCases = () => {
162164

163165
<DataTable
164166
columns={columns}
165-
rows={generateTableData(data)}
166-
defaultSelectedRows={formattedData(
167-
CollaborativeDetails?.data?.collaboratives[0]?.useCases
168-
)}
167+
rows={rows}
168+
defaultSelectedRows={defaultSelectedRows}
169169
onRowSelectionChange={(selected) => {
170170
setSelectedRows(Array.isArray(selected) ? selected : []); // Ensure selected is always an array
171171
}}

0 commit comments

Comments
 (0)