Skip to content

Commit e43573e

Browse files
committed
fix(chart): fix drill option state issue
1 parent 8f45107 commit e43573e

File tree

2 files changed

+36
-28
lines changed

2 files changed

+36
-28
lines changed

frontend/src/app/components/ChartEditor.tsx

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -200,12 +200,8 @@ export const ChartEditor: FC<ChartEditorProps> = ({
200200
}, [backendChart?.config?.chartGraphId]);
201201

202202
useEffect(() => {
203-
const drillPaths = getDrillPaths(chartConfig?.datas);
204-
if (isEmptyArray(drillPaths)) {
205-
drillOptionRef.current = undefined;
206-
}
207-
if (!isEmptyArray(drillPaths) && !drillOptionRef.current) {
208-
drillOptionRef.current = new ChartDrillOption(drillPaths);
203+
if (!drillOptionRef.current) {
204+
initChartDrillOption(chartConfig?.datas);
209205
}
210206
}, [chartConfig?.datas, drillOptionRef]);
211207

@@ -219,7 +215,7 @@ export const ChartEditor: FC<ChartEditorProps> = ({
219215
const option = drillOptionRef.current;
220216
option.drillDown(param.data.rowData);
221217
drillOptionRef.current = option;
222-
handleDrillOptionChange(option);
218+
handleDrillOptionChange?.(option);
223219
return;
224220
}
225221
if (
@@ -273,6 +269,7 @@ export const ChartEditor: FC<ChartEditorProps> = ({
273269
},
274270
}),
275271
);
272+
initChartDrillOption(finalChartConfig?.datas);
276273
}, [dispatch, chart?.meta?.id, registerChartEvents]);
277274

278275
const handleChartChange = (c: IChart) => {
@@ -292,29 +289,16 @@ export const ChartEditor: FC<ChartEditorProps> = ({
292289
},
293290
}),
294291
);
292+
const drillOpt = initChartDrillOption(finalChartConfig?.datas);
295293
if (!expensiveQuery) {
296-
dispatch(refreshDatasetAction({ drillOption: drillOptionRef?.current }));
294+
dispatch(refreshDatasetAction({ drillOption: drillOpt }));
297295
} else {
298296
setAllowQuery(true);
299297
}
300298
};
301299

302300
const handleChartConfigChange = useCallback(
303301
(type, payload) => {
304-
const drillPaths = getDrillPaths(chartConfig?.datas);
305-
if (isEmptyArray(drillPaths)) {
306-
drillOptionRef.current = undefined;
307-
}
308-
if (
309-
!isEmptyArray(drillPaths) &&
310-
drillOptionRef.current
311-
?.getAllFields()
312-
?.map(p => p.uid)
313-
.join('-') !== drillPaths.map(p => p.uid).join('-')
314-
) {
315-
drillOptionRef.current = new ChartDrillOption(drillPaths);
316-
}
317-
318302
if (expensiveQuery) {
319303
dispatch(
320304
workbenchSlice.actions.updateChartConfig({
@@ -332,11 +316,11 @@ export const ChartEditor: FC<ChartEditorProps> = ({
332316
type,
333317
payload,
334318
needRefresh: payload.needRefresh,
335-
drillOption: drillOptionRef?.current,
319+
updateDrillOption: config => initChartDrillOption(config?.datas),
336320
}),
337321
);
338322
},
339-
[chartConfig?.datas, dispatch, expensiveQuery],
323+
[dispatch, expensiveQuery],
340324
);
341325

342326
const handleDataViewChanged = useCallback(() => {
@@ -545,6 +529,25 @@ export const ChartEditor: FC<ChartEditorProps> = ({
545529
dispatch(refreshDatasetAction({ drillOption: option }));
546530
};
547531

532+
const initChartDrillOption = (
533+
datas?: any[],
534+
): IChartDrillOption | undefined => {
535+
const drillPaths = getDrillPaths(datas);
536+
if (isEmptyArray(drillPaths)) {
537+
drillOptionRef.current = undefined;
538+
}
539+
if (
540+
!isEmptyArray(drillPaths) &&
541+
drillOptionRef.current
542+
?.getAllFields()
543+
?.map(p => p.uid)
544+
.join('-') !== drillPaths.map(p => p.uid).join('-')
545+
) {
546+
drillOptionRef.current = new ChartDrillOption(drillPaths);
547+
}
548+
return drillOptionRef.current;
549+
};
550+
548551
return (
549552
<StyledChartWorkbenchPage>
550553
<SaveFormContext.Provider value={saveFormContextValue}>

frontend/src/app/pages/ChartWorkbenchPage/slice/thunks.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import { createAsyncThunk } from '@reduxjs/toolkit';
2020
import beginViewModelMigration from 'app/migration/ViewConfig/migrationViewModelConfig';
2121
import { ChartDataRequestBuilder } from 'app/models/ChartDataRequestBuilder';
22+
import { ChartConfig } from 'app/types/ChartConfig';
2223
import { ChartDataRequest } from 'app/types/ChartDataRequest';
2324
import { IChartDrillOption } from 'app/types/ChartDrillOption';
2425
import { ChartDTO } from 'app/types/ChartDTO';
@@ -125,7 +126,9 @@ export const updateChartConfigAndRefreshDatasetAction = createAsyncThunk(
125126
type: string;
126127
payload: ChartConfigPayloadType;
127128
needRefresh?: boolean;
128-
drillOption?: IChartDrillOption;
129+
updateDrillOption: (
130+
config?: ChartConfig,
131+
) => IChartDrillOption | undefined;
129132
},
130133
thunkAPI,
131134
) => {
@@ -134,10 +137,12 @@ export const updateChartConfigAndRefreshDatasetAction = createAsyncThunk(
134137
await thunkAPI.dispatch(
135138
workbenchSlice.actions.updateShadowChartConfig(null),
136139
);
140+
const state = thunkAPI.getState() as any;
141+
const workbenchState = state.workbench as typeof initState;
142+
const chartConfig = workbenchState.chartConfig;
143+
const drillOpt = arg.updateDrillOption?.(chartConfig);
137144
if (arg.needRefresh) {
138-
thunkAPI.dispatch(
139-
refreshDatasetAction({ drillOption: arg.drillOption }),
140-
);
145+
thunkAPI.dispatch(refreshDatasetAction({ drillOption: drillOpt }));
141146
}
142147
} catch (error) {
143148
return rejectHandle(error, thunkAPI.rejectWithValue);

0 commit comments

Comments
 (0)