Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions src/api/delete.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import apiClient from '@/api/apiClient';

export interface DeleteResponse {
success: boolean;
message?: string;
}

export const deleteWine: (wineId: number) => Promise<DeleteResponse> = async (wineId) => {
const response = await apiClient.delete<DeleteResponse>(
`/${process.env.NEXT_PUBLIC_TEAM}/wines/${wineId}`,
);
return response.data;
};

export const deleteReview = async (reviewId: number): Promise<DeleteResponse> => {
const response = await apiClient.delete<DeleteResponse>(
`/${process.env.NEXT_PUBLIC_TEAM}/reviews/${reviewId}`,
);
return response.data;
};
3 changes: 0 additions & 3 deletions src/assets/icons/Star.svg

This file was deleted.

91 changes: 91 additions & 0 deletions src/components/Modal/DeleteModal/DeleteModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { useMutation, useQueryClient } from '@tanstack/react-query';
import { AxiosError } from 'axios';

import { deleteReview, deleteWine, DeleteResponse } from '@/api/delete';
import BasicModal from '@/components/common/Modal/BasicModal';
import { Button } from '@/components/ui/button';

interface DeleteModalProps {
type: 'wine' | 'review';
id: number;
showDeleteModal: boolean;
setShowDeleteModal: (state: boolean) => void;
}

const DeleteModal = ({ type, id, showDeleteModal, setShowDeleteModal }: DeleteModalProps) => {
const queryClient = useQueryClient();

const deleteWineMutation = useMutation<DeleteResponse, AxiosError, number>({
mutationFn: (id) => deleteWine(id),
});
const deleteReviewMutation = useMutation<DeleteResponse, AxiosError, number>({
mutationFn: (id) => deleteReview(id),
});

const handleDelete = () => {
if (type === 'wine') {
deleteWineMutation.mutate(id, {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['wines'] }); //삭제후 관련데이터 바로 갱신
console.log('와인 삭제 성공');
setShowDeleteModal(false);
},
onError: (error) => {
console.error('와인 삭제 실패', error);
},
});
} else if (type === 'review') {
deleteReviewMutation.mutate(id, {
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['reviews'] });
console.log('리뷰 삭제 성공');
setShowDeleteModal(false);
},
onError: (error) => {
console.error('리뷰 삭제 실패', error);
},
Comment on lines +27 to +46
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 방법으로 onSuccess, onError 처리도 가능하군요? 항상 Mutation 선언할 때만 썼는데 새롭네요

});
}
};

return (
<div>
<BasicModal
type='register'
title=''
open={showDeleteModal}
onOpenChange={(isOpen: boolean) => setShowDeleteModal(isOpen)}
showCloseButton={false}
buttons={
<div className='flex w-full gap-2'>
<Button
onClick={() => setShowDeleteModal(false)}
type='button'
variant='onlyCancel'
size='xl'
width='full'
fontSize='lg'
>
취소
</Button>
<Button
onClick={handleDelete}
type='button'
variant='purpleDark'
size='xl'
width='full'
fontSize='lg'
>
삭제
</Button>
</div>
}
>
<span className='flex justify-center mb-8 custom-text-2lg-bold md:custom-text-xl-bold'>
정말로 삭제하시겠습니까?
</span>
</BasicModal>
</div>
);
};
export default DeleteModal;
2 changes: 1 addition & 1 deletion src/components/common/winelist/WineCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from 'react';

import Link from 'next/link';

import StarIcon from '@/assets/icons/Star.svg';
import StarIcon from '@/assets/icons/star.svg';
import { ImageCard } from '@/components/common/card/ImageCard';
import { cn } from '@/lib/utils';

Expand Down
2 changes: 1 addition & 1 deletion src/components/common/winelist/WineListCard.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import Link from 'next/link';

import NextIcon from '@/assets/icons/Next.svg';
import StarIcon from '@/assets/icons/Star.svg';
import StarIcon from '@/assets/icons/star.svg';
import { ImageCard } from '@/components/common/card/ImageCard';
import { Button } from '@/components/ui/button';
import { cn } from '@/lib/utils';
Expand Down