|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useActionState, useEffect } from "react"; |
| 4 | +import { redirect, RedirectType } from "next/navigation"; |
| 5 | +import Form from "next/form"; |
| 6 | +import { useQueryClient } from "@tanstack/react-query"; |
| 7 | +import { Button } from "@/components/ui/button"; |
| 8 | +import { Input } from "@/components/ui/input"; |
| 9 | +import { useUserQuery } from "@/hooks/use-user-query"; |
| 10 | +import { |
| 11 | + AlertDialog, |
| 12 | + AlertDialogTrigger, |
| 13 | + AlertDialogContent, |
| 14 | + AlertDialogHeader, |
| 15 | + AlertDialogTitle, |
| 16 | + AlertDialogDescription, |
| 17 | + AlertDialogFooter, |
| 18 | + AlertDialogCancel, |
| 19 | + AlertDialogAction, |
| 20 | +} from "@/components/ui/alert-dialog"; |
| 21 | +import { |
| 22 | + Dialog, |
| 23 | + DialogTrigger, |
| 24 | + DialogContent, |
| 25 | + DialogHeader, |
| 26 | + DialogTitle, |
| 27 | + DialogDescription, |
| 28 | + DialogFooter, |
| 29 | + DialogClose, |
| 30 | +} from "@/components/ui/dialog"; |
| 31 | +import { updateJargon } from "@/app/actions/update-jargon"; |
| 32 | +import { removeJargon } from "@/app/actions/remove-jargon"; |
| 33 | + |
| 34 | +export default function JargonActions({ |
| 35 | + jargonId, |
| 36 | + authorId, |
| 37 | + name, |
| 38 | +}: { |
| 39 | + jargonId: string; |
| 40 | + authorId: string; |
| 41 | + name: string; |
| 42 | +}) { |
| 43 | + const queryClient = useQueryClient(); |
| 44 | + |
| 45 | + const { data: user } = useUserQuery(); |
| 46 | + const isAdmin = user?.app_metadata?.userrole === "admin"; |
| 47 | + const canManage = !!user && (user.id === authorId || isAdmin); |
| 48 | + |
| 49 | + const [updateState, updateAction] = useActionState( |
| 50 | + updateJargon.bind(null, jargonId), |
| 51 | + { ok: false, error: "" }, |
| 52 | + ); |
| 53 | + const [removeState, removeAction] = useActionState( |
| 54 | + removeJargon.bind(null, jargonId), |
| 55 | + { ok: false, error: "" }, |
| 56 | + ); |
| 57 | + |
| 58 | + useEffect(() => { |
| 59 | + if (removeState.ok) { |
| 60 | + queryClient.invalidateQueries({ queryKey: ["jargons"] }); |
| 61 | + redirect("/", RedirectType.replace); |
| 62 | + } |
| 63 | + }, [removeState.ok, queryClient]); |
| 64 | + |
| 65 | + useEffect(() => { |
| 66 | + if (updateState.ok) { |
| 67 | + queryClient.invalidateQueries({ queryKey: ["jargons"] }); |
| 68 | + redirect(`/jargon/${updateState.jargonSlug}`, RedirectType.replace); |
| 69 | + } |
| 70 | + }, [updateState.ok, updateState, queryClient]); |
| 71 | + |
| 72 | + if (!canManage) return null; |
| 73 | + |
| 74 | + return ( |
| 75 | + <div className="flex items-center gap-2"> |
| 76 | + <Dialog> |
| 77 | + <DialogTrigger asChild> |
| 78 | + <Button type="button" variant="ghost" className="h-7 px-2.5 text-xs"> |
| 79 | + 고치기 |
| 80 | + </Button> |
| 81 | + </DialogTrigger> |
| 82 | + <DialogContent className="sm:max-w-[420px]"> |
| 83 | + <DialogHeader> |
| 84 | + <DialogTitle>용어 이름 고치기</DialogTitle> |
| 85 | + <DialogDescription> |
| 86 | + 새로운 용어 이름을 입력해 주세요. 저장하면 짧은 이름(slug)도 함께 |
| 87 | + 바뀌어요. |
| 88 | + </DialogDescription> |
| 89 | + </DialogHeader> |
| 90 | + <Form action={updateAction} className="grid gap-3"> |
| 91 | + <Input name="name" defaultValue={name} autoFocus /> |
| 92 | + {updateState?.error && ( |
| 93 | + <span className="text-xs text-red-600">{updateState.error}</span> |
| 94 | + )} |
| 95 | + <DialogFooter> |
| 96 | + <DialogClose asChild> |
| 97 | + <Button type="button" variant="outline"> |
| 98 | + 닫기 |
| 99 | + </Button> |
| 100 | + </DialogClose> |
| 101 | + <Button type="submit">저장</Button> |
| 102 | + </DialogFooter> |
| 103 | + </Form> |
| 104 | + </DialogContent> |
| 105 | + </Dialog> |
| 106 | + |
| 107 | + <AlertDialog> |
| 108 | + <AlertDialogTrigger asChild> |
| 109 | + <Button |
| 110 | + type="button" |
| 111 | + variant="ghost" |
| 112 | + className="h-7 px-2.5 text-xs text-red-600 hover:text-red-700" |
| 113 | + > |
| 114 | + 지우기 |
| 115 | + </Button> |
| 116 | + </AlertDialogTrigger> |
| 117 | + <AlertDialogContent> |
| 118 | + <AlertDialogHeader> |
| 119 | + <AlertDialogTitle>정말로 용어를 지울까요?</AlertDialogTitle> |
| 120 | + <AlertDialogDescription> |
| 121 | + 이 작업은 되돌릴 수 없어요. 관련 댓글과 번역도 함께 지워져요. |
| 122 | + </AlertDialogDescription> |
| 123 | + </AlertDialogHeader> |
| 124 | + {removeState?.error && ( |
| 125 | + <span className="text-xs text-red-600">{removeState.error}</span> |
| 126 | + )} |
| 127 | + <AlertDialogFooter> |
| 128 | + <AlertDialogCancel>닫기</AlertDialogCancel> |
| 129 | + <Form action={removeAction}> |
| 130 | + <AlertDialogAction |
| 131 | + type="submit" |
| 132 | + className="bg-red-600 hover:bg-red-700" |
| 133 | + > |
| 134 | + 지우기 |
| 135 | + </AlertDialogAction> |
| 136 | + </Form> |
| 137 | + </AlertDialogFooter> |
| 138 | + </AlertDialogContent> |
| 139 | + </AlertDialog> |
| 140 | + </div> |
| 141 | + ); |
| 142 | +} |
0 commit comments