Skip to content

Commit 6c5da8d

Browse files
committed
- Update to use consoleFetch instead of native fetch
- Restructure files and names of vars
1 parent fe7041f commit 6c5da8d

File tree

4 files changed

+57
-61
lines changed

4 files changed

+57
-61
lines changed

.gitignore

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@ web/node_modules/
1010
plugin-backend
1111
# adding these while the PR is in review. This should be removed after
1212
node_modules/
13-
dist/
14-
vendor
13+
dist/
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { DashboardResource } from '@perses-dev/core';
2+
import buildURL from './perses/url-builder';
3+
import { consoleFetch } from '@openshift-console/dynamic-plugin-sdk';
4+
import { useMutation, UseMutationResult, useQueryClient } from '@tanstack/react-query';
5+
6+
const resource = 'dashboards';
7+
8+
const updateDashboard = async (entity: DashboardResource): Promise<DashboardResource> => {
9+
const HTTPMethodPUT = 'PUT';
10+
const HTTPHeader: Record<string, string> = {
11+
'Content-Type': 'application/json',
12+
Accept: 'application/json',
13+
};
14+
const url = buildURL({
15+
resource: resource,
16+
project: entity.metadata.project,
17+
name: entity.metadata.name,
18+
});
19+
20+
const response = await consoleFetch(url, {
21+
method: HTTPMethodPUT,
22+
headers: {
23+
...HTTPHeader,
24+
},
25+
body: JSON.stringify(entity),
26+
});
27+
28+
if (!response.ok) {
29+
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
30+
}
31+
32+
return response.json();
33+
};
34+
35+
export const useUpdateDashboardMutation = (): UseMutationResult<
36+
DashboardResource,
37+
Error,
38+
DashboardResource
39+
> => {
40+
const queryClient = useQueryClient();
41+
42+
return useMutation<DashboardResource, Error, DashboardResource>({
43+
mutationKey: [resource],
44+
mutationFn: (dashboard) => {
45+
return updateDashboard(dashboard);
46+
},
47+
onSuccess: () => {
48+
return queryClient.invalidateQueries({ queryKey: [resource] });
49+
},
50+
});
51+
};

web/src/components/dashboards/perses/dashboard-list.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,13 @@ const perPageOptions = [
3232
{ title: '20', value: 20 },
3333
];
3434

35-
interface DashboardName {
35+
interface DashboardRowNameLink {
3636
link: ReactNode;
3737
label: string;
3838
}
3939

4040
interface DashboardRow {
41-
name: DashboardName;
41+
name: DashboardRowNameLink;
4242
project: string;
4343
created: ReactNode;
4444
modified: ReactNode;
@@ -153,7 +153,7 @@ const DashboardsTable: React.FunctionComponent<DashboardsTableProps> = ({
153153
return persesDashboards.map((board) => {
154154
const metadata = board?.metadata;
155155
const dashboardsParams = `?dashboard=${metadata?.name}&project=${metadata?.project}`;
156-
const dashboardName: DashboardName = {
156+
const dashboardName: DashboardRowNameLink = {
157157
link: (
158158
<Link
159159
to={`${dashboardBaseURL}${dashboardsParams}`}

web/src/components/dashboards/perses/dashboard-page.tsx

Lines changed: 2 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,7 @@
11
import { useCallback, type FC } from 'react';
22
import { useTranslation } from 'react-i18next';
33
import { useSearchParams } from 'react-router-dom-v5-compat';
4-
import {
5-
QueryClient,
6-
QueryClientProvider,
7-
useMutation,
8-
UseMutationResult,
9-
useQueryClient,
10-
} from '@tanstack/react-query';
4+
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
115
import { QueryParamProvider } from 'use-query-params';
126
import { ReactRouter5Adapter } from 'use-query-params/adapters/react-router-5';
137
import { DashboardLayout } from './dashboard-layout';
@@ -20,9 +14,8 @@ import {
2014
EphemeralDashboardResource,
2115
getResourceExtendedDisplayName,
2216
} from '@perses-dev/core';
23-
import buildURL from './perses/url-builder';
24-
import { getCSRFToken } from '@openshift-console/dynamic-plugin-sdk/lib/utils/fetch/console-fetch-utils';
2517
import { useSnackbar } from '@perses-dev/components';
18+
import { useUpdateDashboardMutation } from './dashboard-api';
2619

2720
const queryClient = new QueryClient({
2821
defaultOptions: {
@@ -46,53 +39,6 @@ const DashboardPage_: FC = () => {
4639
combinedInitialLoad,
4740
} = useDashboardsData();
4841

49-
const resource = 'dashboards';
50-
const HTTPMethodPUT = 'PUT';
51-
const HTTPHeader: Record<string, string> = {
52-
'Content-Type': 'application/json',
53-
Accept: 'application/json',
54-
};
55-
async function updateDashboard(entity: DashboardResource): Promise<DashboardResource> {
56-
const url = buildURL({
57-
resource: resource,
58-
project: entity.metadata.project,
59-
name: entity.metadata.name,
60-
});
61-
62-
const response = await fetch(url, {
63-
method: HTTPMethodPUT,
64-
headers: {
65-
...HTTPHeader,
66-
'X-CSRFToken': getCSRFToken(),
67-
},
68-
body: JSON.stringify(entity),
69-
});
70-
71-
if (!response.ok) {
72-
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
73-
}
74-
75-
return response.json();
76-
}
77-
78-
function useUpdateDashboardMutation(): UseMutationResult<
79-
DashboardResource,
80-
Error,
81-
DashboardResource
82-
> {
83-
const queryClient = useQueryClient();
84-
85-
return useMutation<DashboardResource, Error, DashboardResource>({
86-
mutationKey: [resource],
87-
mutationFn: (dashboard) => {
88-
return updateDashboard(dashboard);
89-
},
90-
onSuccess: () => {
91-
return queryClient.invalidateQueries({ queryKey: [resource] });
92-
},
93-
});
94-
}
95-
9642
const updateDashboardMutation = useUpdateDashboardMutation();
9743
const { successSnackbar, exceptionSnackbar } = useSnackbar();
9844

0 commit comments

Comments
 (0)