Skip to content

Commit e7e831c

Browse files
fixed logout
1 parent 9d4a1f9 commit e7e831c

7 files changed

Lines changed: 62 additions & 47 deletions

File tree

src/api/auth.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ export const login = async(email: string, password: string) =>{
77
return response.data as data;
88
}catch(err){
99

10+
throw err;
11+
}
12+
}
13+
14+
export const logout = async() =>{
15+
try {
16+
const response = await axios.post('/auth/logout')
17+
return response.data as data;
18+
}catch(err){
19+
1020
throw err;
1121
}
1222
}

src/api/logout.ts

Lines changed: 0 additions & 12 deletions
This file was deleted.

src/app/users/page.tsx

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
"use client";
22
import { DataTable } from "@/components/table/data-table";
3-
import columns from "@/components/columns";
43
import userCol from "@/components/columns/UserCol";
54
// import { useEffect, useMemo, useState } from "react";
65
// import { user } from "@/store/interfaces";
7-
import oosers from "@/components/dumUser.json";
86
// import useToast from "@/lib/toast";
97
import { useQuery } from "@tanstack/react-query";
108
import { fetchUsers } from "@/api/fetchUsers";
11-
import { User } from "@/data/schema";
12-
import { useEffect, useState } from "react";
9+
import {type User } from "@/data/schema";
10+
import { useState } from "react";
1311
import { useDebounce } from "use-debounce";
1412
import Image from "next/image";
1513
import loading from "@/assets/images/loading.gif";
@@ -20,12 +18,10 @@ export default function Users() {
2018
const [currentCursor, setCurrentCursor] = useState<string | undefined>(
2119
undefined,
2220
);
23-
const [selectedUser, setSelectedUser] = useState<User | null>(null);
2421
const [pageLimit, setPageLimit] =useState<number>(10);
2522
const [theName, setTheName] = useState<string>("");
2623
// const queryClient = useQueryClient();
2724
const [nameDebounce] = useDebounce(theName, 1000);
28-
const [open, setOpen] = useState(false);
2925
const {
3026
data: userList,
3127
isLoading,
@@ -53,14 +49,9 @@ export default function Users() {
5349
setCurrentCursor(prevCursor ?? undefined); // Move to previous page
5450
}
5551
};
56-
const handleRowClick = (user: User) => {
57-
setSelectedUser(user);
58-
setOpen(true);
59-
};
6052

61-
const handleModalClose = () => {
62-
setOpen(false);
63-
};
53+
54+
6455

6556

6657
return (
@@ -90,13 +81,7 @@ export default function Users() {
9081
/>
9182
</div>
9283
)}
93-
{selectedUser && (
94-
<UserModal
95-
open={open}
96-
onClose={handleModalClose}
97-
user={selectedUser}
98-
/>
99-
)}
84+
10085

10186
<DataTable<User, string>
10287
setPageLimit={setPageLimit}
@@ -106,7 +91,6 @@ export default function Users() {
10691
// data={oosers}
10792
handleNextPage={handleNextPage}
10893
handlePrevPage={handlePrevPage}
109-
onRowClick={handleRowClick}
11094
/>
11195
</div>
11296
);

src/components/columns/UserCol.tsx

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ import { DataTableColumnHeader } from "@/components/table/data-table-column-head
66
import { Badge } from "@/components/ui/badge";
77
import { DataTableRowActions } from "@/components/table/data-table-row-actions";
88
import { type User } from "@/data/schema";
9+
import { banUnban } from "@/api/ban";
10+
import toast from "react-hot-toast";
11+
import { Button } from "../ui/button";
912

1013
const columns: ColumnDef<User>[] = [
1114
// {
@@ -52,7 +55,9 @@ const columns: ColumnDef<User>[] = [
5255
// },
5356
{
5457
accessorKey: "Email",
55-
header: ({ column }) => <DataTableColumnHeader column={column} title="Email" />,
58+
header: ({ column }) => (
59+
<DataTableColumnHeader column={column} title="Email" />
60+
),
5661
cell: ({ row }) => <span>{row.getValue("Email")}</span>,
5762
},
5863
// {
@@ -62,17 +67,23 @@ const columns: ColumnDef<User>[] = [
6267
// },
6368
{
6469
accessorKey: "Gender",
65-
header: ({ column }) => <DataTableColumnHeader column={column} title="Gender" />,
70+
header: ({ column }) => (
71+
<DataTableColumnHeader column={column} title="Gender" />
72+
),
6673
cell: ({ row }) => <span>{row.getValue("Gender")}</span>,
6774
},
6875
{
6976
accessorKey: "RegNo",
70-
header: ({ column }) => <DataTableColumnHeader column={column} title="Reg No." />,
77+
header: ({ column }) => (
78+
<DataTableColumnHeader column={column} title="Reg No." />
79+
),
7180
cell: ({ row }) => <span>{row.getValue("RegNo")}</span>,
7281
},
7382
{
7483
accessorKey: "PhoneNo",
75-
header: ({ column }) => <DataTableColumnHeader column={column} title="Phone No." />,
84+
header: ({ column }) => (
85+
<DataTableColumnHeader column={column} title="Phone No." />
86+
),
7687
cell: ({ row }) => <span>{row.getValue("PhoneNo")}</span>,
7788
},
7889
// {
@@ -100,11 +111,30 @@ const columns: ColumnDef<User>[] = [
100111
// },
101112
{
102113
accessorKey: "IsBanned",
103-
header: ({ column }) => <DataTableColumnHeader column={column} title="Banned" />,
114+
header: ({ column }) => (
115+
<DataTableColumnHeader column={column} title="Banned" />
116+
),
104117
cell: ({ row }) => (
105-
<Badge variant={row.getValue("IsBanned") ? "destructive" : "outline"}>
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+
>
106136
{row.getValue("IsBanned") ? "Yes" : "No"}
107-
</Badge>
137+
</Button>
108138
),
109139
},
110140
// {
@@ -132,7 +162,9 @@ const columns: ColumnDef<User>[] = [
132162
// },
133163
{
134164
accessorKey: "HostelBlock",
135-
header: ({ column }) => <DataTableColumnHeader column={column} title="Hostel Block" />,
165+
header: ({ column }) => (
166+
<DataTableColumnHeader column={column} title="Hostel Block" />
167+
),
136168
cell: ({ row }) => <span>{row.getValue("HostelBlock")}</span>,
137169
},
138170
// {

src/components/login-form.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { cn } from "@/lib/utils";
1414
import { Loader2 } from "lucide-react"; // Import spinner icon
1515
import { useRouter } from "next/navigation";
1616
import { useState } from "react";
17+
import toast from "react-hot-toast";
1718

1819
export function LoginForm({
1920
className,
@@ -30,6 +31,7 @@ export function LoginForm({
3031
try {
3132
const response = await login(email, password);
3233
if (response.status === "success") {
34+
toast.success("Login successful");
3335
router.push("/users");
3436
}
3537
} catch (err) {

src/components/navbar.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,14 @@ import {
1717
import { LogOutIcon, MenuIcon } from "lucide-react";
1818
import { useRouter } from "next/navigation";
1919
import { Sidebar } from "./sidebar";
20+
import { logout } from "@/api/auth";
2021

2122
export function Navbar() {
2223
const router = useRouter();
2324

2425
const handleLogout = async ()=>{
2526
try{
26-
await fetch("/api/auth/signout", {
27-
method: "POST",
28-
headers: {
29-
'Content-Type': 'application/json'
30-
},
31-
})
27+
await logout();
3228
router.push("/login")
3329
}catch(err){
3430
console.log(err);

src/env.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export const env = createEnv({
1616
* `NEXT_PUBLIC_`.
1717
*/
1818
client: {
19+
NEXT_PUBLIC_BASEURL: z.string(),
1920
// NEXT_PUBLIC_CLIENTVAR: z.string(),
2021
},
2122

@@ -24,6 +25,8 @@ export const env = createEnv({
2425
* middlewares) or client-side so we need to destruct manually.
2526
*/
2627
runtimeEnv: {
28+
NEXT_PUBLIC_BASEURL: process.env.NEXT_PUBLIC_BASEURL,
29+
2730
NODE_ENV: process.env.NODE_ENV,
2831
// NEXT_PUBLIC_CLIENTVAR: process.env.NEXT_PUBLIC_CLIENTVAR,
2932
},

0 commit comments

Comments
 (0)