Skip to content

Commit d272bd7

Browse files
committed
fix queries and add relevant mutations to delete the chart
1 parent 31e06be commit d272bd7

File tree

1 file changed

+116
-22
lines changed
  • app/[locale]/dashboard/[entityType]/[entitySlug]/dataset/[id]/edit/charts/components

1 file changed

+116
-22
lines changed

app/[locale]/dashboard/[entityType]/[entitySlug]/dataset/[id]/edit/charts/components/ChartsList.tsx

Lines changed: 116 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,21 @@ interface ChartsListProps {
2222
setType: any;
2323
type: any;
2424
setChartId: any;
25+
setImageId: any;
2526
}
2627
const chartDetailsQuery: any = graphql(`
27-
query chartsDetails($datasetId: UUID!) {
28-
chartsDetails(datasetId: $datasetId) {
29-
id
30-
name
31-
chartType
28+
query ChartDetails($datasetId: UUID!) {
29+
getChartData(datasetId: $datasetId) {
30+
__typename
31+
... on TypeResourceChart {
32+
name
33+
id
34+
chartType
35+
}
36+
... on TypeResourceChartImage {
37+
name
38+
id
39+
}
3240
}
3341
}
3442
`);
@@ -39,14 +47,29 @@ const deleteResourceChart: any = graphql(`
3947
}
4048
`);
4149

50+
const deleteResourceChartImage: any = graphql(`
51+
mutation deleteResourceChartImage($resourceChartImageId: String!) {
52+
deleteResourceChartImage(resourceChartImageId: $resourceChartImageId)
53+
}
54+
`);
4255

43-
44-
56+
const AddResourceChartImage: any = graphql(`
57+
mutation GenerateResourceChartImage($dataset: UUID!) {
58+
addResourceChartImage(dataset: $dataset) {
59+
__typename
60+
... on TypeResourceChartImage {
61+
id
62+
name
63+
}
64+
}
65+
}
66+
`);
4567

4668
const ChartsList: React.FC<ChartsListProps> = ({
4769
setType,
4870
type,
4971
setChartId,
72+
setImageId,
5073
}) => {
5174
const params = useParams<{
5275
entityType: string;
@@ -76,24 +99,73 @@ const ChartsList: React.FC<ChartsListProps> = ({
7699

77100
useEffect(() => {
78101
refetch();
79-
if (data?.chartsDetails) {
80-
setFilteredRows(data.chartsDetails);
102+
if (data?.getChartData) {
103+
setFilteredRows(data.getChartData);
81104
}
82105
}, [data, type]);
83106

84-
const { mutate, isLoading: deleteLoading } = useMutation(
85-
(data: { chartId: UUID }) =>
107+
const deleteResourceChartmutation: { mutate: any; isLoading: any } =
108+
useMutation(
109+
(data: { chartId: UUID }) =>
110+
GraphQL(
111+
deleteResourceChart,
112+
{
113+
[params.entityType]: params.entitySlug,
114+
},
115+
data
116+
),
117+
{
118+
onSuccess: () => {
119+
toast('Chart Deleted Successfully');
120+
refetch();
121+
},
122+
onError: (err: any) => {
123+
toast(`Received ${err} while deleting chart `);
124+
},
125+
}
126+
);
127+
128+
const deleteResourceChartImagemutation: { mutate: any; isLoading: any } =
129+
useMutation(
130+
(data: { resourceChartImageId: string }) =>
131+
GraphQL(
132+
deleteResourceChartImage,
133+
{
134+
[params.entityType]: params.entitySlug,
135+
},
136+
data
137+
),
138+
{
139+
onSuccess: () => {
140+
toast('ChartImage Deleted Successfully');
141+
refetch();
142+
},
143+
onError: (err: any) => {
144+
toast(`Received ${err} while deleting chart `);
145+
},
146+
}
147+
);
148+
149+
const resourceChartImageMutation: {
150+
mutate: any;
151+
isLoading: any;
152+
} = useMutation(
153+
(data: { dataset: UUID }) =>
86154
GraphQL(
87-
deleteResourceChart,
155+
AddResourceChartImage,
88156
{
89157
[params.entityType]: params.entitySlug,
90158
},
91159
data
92160
),
93161
{
94-
onSuccess: () => {
95-
toast('Chart Deleted Successfully');
162+
onSuccess: (res: any) => {
163+
toast('Resource Chart Image Created Successfully');
96164
refetch();
165+
setType('img');
166+
setImageId(res.addResourceChartImage.id);
167+
168+
// setImageId(res.id);
97169
},
98170
onError: (err: any) => {
99171
toast(`Received ${err} while deleting chart `);
@@ -102,8 +174,13 @@ const ChartsList: React.FC<ChartsListProps> = ({
102174
);
103175

104176
const handleChart = (row: any) => {
105-
setChartId(row.original.id);
106-
setType('visualize');
177+
if (row.original.__typename === 'TypeResourceChart') {
178+
setChartId(row.original.id);
179+
setType('visualize');
180+
} else {
181+
setType('img');
182+
setImageId(row.original.id);
183+
}
107184
};
108185

109186
const generateColumnData = () => {
@@ -133,7 +210,15 @@ const ChartsList: React.FC<ChartsListProps> = ({
133210
size="medium"
134211
icon={Icons.delete}
135212
color="interactive"
136-
onClick={() => mutate({ chartId: row.original.id })}
213+
onClick={() => {
214+
row.original.__typename === 'TypeResourceChart'
215+
? deleteResourceChartmutation.mutate({
216+
chartId: row.original.id,
217+
})
218+
: deleteResourceChartImagemutation.mutate({
219+
resourceChartImageId: row.original.id,
220+
});
221+
}}
137222
>
138223
Delete
139224
</IconButton>
@@ -143,11 +228,14 @@ const ChartsList: React.FC<ChartsListProps> = ({
143228
];
144229
};
145230

146-
const generateTableData = (accessModel: any[]) => {
147-
return accessModel?.map((item: any) => ({
231+
const generateTableData = (data: any[]) => {
232+
return data?.map((item: any) => ({
148233
name: item.name,
149-
type: toTitleCase(item.chartType.split('_').join(' ').toLowerCase()),
234+
type: item.chartType
235+
? toTitleCase(item.chartType.split('_').join(' ').toLowerCase())
236+
: 'Image',
150237
id: item.id,
238+
typename: item.__typename,
151239
}));
152240
};
153241

@@ -162,7 +250,7 @@ const ChartsList: React.FC<ChartsListProps> = ({
162250
return (
163251
<>
164252
{' '}
165-
{!data || isLoading || deleteLoading ? (
253+
{!data || isLoading || deleteResourceChartmutation.isLoading ? (
166254
<div className=" mt-8 flex justify-center">
167255
<Spinner />
168256
</div>
@@ -181,7 +269,13 @@ const ChartsList: React.FC<ChartsListProps> = ({
181269
<Button onClick={(e) => setType('visualize')}>
182270
Visualize Data
183271
</Button>
184-
<Button onClick={(e) => setType('img')}>Add Image</Button>
272+
<Button
273+
onClick={(e) =>
274+
resourceChartImageMutation.mutate({ dataset: params.id })
275+
}
276+
>
277+
Add Image
278+
</Button>
185279
</div>
186280
</div>
187281
<DataTable

0 commit comments

Comments
 (0)