-
Notifications
You must be signed in to change notification settings - Fork 76
Expand file tree
/
Copy pathuseAPI.ts
More file actions
93 lines (81 loc) · 2.57 KB
/
Copy pathuseAPI.ts
File metadata and controls
93 lines (81 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { getFetchClient } from '@strapi/strapi/admin';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import { getApiClient } from '../../../api';
import { resolveGlobalLikeId } from '../utils';
import { UiFormSchema } from '../schemas';
export const useConfig = () => {
// TODO: Resolve useQuery issues to use useFetchClient
const fetch = getFetchClient();
const apiClient = getApiClient(fetch);
return useQuery({
queryKey: apiClient.readSettingsConfigIndex(),
queryFn() {
return apiClient.readSettingsConfig();
},
staleTime: 1000 * 60 * 5,
});
};
export const useRestart = () => {
const fetch = getFetchClient();
const apiClient = getApiClient(fetch);
const healthCheck = useQuery({
queryKey: apiClient.healthCheckIndex(),
queryFn: () => apiClient.healthCheck(),
retry: true,
retryDelay: 1000 * 5,
enabled: false,
});
return useMutation({
mutationFn: () => {
return apiClient.restart().then(() => healthCheck.refetch());
},
});
};
export const useRestoreConfig = () => {
const fetch = getFetchClient();
const apiClient = getApiClient(fetch);
return useMutation({
mutationFn: () => apiClient.restoreConfig(),
});
};
export const useContentTypes = () => {
const fetch = getFetchClient();
const apiClient = getApiClient(fetch);
return useQuery({
queryKey: apiClient.readContentTypeIndex(),
queryFn: () => apiClient.readContentType(),
staleTime: Infinity,
});
};
export const useSaveConfig = () => {
const fetch = getFetchClient();
const apiClient = getApiClient(fetch);
const queryClient = useQueryClient();
return useMutation({
mutationFn(data: UiFormSchema) {
return apiClient.updateConfig({
...data,
contentTypesNameFields: Object.fromEntries(
data.contentTypesNameFields.map(({ key, fields }) => [key, fields])
),
contentTypesPopulate: Object.fromEntries(
data.contentTypesPopulate.map(({ key, fields }) => [key, fields])
),
pathDefaultFields: Object.fromEntries(
data.pathDefaultFields.map(({ key, fields }) => [key, fields])
),
additionalFields: data.audienceFieldChecked
? [...data.additionalFields, 'audience']
: data.additionalFields,
gql: {
navigationItemRelated: data.contentTypes.map((uid: string) => {
return resolveGlobalLikeId(uid);
}),
},
});
},
onSuccess() {
queryClient.invalidateQueries({ queryKey: apiClient.readSettingsConfigIndex() });
},
});
};