|
5 | 5 | // file, You can obtain one at https://mozilla.org/MPL/2.0/. |
6 | 6 |
|
7 | 7 | import { GET_PARAMS } from './enum'; |
8 | | -import { apiFetch } from './api'; |
| 8 | +import { apiFetch, ApiFetchResponse, ExtendedError } from './api'; |
| 9 | +import { |
| 10 | + CancelledError, |
| 11 | + QueryClient, |
| 12 | + UndefinedInitialDataOptions, |
| 13 | + useMutation, |
| 14 | + UseMutationOptions, |
| 15 | + useQuery, |
| 16 | + useQueryClient, |
| 17 | +} from '@tanstack/react-query'; |
| 18 | +import { queryClient } from '../common/queryClient'; |
| 19 | +import { QueryParams, urlEncode } from '../url2'; |
| 20 | +import { dashboardMigrateSaveToOld } from '../store2/urlStore/dashboardMigrate'; |
| 21 | +import { toNumber } from '../common/helpers'; |
9 | 22 |
|
10 | 23 | const ApiDashboardEndpoint = '/api/dashboard'; |
11 | 24 |
|
@@ -61,3 +74,139 @@ export async function apiDashboardSaveFetch(params: ApiDashboardPost, keyRequest |
61 | 74 | keyRequest, |
62 | 75 | }); |
63 | 76 | } |
| 77 | + |
| 78 | +export function getDashboardOptions<T = ApiDashboard>( |
| 79 | + dashboardId: string, |
| 80 | + dashboardVersion?: string |
| 81 | +): UndefinedInitialDataOptions<ApiDashboard, ExtendedError, T, [string, ApiDashboardGet]> { |
| 82 | + const fetchParams: ApiDashboardGet = { [GET_PARAMS.dashboardID]: dashboardId }; |
| 83 | + if (dashboardVersion != null) { |
| 84 | + fetchParams[GET_PARAMS.dashboardApiVersion] = dashboardVersion; |
| 85 | + } |
| 86 | + return { |
| 87 | + queryKey: [ApiDashboardEndpoint, fetchParams], |
| 88 | + queryFn: async ({ signal }) => { |
| 89 | + const { response, error } = await apiDashboardFetch(fetchParams, signal); |
| 90 | + if (error) { |
| 91 | + throw error; |
| 92 | + } |
| 93 | + if (!response) { |
| 94 | + throw new ExtendedError('empty response'); |
| 95 | + } |
| 96 | + return response; |
| 97 | + }, |
| 98 | + placeholderData: (previousData, previousQuery) => previousData, |
| 99 | + }; |
| 100 | +} |
| 101 | + |
| 102 | +export async function apiDashboard(dashboardId: string, dashboardVersion?: string) { |
| 103 | + const result: ApiFetchResponse<ApiDashboard> = { ok: false, status: 0 }; |
| 104 | + try { |
| 105 | + result.response = await queryClient.fetchQuery(getDashboardOptions<ApiDashboard>(dashboardId, dashboardVersion)); |
| 106 | + result.ok = true; |
| 107 | + } catch (error) { |
| 108 | + result.status = ExtendedError.ERROR_STATUS_UNKNOWN; |
| 109 | + if (error instanceof ExtendedError) { |
| 110 | + result.error = error; |
| 111 | + result.status = error.status; |
| 112 | + } else if (error instanceof CancelledError) { |
| 113 | + result.error = new ExtendedError(error, ExtendedError.ERROR_STATUS_ABORT); |
| 114 | + result.status = ExtendedError.ERROR_STATUS_ABORT; |
| 115 | + } else { |
| 116 | + result.error = new ExtendedError(error); |
| 117 | + } |
| 118 | + } |
| 119 | + return result; |
| 120 | +} |
| 121 | + |
| 122 | +export function useApiDashboard<T = ApiDashboard>( |
| 123 | + dashboardId: string, |
| 124 | + dashboardVersion?: string, |
| 125 | + select?: (response?: ApiDashboard) => T, |
| 126 | + enabled: boolean = true |
| 127 | +) { |
| 128 | + const options = getDashboardOptions<ApiDashboard>(dashboardId, dashboardVersion); |
| 129 | + return useQuery({ ...options, select, enabled }); |
| 130 | +} |
| 131 | +export function getDashboardSaveFetchParams(params: QueryParams, remove?: boolean): DashboardInfo { |
| 132 | + const searchParams = urlEncode(params); |
| 133 | + const oldDashboardParams = dashboardMigrateSaveToOld(params); |
| 134 | + oldDashboardParams.dashboard.data.searchParams = searchParams; |
| 135 | + const dashboardParams: DashboardInfo = { |
| 136 | + dashboard: { |
| 137 | + name: params.dashboardName, |
| 138 | + description: params.dashboardDescription, |
| 139 | + version: params.dashboardVersion ?? 0, |
| 140 | + dashboard_id: toNumber(params.dashboardId) ?? undefined, |
| 141 | + data: { |
| 142 | + ...oldDashboardParams.dashboard.data, |
| 143 | + searchParams, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }; |
| 147 | + if (remove) { |
| 148 | + dashboardParams.delete_mark = true; |
| 149 | + } |
| 150 | + return dashboardParams; |
| 151 | +} |
| 152 | + |
| 153 | +export function getDashboardSaveOptions( |
| 154 | + queryClient: QueryClient, |
| 155 | + remove?: boolean |
| 156 | +): UseMutationOptions<ApiDashboard, Error, QueryParams, unknown> { |
| 157 | + return { |
| 158 | + retry: false, |
| 159 | + mutationFn: async (params: QueryParams) => { |
| 160 | + const dashboardParams: DashboardInfo = getDashboardSaveFetchParams(params, remove); |
| 161 | + const { response, error } = await apiDashboardSaveFetch(dashboardParams); |
| 162 | + if (error) { |
| 163 | + throw error; |
| 164 | + } |
| 165 | + if (!response) { |
| 166 | + throw new ExtendedError('empty response'); |
| 167 | + } |
| 168 | + return response; |
| 169 | + }, |
| 170 | + onSuccess: (data, params) => { |
| 171 | + if (data.data.dashboard.dashboard_id) { |
| 172 | + const fetchParams: ApiDashboardGet = { [GET_PARAMS.dashboardID]: data.data.dashboard.dashboard_id.toString() }; |
| 173 | + if (data.data.dashboard.version != null) { |
| 174 | + fetchParams[GET_PARAMS.dashboardApiVersion] = data.data.dashboard.version.toString(); |
| 175 | + } |
| 176 | + queryClient.setQueryData([ApiDashboardEndpoint, fetchParams], data); |
| 177 | + } |
| 178 | + }, |
| 179 | + }; |
| 180 | +} |
| 181 | + |
| 182 | +export async function apiDashboardSave(params: QueryParams, remove?: boolean): Promise<ApiFetchResponse<ApiDashboard>> { |
| 183 | + const options = getDashboardSaveOptions(queryClient, remove); |
| 184 | + const result: ApiFetchResponse<ApiDashboard> = { ok: false, status: 0 }; |
| 185 | + try { |
| 186 | + result.response = await options.mutationFn?.(params); |
| 187 | + if (result.response) { |
| 188 | + result.ok = true; |
| 189 | + options.onSuccess?.(result.response, params, undefined); |
| 190 | + } else { |
| 191 | + result.error = new ExtendedError('empty response'); |
| 192 | + result.status = ExtendedError.ERROR_STATUS_UNKNOWN; |
| 193 | + } |
| 194 | + } catch (error) { |
| 195 | + result.status = ExtendedError.ERROR_STATUS_UNKNOWN; |
| 196 | + if (error instanceof ExtendedError) { |
| 197 | + result.error = error; |
| 198 | + result.status = error.status; |
| 199 | + } else if (error instanceof CancelledError) { |
| 200 | + result.error = new ExtendedError(error, ExtendedError.ERROR_STATUS_ABORT); |
| 201 | + result.status = ExtendedError.ERROR_STATUS_ABORT; |
| 202 | + } else { |
| 203 | + result.error = new ExtendedError(error); |
| 204 | + } |
| 205 | + } |
| 206 | + return result; |
| 207 | +} |
| 208 | + |
| 209 | +export function useApiDashboardSave(remove?: boolean) { |
| 210 | + const queryClient = useQueryClient(); |
| 211 | + return useMutation(getDashboardSaveOptions(queryClient, remove)); |
| 212 | +} |
0 commit comments