From 1da03c0c427ab403e5c91d151bf39d6e000ab3c4 Mon Sep 17 00:00:00 2001 From: Maxime Perrault Date: Thu, 24 Oct 2024 11:46:18 +0200 Subject: [PATCH] feat: add tags to dashboard api to manage api refresh calls --- frontend/src/api/dashboardsAPI.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/frontend/src/api/dashboardsAPI.ts b/frontend/src/api/dashboardsAPI.ts index 8b8d63199..dafb5145a 100644 --- a/frontend/src/api/dashboardsAPI.ts +++ b/frontend/src/api/dashboardsAPI.ts @@ -14,20 +14,28 @@ const GET_DASHBOARD_ERROR_MESSAGE = "Nous n'avons pas pu récupérer le tableau export const dashboardsAPI = monitorenvPrivateApi.injectEndpoints({ endpoints: build => ({ getDashboard: build.query({ + providesTags: (_, __, id) => [{ id, type: 'Dashboards' }], query: id => `/v1/dashboards/${id}`, transformErrorResponse: response => new FrontendApiError(GET_DASHBOARD_ERROR_MESSAGE, response), transformResponse: (response: Dashboard.DashboardFromApi) => response }), - getDashboards: build.query({ + getDashboards: build.query({ + providesTags: result => + result + ? // successful query + [...result.map(({ id }) => ({ id, type: 'Dashboards' as const })), { id: 'LIST', type: 'Dashboards' }] + : // an error occurred, but we still want to refetch this query when `{ type: 'Missions', id: 'LIST' }` is invalidated + [{ id: 'LIST', type: 'Dashboards' }], query: () => '/v1/dashboards', transformErrorResponse: response => new FrontendApiError(GET_DASHBOARDS_ERROR_MESSAGE, response), - transformResponse: (response: Dashboard.Dashboard[]) => response + transformResponse: (response: Dashboard.DashboardFromApi[]) => response }), getExtratedArea: build.query({ query: geometry => `/v1/dashboards/extract?geometry=${geoJsonToWKT(geometry)}`, transformErrorResponse: response => new FrontendApiError(GET_EXTRACTED_AREAS_ERROR_MESSAGE, response) }), - save: build.query({ + save: build.mutation({ + invalidatesTags: (_, __, { id }) => [{ id, type: 'Dashboards' }], query: mission => ({ body: mission, method: 'PUT',