Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 27 additions & 1 deletion src/app/admin/dashboard/_components/addRole.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,44 @@
import TextInput from '@/app/(user)/register/_components/textInput';
import { Button } from '@/components/ui/button';
import { useAuth } from '@/contexts/auth';
import { UserPlus } from 'lucide-react';

import { useState } from 'react';
import toast from 'react-hot-toast';
import { useAddStaff } from '../api/user';

export default function AddRole() {
const [phone, setPhone] = useState('');
const { token } = useAuth();
const { mutateAsync: addStaff } = useAddStaff(token?.accessToken || '');
const { user } = useAuth();

function onAddStaff() {
if (phone == user?.phone) {
toast.error('You cannot add yourself as a staff member', {
duration: 5000,
});
return;
}
const resp = addStaff({ phone });
toast.promise(resp, {
loading: 'Adding staff',
success: 'Adding staff successful',
error: 'No user found with that phone number',
});
}

return (
<div className="flex items-center justify-center gap-4 border-b-2 border-b-light-pink px-6 py-4">
<TextInput
className="rounded-full border-light-gray shadow-none placeholder:text-center placeholder:text-dark-gray"
placeholder="add phone number"
value={phone}
onChange={e => setPhone(e.target.value)}
/>
<Button
inClassName="flex items-center justify-center gap-2 text-sm text-nowrap py-1"
variant={'outline'}
onClick={onAddStaff}
>
<UserPlus />
Add Role
Expand Down
32 changes: 17 additions & 15 deletions src/app/admin/dashboard/_components/detail.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
'use client';

import React from 'react';
import React, { useState } from 'react';
import AddRole from './addRole';
import SearchStaff from './searchStaff';
import UserList from './userList';
import { User } from '@/schema/user';
import { useAuth } from '@/contexts/auth';
import { useGetUsers } from '../api/user';

interface DetailProps {
users: User[];
name: string;
setName: (name: string) => void;
handleSearch: () => void;
}
export default function Detail() {
const [name, setName] = useState('');
const { token } = useAuth();
const { isLoading, refetch, data } = useGetUsers(
token?.accessToken || '',
name,
);

function handleSearch() {
refetch();
}

const users = data || [];

export default function Detail({
users,
setName,
name,
handleSearch,
}: DetailProps) {
return (
<div className="w-full flex-1 bg-white text-base">
<AddRole />
Expand All @@ -27,7 +29,7 @@ export default function Detail({
onChange={e => setName(e.target.value)}
handleSearch={handleSearch}
/>
<UserList users={users} />
<UserList users={users} isLoading={isLoading} />
</div>
);
}
28 changes: 28 additions & 0 deletions src/app/admin/dashboard/_components/spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';

export default function Spinner() {
return (
<div
role="status"
className="m-auto flex min-h-80 items-center justify-center"
>
<svg
aria-hidden="true"
className="m-auto h-8 w-8 animate-spin fill-dark-pink text-gray-200 dark:text-gray-600"
viewBox="0 0 100 101"
fill="none"
xmlns="http://www.w3.org/2000/svg"
>
<path
d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z"
fill="currentColor"
/>
<path
d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z"
fill="currentFill"
/>
</svg>
<span className="sr-only">Loading...</span>
</div>
);
}
39 changes: 34 additions & 5 deletions src/app/admin/dashboard/_components/table/action.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,62 @@ import {
} from '@radix-ui/react-popover';

import ActionList from './actionList';
import { useDeleteUser } from '../../api/user';
import { useDeleteUser, useUpdateUserRole } from '../../api/user';
import { useAuth } from '@/contexts/auth';
import toast from 'react-hot-toast';

export default function Action({ id }: { id: string }) {
const { token } = useAuth();
const { mutate: deleteUser } = useDeleteUser(id, token?.accessToken || '');
const { mutateAsync: deleteUser } = useDeleteUser(token?.accessToken || '');
const { mutateAsync: updateRole } = useUpdateUserRole(
token?.accessToken || '',
);

function onDelete() {
const resp = deleteUser({ id });
toast.promise(resp, {
success: 'Delete user success',
error: 'Failed Delete user',
loading: 'Deleting user',
});
}

function onUpdateRole(role: 'admin' | 'staff' | 'member') {
const resp = updateRole({ id, role });
toast.promise(resp, {
success: 'Update role success',
error: 'Failed update role',
loading: 'Updating role',
});
}

const actions = [
{
imageURL: '/admin/dashboard/circle-x.svg',
className: 'bg-error',
text: 'Remove',
fn: deleteUser,
fn: onDelete,
},
{
imageURL: '/admin/dashboard/profile.svg',
className: 'bg-orange-500',
text: 'Change to member',
fn: () => onUpdateRole('member'),
},
{
imageURL: '/admin/dashboard/profile.svg',
className: 'bg-dark-pink',
text: 'Change to Staff',
fn: () => {},
fn: () => onUpdateRole('staff'),
},
{
imageURL: '/admin/dashboard/crown.svg',
className: 'bg-dark-blue',
text: 'Change to Admin',
fn: () => {},
fn: () => onUpdateRole('admin'),
},
];

return (
<Popover>
<PopoverTrigger>
Expand Down
19 changes: 15 additions & 4 deletions src/app/admin/dashboard/_components/userList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ import React from 'react';
import Header from './table/header';
import UserRow from './table/userRow';
import { User } from '@/schema/user';
import Spinner from './spinner';
import { useAuth } from '@/contexts/auth';

interface UserListProps {
users: User[];
isLoading: boolean;
}

export default function UserList({ users }: UserListProps) {
export default function UserList({ users, isLoading }: UserListProps) {
const { user: me } = useAuth();

return (
<div>
<Header />
{users.map(user => (
<UserRow key={user.id} user={user} />
))}
{isLoading ? (
<Spinner />
) : (
users
.filter(
user => user.role != 'member' && (!me?.id || me.id != user.id),
)
.map(user => <UserRow key={user.id} user={user} />)
)}
</div>
);
}
62 changes: 60 additions & 2 deletions src/app/admin/dashboard/api/user.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { Role } from '@/const/role';
import { apiClient } from '@/utils/axios';
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
import toast from 'react-hot-toast';

export async function getUsers(accessToken: string, name?: string) {
const query = name ? `?name=${name}` : '';
Expand All @@ -26,12 +28,68 @@ export async function deleteUser(id: string, accessToken: string) {
});
}

export function useDeleteUser(id: string, accessToken: string) {
export function useDeleteUser(accessToken: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: () => deleteUser(id, accessToken),
mutationFn: ({ id }: { id: string }) => deleteUser(id, accessToken),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['users'] });
toast.success('User deleted');
},
onError: error => {
console.error('Failed to update user role:', error);
},
});
}

export function updateUserRole(id: string, role: Role, accessToken: string) {
return apiClient.patch(
`/users/${id}`,
{ role },
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
}

export function useUpdateUserRole(accessToken: string) {
const queryClient = useQueryClient();

return useMutation({
mutationFn: ({ id, role }: { id: string; role: Role }) =>
updateUserRole(id, role, accessToken),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['users'] });
},
onError: error => {
console.error('Failed to update user role:', error);
},
});
}

export function addStaff(phone: string, accessToken: string) {
return apiClient.patch(
`/users/addstaff/${phone}`,
{},
{
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
);
}

export function useAddStaff(accessToken: string) {
const queryClient = useQueryClient();
return useMutation({
mutationFn: ({ phone }: { phone: string }) => addStaff(phone, accessToken),
onSuccess: () => {
queryClient.invalidateQueries({ queryKey: ['users'] });
},
onError: error => {
console.error('Failed to add staff:', error);
},
});
}
35 changes: 4 additions & 31 deletions src/app/admin/dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,14 @@
import Background from '@/components/staff/qr/background';
import Top from './_components/top';
import Detail from './_components/detail';
import Load from '@/components/loading/loading';
import { useGetUsers } from './api/user';
import { useState } from 'react';
import { useAuth } from '@/contexts/auth';

export default function Page() {
const [name, setName] = useState('');
const { token } = useAuth();
const {
isLoading,
refetch,
data: users,
} = useGetUsers(token?.accessToken || '', name);

function handleSearch() {
refetch();
}

return (
<div className="item-center relative flex min-h-screen w-full flex-col pt-8">
{isLoading ? (
<Load />
) : (
<>
<Top />
<Background />
<div className="mt-4"></div>
<Detail
name={name}
setName={setName}
users={users}
handleSearch={handleSearch}
/>
</>
)}
<Top />
<Background />
<div className="mt-4"></div>
<Detail />
</div>
);
}
2 changes: 2 additions & 0 deletions src/const/role.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export const roles = ['member', 'staff', 'admin'] as const;

export type Role = (typeof roles)[number];
Loading