Skip to content

Commit 7e9b7ea

Browse files
committed
fix saving issue(aggregate type, yaxiscolumns) and show only integer values in yaxis col dropdown
1 parent 052cbd9 commit 7e9b7ea

File tree

2 files changed

+109
-41
lines changed

2 files changed

+109
-41
lines changed

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

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import { ChartTypes } from '@/gql/generated/graphql';
33
import { Button, Checkbox, Select, TextField } from 'opub-ui';
44

@@ -33,6 +33,16 @@ const ChartForm: React.FC<ChartFormProps> = ({
3333
chartData.type === ChartTypes.Line ||
3434
chartData.type === ChartTypes.Multiline;
3535

36+
37+
useEffect(() => {
38+
if (!chartData.options.yAxisColumn || chartData.options.yAxisColumn.length === 0) {
39+
handleChange('options', {
40+
...chartData.options,
41+
yAxisColumn: [{ fieldName: '', label: '', color: '' }],
42+
});
43+
}
44+
}, [chartData.options.yAxisColumn]);
45+
3646
const handleYAxisColumnChange = (
3747
index: number,
3848
field: string,
@@ -70,7 +80,31 @@ const ChartForm: React.FC<ChartFormProps> = ({
7080
});
7181
};
7282

73-
console.log(chartData, "data");
83+
const updateChartData = (field: string, value: any) => {
84+
85+
if (field === 'type') {
86+
const newData = {
87+
...chartData,
88+
type: value,
89+
options: {
90+
...chartData.options,
91+
yAxisColumn: chartData.options.yAxisColumn.length > 0
92+
? chartData.options.yAxisColumn
93+
: [{ fieldName: '', label: '', color: '' }],
94+
},
95+
};
96+
handleChange(field, value);
97+
handleSave(newData); // Pass the new data directly
98+
} else {
99+
const newData = {
100+
...chartData,
101+
[field]: value,
102+
};
103+
handleChange(field, value);
104+
handleSave(newData);
105+
}
106+
};
107+
74108

75109
return (
76110
<div className="flex flex-col gap-4">
@@ -99,7 +133,7 @@ console.log(chartData, "data");
99133
label="Chart Type"
100134
value={chartData.type}
101135
onBlur={() => handleSave(chartData)}
102-
onChange={(e) => handleChange('type', e)}
136+
onChange={(e) => updateChartData('type', e)}
103137
placeholder="Select"
104138
/>
105139
<Select
@@ -128,7 +162,20 @@ console.log(chartData, "data");
128162
>
129163
Show Legend
130164
</Checkbox>
131-
165+
<Select
166+
name="aggregateType"
167+
options={[
168+
{ label: 'None', value: 'NONE' },
169+
{ label: 'Sum', value: 'SUM' },
170+
{ label: 'Average', value: 'AVERAGE' },
171+
{ label: 'Count', value: 'COUNT' },
172+
]}
173+
label="Aggregate"
174+
value={chartData.options.aggregateType}
175+
defaultValue="SUM"
176+
onBlur={() => handleSave(chartData)}
177+
onChange={(e) => handleChange('options', { ...chartData.options, aggregateType: e })}
178+
/>
132179
{!isAssamChart && (
133180
<>
134181
<Select
@@ -181,10 +228,12 @@ console.log(chartData, "data");
181228
<div key={index} className="flex items-end gap-4">
182229
<Select
183230
name={`yAxisColumn-${index}`}
184-
options={resourceSchema?.map((field) => ({
185-
label: field.fieldName,
186-
value: field.id,
187-
}))}
231+
options={resourceSchema
232+
?.filter(field => field.format.toUpperCase() === 'INTEGER' )
233+
.map((field) => ({
234+
label: field.fieldName,
235+
value: field.id,
236+
}))}
188237
label="Y-axis Column"
189238
value={column.fieldName}
190239
onChange={(e) =>
@@ -203,7 +252,7 @@ console.log(chartData, "data");
203252
<input
204253
name={`yAxisColor-${index}`}
205254
type="Color"
206-
value={column.color}
255+
value={column.color || '#000000'}
207256
onChange={(e: any) => {
208257
handleYAxisColumnChange(index, 'color', e.target.value);
209258
}}

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

Lines changed: 51 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { UUID } from 'crypto';
22
import React, { useCallback, useEffect, useRef, useState } from 'react';
33
import { useParams } from 'next/navigation';
44
import { renderGeoJSON } from '@/geo_json/render_geojson';
5+
import { ChartTypes } from '@/gql/generated/graphql';
56
import { useMutation, useQuery } from '@tanstack/react-query';
67
import ReactECharts from 'echarts-for-react';
78
import * as echarts from 'echarts/core';
@@ -17,7 +18,6 @@ import {
1718
getResourceChartDetails,
1819
} from '../queries';
1920
import ChartForm from './ChartForm';
20-
import { ChartTypes } from '@/gql/generated/graphql';
2121
import ChartHeader from './ChartHeader';
2222

2323
interface YAxisColumnItem {
@@ -177,17 +177,28 @@ const ChartsVisualize: React.FC<VisualizationProps> = ({
177177
const [resourceSchema, setResourceSchema] = useState<any[]>([]);
178178

179179
useEffect(() => {
180-
if (resourceData) {
181-
if (chartData.resource) {
182-
const resource = resourceData?.datasetResources.find(
183-
(resource: any) => resource.id === chartData.resource
184-
);
180+
if (resourceData && chartData.resource) {
181+
const resource = resourceData?.datasetResources.find(
182+
(resource: any) => resource.id === chartData.resource
183+
);
184+
if (resource) {
185+
setResourceSchema(resource.schema || []);
186+
} else {
187+
setResourceSchema([]); // Reset if not found
188+
}
189+
}
190+
}, [resourceData, chartData.resource]);
191+
useEffect(() => {
192+
if (chartId && chartDetails?.resourceChart) {
193+
const resource = resourceData?.datasetResources?.find(
194+
(r: any) => r.id === chartDetails.resourceChart.resource?.id
195+
);
185196

186-
setResourceSchema(resource?.schema || []);
187-
handleChange('resource', chartData.resource);
197+
if (resource) {
198+
setResourceSchema(resource.schema || []);
188199
}
189200
}
190-
}, [resourceData, chartId, chartData.resource]);
201+
}, [chartId, chartDetails, resourceData]);
191202

192203
useEffect(() => {
193204
if (chartId && chartDetails?.resourceChart) {
@@ -294,10 +305,7 @@ const ChartsVisualize: React.FC<VisualizationProps> = ({
294305
});
295306
}, []);
296307

297-
const {
298-
mutate,
299-
isLoading: editMutationLoading,
300-
} = useMutation(
308+
const { mutate, isLoading: editMutationLoading } = useMutation(
301309
(chartInput: { chartInput: ResourceChartInput }) =>
302310
GraphQL(
303311
createChart,
@@ -323,41 +331,52 @@ const ChartsVisualize: React.FC<VisualizationProps> = ({
323331

324332
const handleSave = useCallback(
325333
(updatedData: ChartData) => {
334+
326335
if (JSON.stringify(previousChartData) !== JSON.stringify(updatedData)) {
327-
336+
// Filter out empty Y-axis columns
328337
const validYAxisColumns = updatedData.options.yAxisColumn.filter(
329-
col => col.fieldName && col.fieldName.trim() !== ''
338+
(col) => col.fieldName && col.fieldName.trim() !== ''
330339
);
331-
340+
332341
const chartInput: ResourceChartInput = {
333342
chartId: updatedData.chartId,
334343
description: updatedData.description,
335344
filters: updatedData.filters,
336345
name: updatedData.name,
337346
options: {
338347
...updatedData.options,
339-
yAxisColumn: validYAxisColumns
348+
yAxisColumn: validYAxisColumns,
340349
},
341350
resource: updatedData.resource,
342-
type: updatedData.type as ChartTypes,
351+
type: updatedData.type,
343352
};
344353

345-
mutate(
346-
{ chartInput },
347-
{
348-
onSuccess: (data) => {
349-
setChartData((prev) => ({
350-
...prev,
351-
chart: data.chart,
352-
}));
353-
},
354-
}
355-
);
354+
// Store current type before mutation
355+
const currentType = updatedData.type;
356356

357-
setPreviousChartData(updatedData);
357+
mutate(
358+
{ chartInput },
359+
{
360+
onSuccess: (data) => {
361+
setChartData((prev) => ({
362+
...prev,
363+
chart: data.chart,
364+
type: currentType, // Preserve the type from before mutation
365+
options: {
366+
...prev.options,
367+
yAxisColumn: validYAxisColumns,
368+
},
369+
}));
370+
},
371+
}
372+
);
373+
setPreviousChartData({
374+
...updatedData,
375+
type: currentType, // Ensure previousChartData also has correct type
376+
});
358377
}
359378
},
360-
[previousChartData]
379+
[previousChartData, mutate]
361380
);
362381

363382
const handleResourceChange = useCallback(
@@ -407,7 +426,7 @@ const ChartsVisualize: React.FC<VisualizationProps> = ({
407426
/>
408427
<div className="mb-6 flex flex-col gap-6 p-8 text-center">
409428
<Text>Preview</Text>
410-
{chartData.chart && Object.keys(chartData.chart).length > 0 && (
429+
{chartData.chart && Object.keys(chartData.chart).length > 0 && (
411430
<ReactECharts option={chartData.chart} ref={chartRef} />
412431
)}
413432
</div>

0 commit comments

Comments
 (0)