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