Skip to content

Commit

Permalink
feat : 보낸 지도 편지 상세 보기 페이지 삭제 API연동 (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
mmjjaa authored Dec 8, 2024
1 parent 34bd1ad commit eb19fd0
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 4 deletions.
18 changes: 14 additions & 4 deletions src/components/LetterDetailPage/Delete/DeleteModal.tsx
Original file line number Diff line number Diff line change
@@ -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 = {
Expand All @@ -9,32 +10,41 @@ 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 = () => {
mutation.mutate(undefined, {
onSuccess: () => {
closeModal();
addToast('편지 삭제에 성공했습니다.', 'success');
navigate('/storage/keyword');
navigate(
letterType === 'keyword'
? '/storage/keyword'
: '/storage/map'
);
},
onError: () => {
addToast('편지 삭제에 실패했습니다.', 'error');
}
});
};

return (
<div className="flex flex-col bg-white rounded-2xl items-center justify-center w-full h-full p-4">
<Margin top={10} />
Expand Down
21 changes: 21 additions & 0 deletions src/hooks/useDeleteMapSentLetter.ts
Original file line number Diff line number Diff line change
@@ -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<number[], Error, void>
) => {
return useMutation<number[], Error, void>({
mutationFn: async () => {
const response = await deleteMapSentLetter({
letterIds
});
return response.result;
},
...options
});
};
22 changes: 22 additions & 0 deletions src/service/MapLetter/deleteMapSentLetter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { defaultApi } from '@/service/api';
import { ApiResponseType } from '@/types/apiResponse';

type deleteMapSentLettersRequestProps = {
letterIds: number[];
};

type deleteMapSentLettersResponse = ApiResponseType<number[]>;

export async function deleteMapSentLetter({
letterIds
}: deleteMapSentLettersRequestProps): Promise<deleteMapSentLettersResponse> {
const api = defaultApi();

const response = await api.delete<deleteMapSentLettersResponse>('/map', {
data: {
letterIds
}
});

return response.data;
}

0 comments on commit eb19fd0

Please sign in to comment.