|
| 1 | +import { banUnban } from "@/api/ban"; |
| 2 | +import { Button } from "@/components/ui/button"; |
| 3 | +import { User } from "@/data/schema"; |
| 4 | +import { useMutation, useQueryClient } from "@tanstack/react-query"; |
| 5 | +import { Row } from "@tanstack/react-table"; |
| 6 | +import { ApiError } from "next/dist/server/api-utils"; |
| 7 | +import toast from "react-hot-toast"; |
| 8 | +const BanBtn = ({ row }: { row: Row<User> }) => { |
| 9 | + const queryClient = useQueryClient(); |
| 10 | + const handleUnban = useMutation({ |
| 11 | + mutationFn: (id: string) => { |
| 12 | + return toast.promise( |
| 13 | + banUnban({ |
| 14 | + ban: !row.getValue("IsBanned"), |
| 15 | + email: row.getValue("Email"), |
| 16 | + }), |
| 17 | + { |
| 18 | + loading: "UnRoasting...", |
| 19 | + success: "UnRoast success", |
| 20 | + error: (err: ApiError) => err.message, |
| 21 | + }, |
| 22 | + ); |
| 23 | + }, |
| 24 | + onSuccess: async () => { |
| 25 | + await queryClient.invalidateQueries({ queryKey: ["users"] }); |
| 26 | + }, |
| 27 | + }); |
| 28 | + |
| 29 | + const handleBan = useMutation({ |
| 30 | + mutationFn: (id: string) => { |
| 31 | + return toast.promise( |
| 32 | + banUnban({ |
| 33 | + ban: !row.getValue("IsBanned"), |
| 34 | + email: row.getValue("Email"), |
| 35 | + }), |
| 36 | + { |
| 37 | + loading: "Roasting...", |
| 38 | + success: "Roast success", |
| 39 | + error: (err: ApiError) => err.message, |
| 40 | + }, |
| 41 | + ); |
| 42 | + }, |
| 43 | + onSuccess: async () => { |
| 44 | + await queryClient.invalidateQueries({ queryKey: ["users"] }); |
| 45 | + }, |
| 46 | + }); |
| 47 | + const onRoastSubmit = (id: string) => { |
| 48 | + handleBan.mutate(id); |
| 49 | + }; |
| 50 | + const onunRoastSubmit = (id: string) => { |
| 51 | + handleUnban.mutate(id); |
| 52 | + }; |
| 53 | + return ( |
| 54 | + <div> |
| 55 | + {row.original.IsBanned ? ( |
| 56 | + <div> |
| 57 | + <Button |
| 58 | + variant={row.getValue("IsBanned") ? "destructive" : "outline"} |
| 59 | + onClick={() => { |
| 60 | + onunRoastSubmit(row.original.ID); |
| 61 | + }} |
| 62 | + > |
| 63 | + UnBan |
| 64 | + </Button> |
| 65 | + </div> |
| 66 | + ) : ( |
| 67 | + <div> |
| 68 | + <Button |
| 69 | + onClick={() => { |
| 70 | + onRoastSubmit(row.original.ID); |
| 71 | + }} |
| 72 | + variant={row.getValue("IsBanned") ? "destructive" : "outline"} |
| 73 | + > |
| 74 | + Ban |
| 75 | + </Button>{" "} |
| 76 | + </div> |
| 77 | + )} |
| 78 | + </div> |
| 79 | + ); |
| 80 | +}; |
| 81 | +export default BanBtn; |
0 commit comments