|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { |
| 4 | + getCommentsDisclosure, |
| 5 | + postCommentsDisclosure, |
| 6 | +} from "@/src/apis/comment"; |
| 7 | +import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; |
| 8 | + |
| 9 | +const QUERY_KEY = ["comments-disclosure"]; |
| 10 | + |
| 11 | +export default function CommentsDisclosurePage() { |
| 12 | + const queryClient = useQueryClient(); |
| 13 | + const { data, isLoading, isError } = useQuery( |
| 14 | + QUERY_KEY, |
| 15 | + getCommentsDisclosure |
| 16 | + ); |
| 17 | + |
| 18 | + const { mutate: updateDisclosure, isLoading: isUpdating } = useMutation( |
| 19 | + postCommentsDisclosure, |
| 20 | + { |
| 21 | + onSuccess: () => { |
| 22 | + queryClient.invalidateQueries({ queryKey: QUERY_KEY }); |
| 23 | + }, |
| 24 | + onError: () => { |
| 25 | + alert("댓글 공개 상태 변경에 실패했습니다. 잠시 후 다시 시도해주세요."); |
| 26 | + }, |
| 27 | + } |
| 28 | + ); |
| 29 | + |
| 30 | + if (isLoading) { |
| 31 | + return <div className="mt-10">로딩중...</div>; |
| 32 | + } |
| 33 | + |
| 34 | + if (isError || data === undefined) { |
| 35 | + return <div className="mt-10">에러 발생</div>; |
| 36 | + } |
| 37 | + |
| 38 | + const nextDisclosure = !data; |
| 39 | + |
| 40 | + return ( |
| 41 | + <section className="mt-10 flex flex-col gap-4 max-w-xl"> |
| 42 | + <h2 className="text-xl font-semibold">댓글 전체 공개 상태 관리</h2> |
| 43 | + <div className="rounded-lg border p-6 flex items-center justify-between"> |
| 44 | + <div className="text-lg font-medium"> |
| 45 | + 현재 상태:{" "} |
| 46 | + <span className={data ? "text-primary" : "text-gray-500"}> |
| 47 | + {data ? "활성화" : "비활성화"} |
| 48 | + </span> |
| 49 | + </div> |
| 50 | + <button |
| 51 | + type="button" |
| 52 | + className={`rounded-md text-white px-4 py-2 disabled:bg-gray-300 ${ |
| 53 | + nextDisclosure |
| 54 | + ? "bg-blue-500 hover:bg-blue-600" |
| 55 | + : "bg-gray-500 hover:bg-gray-600" |
| 56 | + }`} |
| 57 | + onClick={() => updateDisclosure(nextDisclosure)} |
| 58 | + disabled={isUpdating} |
| 59 | + > |
| 60 | + {nextDisclosure ? "활성화" : "비활성화"} |
| 61 | + </button> |
| 62 | + </div> |
| 63 | + </section> |
| 64 | + ); |
| 65 | +} |
0 commit comments