Skip to content

Commit

Permalink
refactor(usePostUploadImages): max width, height 값 받을 수 있도록 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
simorimi committed Oct 20, 2024
1 parent 7e6ca5e commit f318d32
Showing 1 changed file with 32 additions and 22 deletions.
54 changes: 32 additions & 22 deletions frontend/src/queries/usePostUploadImages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,40 @@ import { ERROR_MESSAGE_MAP } from "@constants/errorMessage";

import resizeAndConvertImage from "@utils/resizeAndConvertImage";

interface MutationFnVariables {
files: File[];
maxWidth?: number;
maxHeight?: number;
}

export const usePostUploadImages = () => {
const { isPaused, ...rest } = useMutation<string[], ApiError | AxiosError<ErrorResponse>, File[]>(
{
mutationFn: async (files: File[]) => {
const processedFiles = await Promise.all(files.map((file) => resizeAndConvertImage(file)));
const formData = new FormData();

processedFiles.forEach((file) => {
formData.append("files", file);
});

const response = await authClient.post(API_ENDPOINT_MAP.image, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
});

return response.data;
},
onError: (error) => {
alert(error.message);
},
const { isPaused, ...rest } = useMutation<
string[],
ApiError | AxiosError<ErrorResponse>,
MutationFnVariables
>({
mutationFn: async ({ files, maxWidth, maxHeight }: MutationFnVariables) => {
const processedFiles = await Promise.all(
files.map((file) => resizeAndConvertImage(file, maxWidth, maxHeight)),
);
const formData = new FormData();

processedFiles.forEach((file) => {
formData.append("files", file);
});

const response = await authClient.post(API_ENDPOINT_MAP.image, formData, {
headers: {
"Content-Type": "multipart/form-data",
},
});

return response.data;
},
onError: (error) => {
alert(error.message);
},
);
});

if (isPaused) alert(ERROR_MESSAGE_MAP.network);

Expand Down

0 comments on commit f318d32

Please sign in to comment.