Skip to content

Commit

Permalink
Refactor : 게시글 삭제 로직 변경 (첨부파일 함께 삭제)
Browse files Browse the repository at this point in the history
  • Loading branch information
jobkaeHenry committed Dec 4, 2023
1 parent f5f9f0d commit 51c0778
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
7 changes: 6 additions & 1 deletion client/src/components/post/PostCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,12 @@ const PostCard = ({
</Typography>
</Stack>

{isMyPost && <PostCardOptionDropdown postId={postNo} />}
{isMyPost && (
<PostCardOptionDropdown
postId={postNo}
filePk={postAttachUrls?.[0]?.attachNo}
/>
)}
</Stack>

{alcoholName && (
Expand Down
31 changes: 20 additions & 11 deletions client/src/components/post/PostCardOptionDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,36 @@ import React, { useState } from "react";
import { MoreVertOutlined } from "@mui/icons-material";
import { ButtonBase, Menu, MenuItem } from "@mui/material";
import { useDeletePostMutation } from "@/queries/post/useDeletePostMutation";
import useDeleteAttachMutation from "@/queries/attach/useDeleteAttachMutation";
import { useRouter } from "next/navigation";
import HOME from "@/const/clientPath";

type PostCardOptionDropdownProps = {
postId: number;
filePk?: string;
};

const PostCardOptionDropdown = ({ postId }: PostCardOptionDropdownProps) => {
const PostCardOptionDropdown = ({
postId,
filePk,
}: PostCardOptionDropdownProps) => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
const router = useRouter();

const handleClick = (event: React.MouseEvent<HTMLButtonElement>) => {
setAnchorEl(event.currentTarget);
};
const { mutate: deletePost } = useDeletePostMutation();
const { mutateAsync: deletePost } = useDeletePostMutation();
const { mutateAsync: deleteFile } = useDeleteAttachMutation();

const deleteHandler = async () => {
if (confirm("정말 삭제하시겠습니까?")) {
await deletePost(postId);
filePk && (await deleteFile(filePk));
router.push(HOME);
}
};

const handleClose = () => {
setAnchorEl(null);
Expand All @@ -25,15 +42,7 @@ const PostCardOptionDropdown = ({ postId }: PostCardOptionDropdownProps) => {
<MoreVertOutlined />
</ButtonBase>
<Menu open={open} anchorEl={anchorEl} onClose={handleClose}>
<MenuItem
onClick={() => {
if (confirm("정말 삭제하시겠습니까?")) {
deletePost(postId);
}
}}
>
삭제
</MenuItem>
<MenuItem onClick={deleteHandler}>삭제</MenuItem>
<MenuItem>수정</MenuItem>
</Menu>
</>
Expand Down

0 comments on commit 51c0778

Please sign in to comment.