|
| 1 | +import { useMutation, useSuspenseQuery } from "@tanstack/react-query"; |
| 2 | + |
| 3 | +import BackendAdminAPIs from '../apis/admin_api'; |
| 4 | +import { BackendAPIClient } from '../apis/client'; |
| 5 | +import BackendAPIHooks from './useAPI'; |
| 6 | + |
| 7 | +const QUERY_KEYS = { |
| 8 | + ADMIN_ME: ["query", "admin", "me"], |
| 9 | + ADMIN_LIST: ["query", "admin", "list"], |
| 10 | + ADMIN_RETRIEVE: ["query", "admin", "retrieve"], |
| 11 | + ADMIN_SCHEMA: ["query", "admin", "schema"], |
| 12 | +}; |
| 13 | + |
| 14 | +const MUTATION_KEYS = { |
| 15 | + ADMIN_SIGN_IN: ["mutation", "admin", "sign-in"], |
| 16 | + ADMIN_CREATE: ["mutation", "admin", "create"], |
| 17 | + ADMIN_UPDATE: ["mutation", "admin", "update"], |
| 18 | + ADMIN_REMOVE: ["mutation", "admin", "remove"], |
| 19 | +} |
| 20 | + |
| 21 | +namespace BackendAdminAPIHooks { |
| 22 | + export const useBackendAdminClient = () => { |
| 23 | + const { backendApiDomain, backendApiTimeout, backendApiCSRFCookieName } = BackendAPIHooks.useBackendContext(); |
| 24 | + return new BackendAPIClient(backendApiDomain, backendApiTimeout, backendApiCSRFCookieName, true); |
| 25 | + } |
| 26 | + |
| 27 | + export const useSignedInUserQuery = (client: BackendAPIClient) => useSuspenseQuery({ |
| 28 | + queryKey: QUERY_KEYS.ADMIN_ME, |
| 29 | + queryFn: BackendAdminAPIs.me(client), |
| 30 | + }); |
| 31 | + |
| 32 | + export const useSignInMutation = (client: BackendAPIClient) => useMutation({ |
| 33 | + mutationKey: [ ...MUTATION_KEYS.ADMIN_SIGN_IN ], |
| 34 | + mutationFn: BackendAdminAPIs.signIn(client), |
| 35 | + }) |
| 36 | + |
| 37 | + export const useSignOutMutation = (client: BackendAPIClient) => useMutation({ |
| 38 | + mutationKey: [ ...MUTATION_KEYS.ADMIN_SIGN_IN, "sign-out" ], |
| 39 | + mutationFn: BackendAdminAPIs.signOut(client), |
| 40 | + }); |
| 41 | + |
| 42 | + export const useSchemaQuery = (client: BackendAPIClient, app: string, resource: string) => useSuspenseQuery({ |
| 43 | + queryKey: [ ...QUERY_KEYS.ADMIN_SCHEMA, app, resource], |
| 44 | + queryFn: BackendAdminAPIs.schema(client, app, resource), |
| 45 | + }); |
| 46 | + |
| 47 | + export const useListQuery = <T>(client: BackendAPIClient, app: string, resource: string) => useSuspenseQuery({ |
| 48 | + queryKey: [ ...QUERY_KEYS.ADMIN_LIST, app, resource], |
| 49 | + queryFn: BackendAdminAPIs.list<T>(client, app, resource), |
| 50 | + }); |
| 51 | + |
| 52 | + export const useRetrieveQuery = <T>(client: BackendAPIClient, app: string, resource: string, id: string) => useSuspenseQuery({ |
| 53 | + queryKey: [ ...QUERY_KEYS.ADMIN_RETRIEVE, app, resource, id], |
| 54 | + queryFn: BackendAdminAPIs.retrieve<T>(client, app, resource, id), |
| 55 | + }); |
| 56 | + |
| 57 | + export const useCreateMutation = <T>(client: BackendAPIClient, app: string, resource: string) => useMutation({ |
| 58 | + mutationKey: [ ...MUTATION_KEYS.ADMIN_CREATE, app, resource], |
| 59 | + mutationFn: BackendAdminAPIs.create<T>(client, app, resource), |
| 60 | + }); |
| 61 | + |
| 62 | + export const useUpdateMutation = <T>(client: BackendAPIClient, app: string, resource: string, id: string) => useMutation({ |
| 63 | + mutationKey: [ ...MUTATION_KEYS.ADMIN_UPDATE, app, resource, id], |
| 64 | + mutationFn: BackendAdminAPIs.update<T>(client, app, resource, id), |
| 65 | + }); |
| 66 | + |
| 67 | + export const useRemoveMutation = (client: BackendAPIClient, app: string, resource: string, id: string) => useMutation({ |
| 68 | + mutationKey: [ ...MUTATION_KEYS.ADMIN_REMOVE, app, resource, id], |
| 69 | + mutationFn: BackendAdminAPIs.remove(client, app, resource, id), |
| 70 | + }); |
| 71 | + |
| 72 | + export const useUploadPublicFileMutation = (client: BackendAPIClient) => useMutation({ |
| 73 | + mutationKey: [ ...MUTATION_KEYS.ADMIN_CREATE, "public-file", "upload" ], |
| 74 | + mutationFn: BackendAdminAPIs.uploadPublicFile(client), |
| 75 | + }); |
| 76 | + |
| 77 | + export const useListPageSectionsQuery = (client: BackendAPIClient, pageId: string) => useSuspenseQuery({ |
| 78 | + queryKey: [ ...QUERY_KEYS.ADMIN_LIST, "cms", "page", pageId, "section" ], |
| 79 | + queryFn: BackendAdminAPIs.listSections(client, pageId), |
| 80 | + }); |
| 81 | + |
| 82 | + export const useBulkUpdatePageSectionsMutation = (client: BackendAPIClient, pageId: string) => useMutation({ |
| 83 | + mutationKey: [ ...MUTATION_KEYS.ADMIN_UPDATE, "cms", "page", pageId, "section" ], |
| 84 | + mutationFn: BackendAdminAPIs.bulkUpdateSections(client, pageId), |
| 85 | + }); |
| 86 | +} |
| 87 | + |
| 88 | +export default BackendAdminAPIHooks; |
0 commit comments