Skip to content

Commit eefa819

Browse files
committed
refactor: 유실물 등록/수정 요청 useMutation으로 적용 (#258)
1 parent 589614c commit eefa819

File tree

2 files changed

+43
-20
lines changed

2 files changed

+43
-20
lines changed

services/one-app/src/app/(site)/lost-found/_components/LostFoundForm.tsx

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import useFormAdapter from '../_hook/useFormAdapter';
1313
import useFormImage from '../_hook/useFormImage';
1414
import useFormInitialize from '../_hook/useFormInitialize';
1515
import createFormData from '@/lib/createFormData';
16+
import { usePostLostFound } from '../_lib/post';
1617

1718
type Props = {
1819
lostId?: string | null;
@@ -24,6 +25,10 @@ const LostFoundForm = ({ lostId = null }: Props) => {
2425
lostId: lostId ?? '',
2526
});
2627

28+
const { mutate: lostFoundMutate } = usePostLostFound((id: string) => {
29+
router.push(`/lost-found/${id}`);
30+
});
31+
2732
const { images, setImages, removeImageIds, handleFileChange, onDeleteImage } =
2833
useFormImage();
2934

@@ -53,26 +58,7 @@ const LostFoundForm = ({ lostId = null }: Props) => {
5358
image.data !== null ? [image.data] : [],
5459
),
5560
});
56-
57-
try {
58-
const res = await apiClient.post(
59-
`/lost-posts/${lostId || ''}`,
60-
postData,
61-
{
62-
headers: {
63-
'Content-Type': 'multipart/form-data',
64-
},
65-
},
66-
);
67-
const { code, result } = res?.data;
68-
69-
if (code === '100') {
70-
router.push(`/lost-found/${result.id}`);
71-
}
72-
} catch (error) {
73-
// Todo - 에러 팝업 추가 필요
74-
alert('유실물 등록 에러 발생');
75-
}
61+
lostFoundMutate({ lostId, postData });
7662
};
7763

7864
const onChangeEditorContent = (editorState: EditorState | null) => {
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { apiClient } from '@/app/api';
2+
import { useMutation } from '@tanstack/react-query';
3+
4+
import type { ErrorCode } from '@/model/Error';
5+
6+
type PostLostFoundParams = {
7+
lostId: string | null;
8+
postData: FormData;
9+
};
10+
11+
const postLostFound = ({ lostId, postData }: PostLostFoundParams) =>
12+
apiClient.post(`/lost-posts/${lostId || ''}`, postData, {
13+
headers: {
14+
'Content-Type': 'multipart/form-data',
15+
},
16+
});
17+
18+
export const usePostLostFound = (successCallback: (id: string) => void) => {
19+
return useMutation({
20+
mutationFn: (params: PostLostFoundParams) => postLostFound(params),
21+
onError: () => {
22+
alert('유실물 등록 에러 발생');
23+
},
24+
onSuccess: (res: { data: { code: ErrorCode; result: { id: string } } }) => {
25+
const {
26+
data: {
27+
code,
28+
result: { id },
29+
},
30+
} = res;
31+
32+
if (code === '100') {
33+
successCallback(id);
34+
}
35+
},
36+
});
37+
};

0 commit comments

Comments
 (0)