3
3
computed , reactive , watch ,
4
4
} from ' vue' ;
5
5
import type { TranslateResult } from ' vue-i18n' ;
6
- import { useRoute } from ' vue-router/composables' ;
7
6
8
7
import { useMutation , useQueryClient } from ' @tanstack/vue-query' ;
9
8
@@ -13,6 +12,7 @@ import {
13
12
14
13
import type { WidgetCreateParams , WidgetModel } from ' @/api-clients/dashboard/_types/widget-type' ;
15
14
import type { DataTableAddParameters } from ' @/api-clients/dashboard/public-data-table/schema/api-verbs/add' ;
15
+ import { useServiceQueryKey } from ' @/query/query-key/use-service-query-key' ;
16
16
import { i18n } from ' @/translations' ;
17
17
18
18
import { useAllReferenceStore } from ' @/store/reference/all-reference-store' ;
@@ -41,21 +41,22 @@ import {
41
41
DATA_TABLE_TYPE , TRANSFORM_DATA_TABLE_DEFAULT_OPTIONS ,
42
42
} from ' @/common/modules/widgets/_constants/data-table-constant' ;
43
43
import { getDuplicatedDataTableName } from ' @/common/modules/widgets/_helpers/widget-data-table-helper' ;
44
+ import { useWidgetContextStore } from ' @/common/modules/widgets/_store/widget-context-store' ;
44
45
import { useWidgetGenerateStore } from ' @/common/modules/widgets/_store/widget-generate-store' ;
45
46
import type { DataTableModel } from ' @/common/modules/widgets/types/widget-data-table-type' ;
46
47
import type {
47
48
DataTableDataType , DataTableSourceType , DataTableOperator , DataTableAddOptions , DataTableTransformOptions ,
48
49
} from ' @/common/modules/widgets/types/widget-model' ;
49
50
50
- import { useDashboardGetQuery } from ' @/services/_shared/dashboard/dashboard-detail/composables/use-dashboard-get-query' ;
51
- import { useDashboardWidgetListQuery } from ' @/services/_shared/dashboard/dashboard-detail/composables/use-dashboard-widget-list-query' ;
52
51
53
52
const widgetGenerateStore = useWidgetGenerateStore ();
54
53
const widgetGenerateState = widgetGenerateStore .state ;
55
54
const allReferenceStore = useAllReferenceStore ();
55
+ const widgetContextStore = useWidgetContextStore ();
56
+ const widgetContextState = widgetContextStore .state ;
57
+ const widgetContextGetters = widgetContextStore .getters ;
56
58
const userStore = useUserStore ();
57
- const route = useRoute ();
58
- const dashboardId = computed (() => route .params .dashboardId );
59
+ const dashboardId = computed (() => widgetContextGetters .dashboardId );
59
60
60
61
const emit = defineEmits <{(e : ' scroll' ): void ;}>();
61
62
@@ -76,18 +77,6 @@ const {
76
77
});
77
78
const queryClient = useQueryClient ();
78
79
79
- const {
80
- dashboard,
81
- } = useDashboardGetQuery ({
82
- dashboardId ,
83
- });
84
- const {
85
- keys : widgetKeys,
86
- } = useDashboardWidgetListQuery ({
87
- dashboardId ,
88
- });
89
-
90
-
91
80
const storeState = reactive ({
92
81
metrics: computed <MetricReferenceMap >(() => allReferenceStore .getters .metric ),
93
82
costDataSources: computed <CostDataSourceReferenceMap >(() => allReferenceStore .getters .costDataSource ),
@@ -204,30 +193,33 @@ const state = reactive({
204
193
});
205
194
206
195
/* Api */
196
+ const { withSuffix : privateWidgetListWithSuffix } = useServiceQueryKey (' dashboard' , ' private-widget' , ' list' );
197
+ const { withSuffix : publicWidgetListWithSuffix } = useServiceQueryKey (' dashboard' , ' public-widget' , ' list' );
198
+
207
199
const widgetCreateFn = (params : WidgetCreateParams ): Promise <WidgetModel > => {
208
- if (dashboardId .value ?.startsWith (' private' )) {
200
+ if (! params .dashboard_id ) {
201
+ throw new Error (' dashboardId is undefined' );
202
+ }
203
+
204
+ if (params .dashboard_id .startsWith (' private' )) {
209
205
return widgetApi .privateWidgetAPI .create (params );
210
206
}
211
207
return widgetApi .publicWidgetAPI .create (params );
212
208
};
213
209
const { mutateAsync : createWidget, isPending : widgetCreateLoading } = useMutation ({
214
210
mutationFn: widgetCreateFn ,
215
- onSuccess : (data ) => {
216
- const _isPrivate = dashboardId .value ?.startsWith (' private' );
217
- const widgetListQueryKey = _isPrivate ? widgetKeys .privateWidgetListQueryKey : widgetKeys .publicWidgetListQueryKey ;
218
- queryClient .setQueryData (widgetListQueryKey .value , (oldData : ListResponse <WidgetModel >) => (oldData .results ?.length ? {
219
- ... oldData , results: [... oldData .results , data ],
220
- } : {
221
- ... oldData , results: [data ],
222
- }));
211
+ onSuccess : (data , variables ) => {
212
+ const _isPrivate = variables .dashboard_id .startsWith (' private' );
213
+ const widgetListQueryKey = _isPrivate ? privateWidgetListWithSuffix (variables .dashboard_id ) : publicWidgetListWithSuffix (variables .dashboard_id );
214
+ queryClient .invalidateQueries ({ queryKey: widgetListQueryKey });
223
215
widgetGenerateStore .setWidgetFormInfo (data );
224
216
},
225
217
onError : (e ) => {
226
218
ErrorHandler .handleError (e );
227
219
},
228
220
});
229
221
const dataTableAddFn = (params : DataTableAddParameters ): Promise <DataTableModel > => {
230
- if (dashboardId . value ? .startsWith (' private' )) {
222
+ if (params . widget_id .startsWith (' private' )) {
231
223
return dataTableApi .privateDataTableAPI .add (params );
232
224
}
233
225
return dataTableApi .publicDataTableAPI .add (params );
@@ -297,10 +289,13 @@ const handleSelectPopperCondition = (condition: DataTableDataType) => {
297
289
state .selectedPopperCondition = condition ;
298
290
};
299
291
const handleConfirmDataSource = async () => {
292
+ if (! dashboardId .value ) {
293
+ throw new Error (' dashboardId is undefined' );
294
+ }
300
295
// create widget
301
296
if (widgetGenerateState .overlayType === ' ADD' && ! widgetGenerateState .widgetId ) {
302
297
await createWidget ({
303
- dashboard_id: dashboardId .value as string ,
298
+ dashboard_id: dashboardId .value ,
304
299
tags: { created_by: userStore .state .userId },
305
300
widget_type: ' table' ,
306
301
});
@@ -355,7 +350,7 @@ const handleConfirmDataSource = async () => {
355
350
356
351
const mergedParams = {
357
352
... addParameters ,
358
- vars: dashboard . value ?.vars || {},
353
+ vars: widgetContextState . dashboard ?.vars || {},
359
354
};
360
355
if (state .selectedDataSourceDomain === DATA_SOURCE_DOMAIN .COST ) {
361
356
mergedParams .options = costOptions ;
0 commit comments