|
| 1 | +import { useState } from "react"; |
| 2 | +import { MoreVertical, X } from "lucide-react"; |
| 3 | +import CardDetail from "./CardDetail"; |
| 4 | +import CommentList from "./CommentList"; |
| 5 | +import CardInput from "@/components/modalInput/CardInput"; |
| 6 | +import { useMutation, useQueryClient } from "@tanstack/react-query"; |
| 7 | +import { createComment } from "@/api/comment"; |
| 8 | +import { deleteCard } from "@/api/card"; |
| 9 | +import type { CardType } from "@/types/cards"; |
| 10 | +import TaskModal from "@/components/modalInput/TaskModal"; // 경로는 실제 위치에 맞게 조정하세요 |
| 11 | + |
| 12 | +interface CardDetailModalProps { |
| 13 | + card: CardType; |
| 14 | + currentUserId: number; |
| 15 | + teamId: string; |
| 16 | + dashboardId: number; |
| 17 | + onClose: () => void; |
| 18 | +} |
| 19 | + |
| 20 | +export default function CardDetailPage({ |
| 21 | + card, |
| 22 | + currentUserId, |
| 23 | + teamId, |
| 24 | + dashboardId, |
| 25 | + onClose, |
| 26 | +}: CardDetailModalProps) { |
| 27 | + const [cardData, setCardData] = useState<CardType>(card); |
| 28 | + const [commentText, setCommentText] = useState(""); |
| 29 | + const [showMenu, setShowMenu] = useState(false); |
| 30 | + const [isEditModalOpen, setIsEditModalOpen] = useState(false); |
| 31 | + const queryClient = useQueryClient(); |
| 32 | + |
| 33 | + const { mutate: createCommentMutate } = useMutation({ |
| 34 | + mutationFn: createComment, |
| 35 | + onSuccess: () => { |
| 36 | + setCommentText(""); |
| 37 | + queryClient.invalidateQueries({ queryKey: ["comments", card.id] }); |
| 38 | + }, |
| 39 | + }); |
| 40 | + |
| 41 | + const { mutate: deleteCardMutate } = useMutation({ |
| 42 | + mutationFn: () => deleteCard(teamId, card.id), |
| 43 | + onSuccess: () => { |
| 44 | + queryClient.invalidateQueries({ queryKey: ["cards"] }); |
| 45 | + onClose(); |
| 46 | + }, |
| 47 | + }); |
| 48 | + |
| 49 | + const handleCommentSubmit = () => { |
| 50 | + if (!commentText.trim()) return; |
| 51 | + createCommentMutate({ |
| 52 | + content: commentText, |
| 53 | + cardId: card.id, |
| 54 | + columnId: card.columnId, |
| 55 | + dashboardId, |
| 56 | + }); |
| 57 | + }; |
| 58 | + |
| 59 | + return ( |
| 60 | + <> |
| 61 | + <div className="fixed inset-0 bg-black/30 z-50 flex items-center justify-center"> |
| 62 | + <div className="relative bg-white rounded-lg shadow-lg w-[730px] h-[763px] flex flex-col"> |
| 63 | + {/* 오른쪽 상단 메뉴 */} |
| 64 | + <div className="absolute top-2 right-6 w-[50px] h-[50px] z-30 flex items-center gap-2"> |
| 65 | + <div className="relative"> |
| 66 | + <button onClick={() => setShowMenu((prev) => !prev)}> |
| 67 | + <MoreVertical className="w-8 h-8 text-gray-500 hover:text-black" /> |
| 68 | + </button> |
| 69 | + {showMenu && ( |
| 70 | + <div className="absolute right-0 mt-2 w-24 bg-white border rounded shadow z-40"> |
| 71 | + <button |
| 72 | + className="block w-full px-3 py-2 text-sm text-violet-600 hover:bg-gray-100" |
| 73 | + onClick={() => { |
| 74 | + setIsEditModalOpen(true); |
| 75 | + setShowMenu(false); |
| 76 | + }} |
| 77 | + > |
| 78 | + 수정하기 |
| 79 | + </button> |
| 80 | + <button |
| 81 | + className="block w-full px-3 py-2 text-sm text-red-500 hover:bg-gray-100" |
| 82 | + onClick={() => deleteCardMutate()} |
| 83 | + > |
| 84 | + 삭제하기 |
| 85 | + </button> |
| 86 | + </div> |
| 87 | + )} |
| 88 | + </div> |
| 89 | + <button onClick={onClose}> |
| 90 | + <X className="w-8 h-8 text-gray-500 hover:text-black" /> |
| 91 | + </button> |
| 92 | + </div> |
| 93 | + |
| 94 | + {/* 모달 내부 콘텐츠 */} |
| 95 | + <div className="p-6 flex gap-6 overflow-y-auto"> |
| 96 | + <CardDetail card={cardData} columnName={""} /> |
| 97 | + </div> |
| 98 | + |
| 99 | + {/* 댓글 입력창 */} |
| 100 | + <div className="p-4"> |
| 101 | + <CardInput |
| 102 | + hasButton |
| 103 | + small |
| 104 | + value={commentText} |
| 105 | + onTextChange={setCommentText} |
| 106 | + onButtonClick={handleCommentSubmit} |
| 107 | + /> |
| 108 | + </div> |
| 109 | + |
| 110 | + {/* 댓글 목록 */} |
| 111 | + <div className="px-6 space-y-4 max-h-[200px] overflow-y-auto"> |
| 112 | + <CommentList |
| 113 | + cardId={card.id} |
| 114 | + currentUserId={currentUserId} |
| 115 | + teamId={""} |
| 116 | + /> |
| 117 | + </div> |
| 118 | + </div> |
| 119 | + </div> |
| 120 | + |
| 121 | + {/* TaskModal 수정 모드 */} |
| 122 | + {isEditModalOpen && ( |
| 123 | + <TaskModal |
| 124 | + mode="edit" |
| 125 | + onClose={() => setIsEditModalOpen(false)} |
| 126 | + onSubmit={(data) => { |
| 127 | + setCardData((prev) => ({ |
| 128 | + ...prev, |
| 129 | + status: data.status as "todo" | "in-progress" | "done", |
| 130 | + assignee: { ...prev.assignee, nickname: data.assignee }, |
| 131 | + title: data.title, |
| 132 | + description: data.description, |
| 133 | + dueDate: data.deadline, |
| 134 | + tags: data.tags, |
| 135 | + imageUrl: data.image ?? "", |
| 136 | + })); |
| 137 | + setIsEditModalOpen(false); |
| 138 | + }} |
| 139 | + initialData={{ |
| 140 | + status: cardData.status, |
| 141 | + assignee: cardData.assignee.nickname, |
| 142 | + title: cardData.title, |
| 143 | + description: cardData.description, |
| 144 | + deadline: cardData.dueDate, |
| 145 | + tags: cardData.tags, |
| 146 | + image: cardData.imageUrl ?? "", |
| 147 | + }} |
| 148 | + members={[{ nickname: cardData.assignee.nickname }]} |
| 149 | + /> |
| 150 | + )} |
| 151 | + </> |
| 152 | + ); |
| 153 | +} |
0 commit comments