Skip to content

Commit 37b3956

Browse files
banned unbanned added, login toast added
1 parent e7e831c commit 37b3956

2 files changed

Lines changed: 83 additions & 20 deletions

File tree

src/components/banButton.tsx

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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;

src/components/columns/UserCol.tsx

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { type User } from "@/data/schema";
99
import { banUnban } from "@/api/ban";
1010
import toast from "react-hot-toast";
1111
import { Button } from "../ui/button";
12+
import BanBtn from "../banButton";
1213

1314
const columns: ColumnDef<User>[] = [
1415
// {
@@ -115,26 +116,7 @@ const columns: ColumnDef<User>[] = [
115116
<DataTableColumnHeader column={column} title="Banned" />
116117
),
117118
cell: ({ row }) => (
118-
<Button
119-
onClick={() =>
120-
toast.promise(async ()=>{
121-
await banUnban({
122-
ban: !row.getValue("IsBanned"),
123-
email: row.getValue("Email"),
124-
})
125-
await queryClient.invalidateQueries({ queryKey: ["users"] })
126-
}
127-
128-
, {
129-
loading: "Updating...",
130-
success: "User updated",
131-
error: "Failed to update user",
132-
})
133-
}
134-
variant={row.getValue("IsBanned") ? "destructive" : "outline"}
135-
>
136-
{row.getValue("IsBanned") ? "Yes" : "No"}
137-
</Button>
119+
<BanBtn row={row}></BanBtn>
138120
),
139121
},
140122
// {

0 commit comments

Comments
 (0)