Skip to content

Commit cc77285

Browse files
committed
feat: BackendAdminAPI 추가
1 parent 6064155 commit cc77285

File tree

7 files changed

+232
-1
lines changed

7 files changed

+232
-1
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
import BackendAdminAPISchemas from '../schemas/backendAdminAPI';
2+
import { BackendAPIClient } from "./client";
3+
4+
namespace BackendAdminAPIs {
5+
export const me = (client: BackendAPIClient) =>
6+
async () => {
7+
try {
8+
return await client.get<BackendAdminAPISchemas.UserSchema>("v1/admin-api/user/userext/me/")
9+
} catch (error) {
10+
return null;
11+
}
12+
};
13+
14+
export const signIn =
15+
(client: BackendAPIClient) =>
16+
(data: BackendAdminAPISchemas.UserSignInSchema) =>
17+
client.post<BackendAdminAPISchemas.UserSchema, BackendAdminAPISchemas.UserSignInSchema>("v1/admin-api/user/userext/signin/", data);
18+
19+
export const signOut = (client: BackendAPIClient) => () => client.delete<void>("v1/admin-api/user/userext/signout/");
20+
21+
export const list =
22+
<T>(client: BackendAPIClient, app: string, resource: string) =>
23+
() => client.get<T[]>(`v1/admin-api/${app}/${resource}/`);
24+
25+
export const retrieve =
26+
<T>(client: BackendAPIClient, app: string, resource: string, id: string) =>
27+
() => client.get<T>(`v1/admin-api/${app}/${resource}/${id}/`);
28+
29+
export const create =
30+
<T>(client: BackendAPIClient, app: string, resource: string) =>
31+
(data: T) => client.post<Omit<T, "id">, T>(`v1/admin-api/${app}/${resource}/`, data);
32+
33+
export const update =
34+
<T>(client: BackendAPIClient, app: string, resource: string, id: string) =>
35+
(data: Omit<T, "id">) => client.patch<T, Omit<T, "id">>(`v1/admin-api/${app}/${resource}/${id}/`, data);
36+
37+
export const remove =
38+
(client: BackendAPIClient, app: string, resource: string, id: string) =>
39+
() => client.delete<void>(`v1/admin-api/${app}/${resource}/${id}/`);
40+
41+
export const schema =
42+
(client: BackendAPIClient, app: string, resource: string) =>
43+
() => client.get<BackendAdminAPISchemas.AdminSchemaDefinition>(`v1/admin-api/${app}/${resource}/json-schema/`);
44+
45+
export const uploadPublicFile =
46+
(client: BackendAPIClient) =>
47+
(file: File) => {
48+
const formData = new FormData();
49+
formData.append("file", file);
50+
return client.post<BackendAdminAPISchemas.PublicFileSchema, FormData>(
51+
`v1/admin-api/file/publicfile/upload/`,
52+
formData,
53+
{ headers: { "Content-Type": "multipart/form-data" } },
54+
);
55+
};
56+
57+
export const listSections =
58+
(client: BackendAPIClient, pageId: string) =>
59+
() => {
60+
if (!pageId) return Promise.resolve([]);
61+
return client.get<BackendAdminAPISchemas.PageSectionSchema[]>(`v1/admin-api/cms/page/${pageId}/section/`)
62+
};
63+
64+
export const bulkUpdateSections =
65+
(client: BackendAPIClient, pageId: string) =>
66+
(data: {
67+
sections: BackendAdminAPISchemas.PageSectionBulkUpdateSchema[];
68+
}) =>
69+
client.put<BackendAdminAPISchemas.PageSectionSchema[], { sections: BackendAdminAPISchemas.PageSectionBulkUpdateSchema[] }>(
70+
`v1/admin-api/cms/page/${pageId}/section/bulk-update/`, data
71+
);
72+
}
73+
74+
export default BackendAdminAPIs;

