@@ -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' ;
1511import { PenIcon } from 'lucide-react' ;
1612
1713import { 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
2216const 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-
7937export 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
0 commit comments