diff --git a/src/components/LetterDetailPage/Delete/DeleteModal.tsx b/src/components/LetterDetailPage/Delete/DeleteModal.tsx index 350dd2f8..6a76f79b 100644 --- a/src/components/LetterDetailPage/Delete/DeleteModal.tsx +++ b/src/components/LetterDetailPage/Delete/DeleteModal.tsx @@ -1,6 +1,7 @@ import { Margin } from '@/components/Common/Margin/Margin'; -import { useParams, useNavigate } from 'react-router-dom'; +import { useParams, useNavigate, useLocation } from 'react-router-dom'; import { useDeleteKeywordLetter } from '@/hooks/useDeleteKeywordLetter'; +import { useDeleteMapSentLetter } from '@/hooks/useDeleteMapSentLetter'; import { useToastStore } from '@/hooks'; type DeleteModalProps = { @@ -9,17 +10,23 @@ type DeleteModalProps = { export const DeleteModal = ({ closeModal }: DeleteModalProps) => { const navigate = useNavigate(); + const { pathname } = useLocation(); + const letterType = pathname.split('/')[2]; const { dataType, letterId } = useParams<{ dataType: string; letterId: string; }>(); const transformedLetterType = dataType === 'sent' ? 'SEND' : 'RECEIVE'; - const mutation = useDeleteKeywordLetter({ + const keywordMutation = useDeleteKeywordLetter({ letterId: Number(letterId), boxType: transformedLetterType }); + const mapMutation = useDeleteMapSentLetter({ + letterIds: [Number(letterId)] + }); + const mutation = letterType === 'keyword' ? keywordMutation : mapMutation; const { addToast } = useToastStore(); const handleDelete = () => { @@ -27,14 +34,17 @@ export const DeleteModal = ({ closeModal }: DeleteModalProps) => { onSuccess: () => { closeModal(); addToast('편지 삭제에 성공했습니다.', 'success'); - navigate('/storage/keyword'); + navigate( + letterType === 'keyword' + ? '/storage/keyword' + : '/storage/map' + ); }, onError: () => { addToast('편지 삭제에 실패했습니다.', 'error'); } }); }; - return (
diff --git a/src/hooks/useDeleteMapSentLetter.ts b/src/hooks/useDeleteMapSentLetter.ts new file mode 100644 index 00000000..3d4e99ee --- /dev/null +++ b/src/hooks/useDeleteMapSentLetter.ts @@ -0,0 +1,21 @@ +import { useMutation, UseMutationOptions } from '@tanstack/react-query'; +import { deleteMapSentLetter } from '@/service/MapLetter/deleteMapSentLetter'; + +type UseDeleteMapSentLettersProps = { + letterIds: number[]; +}; + +export const useDeleteMapSentLetter = ( + { letterIds }: UseDeleteMapSentLettersProps, + options?: UseMutationOptions +) => { + return useMutation({ + mutationFn: async () => { + const response = await deleteMapSentLetter({ + letterIds + }); + return response.result; + }, + ...options + }); +}; diff --git a/src/service/MapLetter/deleteMapSentLetter.ts b/src/service/MapLetter/deleteMapSentLetter.ts new file mode 100644 index 00000000..47eb3431 --- /dev/null +++ b/src/service/MapLetter/deleteMapSentLetter.ts @@ -0,0 +1,22 @@ +import { defaultApi } from '@/service/api'; +import { ApiResponseType } from '@/types/apiResponse'; + +type deleteMapSentLettersRequestProps = { + letterIds: number[]; +}; + +type deleteMapSentLettersResponse = ApiResponseType; + +export async function deleteMapSentLetter({ + letterIds +}: deleteMapSentLettersRequestProps): Promise { + const api = defaultApi(); + + const response = await api.delete('/map', { + data: { + letterIds + } + }); + + return response.data; +}