Skip to content

Commit 90d7769

Browse files
author
kim
committed
refactor: use mutation from apps query client
1 parent 794b970 commit 90d7769

File tree

3 files changed

+12
-50
lines changed

3 files changed

+12
-50
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
"@codemirror/lang-javascript": "6.2.4",
1515
"@emotion/react": "11.14.0",
1616
"@emotion/styled": "11.14.1",
17-
"@graasp/apps-query-client": "3.8.0",
17+
"@graasp/apps-query-client": "github:graasp/graasp-apps-query-client#upload-app-setting",
1818
"@graasp/sdk": "5.16.5",
1919
"@graasp/ui": "5.5.4",
2020
"@mui/icons-material": "6.5.0",

src/modules/settings/chatbot/ChatbotAvatarEditor.tsx

Lines changed: 7 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,10 @@ import {
88
styled,
99
} from '@mui/material';
1010

11-
import { useLocalContext } from '@graasp/apps-query-client';
12-
13-
import { useMutation } from '@tanstack/react-query';
14-
import axios from 'axios';
1511
import { PenIcon } from 'lucide-react';
1612

1713
import { CHATBOT_THUMBNAIL_APP_SETTING_NAME } from '@/config/appSetting';
18-
import { API_ROUTES, hooks } from '@/config/queryClient';
19-
20-
const { buildUploadAppSettingFilesRoute } = API_ROUTES;
14+
import { hooks, mutations } from '@/config/queryClient';
2115

2216
const VisuallyHiddenInput = styled('input')({
2317
clip: 'rect(0 0 0 0)',
@@ -40,45 +34,10 @@ const EditButton = styled(IconButton)(() => ({
4034
},
4135
}));
4236

43-
export const useUploadThumbnail = () => {
44-
const { itemId, apiHost } = useLocalContext();
45-
const { data: token } = hooks.useAuthToken(itemId);
46-
return useMutation({
47-
mutationFn: async (args: { file: Blob }) => {
48-
const payload = new FormData();
49-
50-
/* WARNING: this file field needs to be the last one,
51-
* otherwise the normal fields can not be read
52-
* https://github.com/fastify/fastify-multipart?tab=readme-ov-file#usage
53-
*/
54-
payload.append('name', CHATBOT_THUMBNAIL_APP_SETTING_NAME);
55-
payload.append('files', args.file);
56-
57-
await axios.post(
58-
`${apiHost}/${buildUploadAppSettingFilesRoute(itemId)}`,
59-
payload,
60-
{
61-
// formData: true,
62-
// allowedMetaFields: ['name'],
63-
headers: {
64-
'Content-Type': 'multipart/form-data',
65-
authorization: `Bearer ${token}`,
66-
},
67-
},
68-
);
69-
},
70-
onSuccess: () => {
71-
console.debug('success');
72-
},
73-
onError: (error: Error) => {
74-
console.error(error);
75-
},
76-
});
77-
};
78-
7937
export function ChatbotAvatarEditor() {
8038
const fileInput = useRef<HTMLInputElement>(null);
81-
const { mutate: uploadThumbnail, isPending } = useUploadThumbnail();
39+
const { mutate: uploadThumbnail, isPending } =
40+
mutations.useUploadAppSettingFile();
8241
const { data: appSettings } = hooks.useAppSettings({
8342
name: CHATBOT_THUMBNAIL_APP_SETTING_NAME,
8443
});
@@ -92,7 +51,10 @@ export function ChatbotAvatarEditor() {
9251

9352
const onChange: ChangeEventHandler<HTMLInputElement> = (e) => {
9453
if (e.target.files) {
95-
uploadThumbnail({ file: e.target.files[0] });
54+
uploadThumbnail({
55+
file: e.target.files[0],
56+
name: CHATBOT_THUMBNAIL_APP_SETTING_NAME,
57+
});
9658
}
9759
};
9860

yarn.lock

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2351,9 +2351,9 @@ __metadata:
23512351
languageName: node
23522352
linkType: hard
23532353

2354-
"@graasp/apps-query-client@npm:3.8.0":
2354+
"@graasp/apps-query-client@github:graasp/graasp-apps-query-client#upload-app-setting":
23552355
version: 3.8.0
2356-
resolution: "@graasp/apps-query-client@npm:3.8.0"
2356+
resolution: "@graasp/apps-query-client@https://github.com/graasp/graasp-apps-query-client.git#commit=eb2e6ca2b6b615c06560dfc8a1560347c78cd8f7"
23572357
dependencies:
23582358
"@emotion/react": "npm:11.14.0"
23592359
"@emotion/styled": "npm:11.14.1"
@@ -2375,7 +2375,7 @@ __metadata:
23752375
date-fns: ^3.3.0 || ^4.0.0
23762376
react: ^18.0.0 || ^19.0.0
23772377
react-dom: ^18.0.0 || ^19.0.0
2378-
checksum: 10/1778df7c7d5bb9722b97fa014f12553dd0c09b511c138ca3185fa1dbb97a93d62f2d40f650fde73d7be61e61d549bbd14553dddc6f9b49698399dfc66229a266
2378+
checksum: 10/fc2f6658f6f5d53872adb6aac2747a98f4de0b81afeb6b9c7f5c6a06132438f5ba857e8ac785b7fd3017f5d2610cee8673f29140c6555f48e31620a456f8593d
23792379
languageName: node
23802380
linkType: hard
23812381

@@ -7753,7 +7753,7 @@ __metadata:
77537753
"@eslint/compat": "npm:1.4.1"
77547754
"@eslint/eslintrc": "npm:3.3.1"
77557755
"@eslint/js": "npm:9.39.1"
7756-
"@graasp/apps-query-client": "npm:3.8.0"
7756+
"@graasp/apps-query-client": "github:graasp/graasp-apps-query-client#upload-app-setting"
77577757
"@graasp/sdk": "npm:5.16.5"
77587758
"@graasp/ui": "npm:5.5.4"
77597759
"@mui/icons-material": "npm:6.5.0"

0 commit comments

Comments
 (0)