packages/common/src/contexts/index.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,25 @@
1-
import { MDXComponents } from 'mdx/types';
21
import * as React from "react";
32

3+
import { MDXComponents } from 'mdx/types';
4+
45
namespace GlobalContext {
56
export type ContextOptions = {
7+
frontendDomain?: string;
68
baseUrl: string;
79
debug?: boolean;
810
backendApiDomain: string;
911
backendApiTimeout: number;
12+
backendApiCSRFCookieName?: string;
1013
mdxComponents?: MDXComponents;
1114
}
1215

1316
export const context = React.createContext<ContextOptions>({
17+
frontendDomain: "",
1418
baseUrl: "",
1519
debug: false,
1620
backendApiDomain: "",
1721
backendApiTimeout: 10000,
22+
backendApiCSRFCookieName: "",
1823
});
1924
}
2025

packages/common/src/hooks/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import BackendAPIHooks from './useAPI';
2+
import BackendAdminAPIHooks from './useAdminAPI';
23
import { useCommonContext as useCommonContextHook } from './useCommonContext';
34
import { useEmail as useEmailHook } from './useEmail';
45

@@ -10,6 +11,7 @@ export namespace CommonHooks {
1011
namespace Hooks {
1112
export const Common = CommonHooks;
1213
export const BackendAPI = BackendAPIHooks;
14+
export const BackendAdminAPI = BackendAdminAPIHooks;
1315
}
1416

1517
export default Hooks;
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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;

packages/common/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
export { default as BackendAdminAPIs } from "./apis/admin_api";
2+
export { default as BackendAPIs } from "./apis/index";
13
export { default as Components } from "./components/index";
24
export { default as Contexts } from "./contexts/index";
35
export { default as Hooks } from "./hooks/index";
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
2+
import { RJSFSchema, UiSchema } from "@rjsf/utils";
3+
4+
namespace BackendAdminAPISchemas {
5+
export type DetailedErrorSchema = {
6+
code: string;
7+
detail: string;
8+
attr: string | null;
9+
};
10+
11+
export type ErrorResponseSchema = {
12+
type: string;
13+
errors: DetailedErrorSchema[];
14+
};
15+
16+
export type AdminSchemaDefinition = {
17+
schema: RJSFSchema;
18+
ui_schema: UiSchema;
19+
translation_fields: { [key: string]: string };
20+
};
21+
22+
export type UserSchema = {
23+
id: number;
24+
username: string;
25+
email: string;
26+
first_name: string;
27+
last_name: string;
28+
is_staff: boolean;
29+
is_active: boolean;
30+
date_joined: string; // ISO 8601 format
31+
};
32+
33+
export type UserSignInSchema = {
34+
identity: string; // username or email
35+
password: string;
36+
};
37+
38+
export type PublicFileSchema = {
39+
id: string; // UUID
40+
file: string; // URL to the public file
41+
mimetype: string | null; // MIME type of the file
42+
hash: string; // Hash of the file for integrity check
43+
size: number; // Size of the file in bytes
44+
};
45+
46+
export type PageSectionSchema = {
47+
id?: string;
48+
order: number;
49+
css: string;
50+
body_ko: string | null;
51+
body_en: string | null;
52+
};
53+
54+
export type PageSectionBulkUpdateSchema = PageSectionSchema | Omit<PageSectionSchema, "id">;
55+
56+
export const _dummy = (_1: PageSectionSchema[]) => console.log("This is a dummy function to ensure the module is not empty.");
57+
}
58+
59+
export default BackendAdminAPISchemas;

packages/common/src/schemas/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import * as _BackendAdminAPISchemas from "./backendAdminAPI";
33

44
namespace CommonSchemas {
55
export const BackendAPI = _BackendAPISchemas;
6+
export const BackendAdminAPI = _BackendAdminAPISchemas;
67
}
78

89
export default CommonSchemas;

0 commit comments

Comments
 (0)