import { createFileRoute, useNavigate } from '@tanstack/react-router' import { DeleteOutlined, SearchOutlined } from '@ant-design/icons' import { Button, Input } from '@imphnen-frontend-service/ui/atoms' import { BackofficeWrapper, DataTable, } from '@imphnen-frontend-service/ui/organisms' import { cn, For } from '@imphnen-frontend-service/utils' import { ColumnDef, getCoreRowModel, getPaginationRowModel, PaginationState, RowSelectionState, useReactTable, } from '@tanstack/react-table' import { ReactElement, useState } from 'react' import { toast } from 'sonner' import { useMentorList, useUserList, useDeleteMentor, MentorDetailResponseDto, TUsersListItem, } from '@imphnen-frontend-service/service' export const Route = createFileRoute('/_authenticated/users-dimentorin')({ component: UsersDimentorinPage, }) function UsersDimentorinPage(): ReactElement { const TABS = ['mentor', 'mentee'] as const const navigate = useNavigate() const [activeTab, setActiveTab] = useState<'mentor' | 'mentee'>('mentor') const [search, setSearch] = useState('') const [deletingId, setDeletingId] = useState(null) const [rowSelection, setRowSelection] = useState({}) const [pagination, setPagination] = useState({ pageIndex: 0, pageSize: 9, }) const { data: mentorData, isLoading: mentorLoading } = useMentorList({ search, page: pagination.pageIndex + 1, per_page: pagination.pageSize, }) const { data: menteeData, isLoading: menteeLoading } = useUserList({ search, page: pagination.pageIndex + 1, per_page: pagination.pageSize, }) const deleteMentor = useDeleteMentor() const mentors: MentorDetailResponseDto[] = mentorData?.data ?? [] const mentees: TUsersListItem[] = menteeData?.data ?? [] const mentorTotal = mentorData?.meta?.total ?? mentors.length const menteeTotal = menteeData?.meta?.total ?? mentees.length const isLoading = activeTab === 'mentor' ? mentorLoading : menteeLoading const totalItems = activeTab === 'mentor' ? mentorTotal : menteeTotal const handleDelete = async (id: string) => { try { await deleteMentor.mutateAsync(id) toast.success('Akun berhasil dihapus') setDeletingId(null) } catch (error) { console.log(error) toast.error('Gagal menghapus akun') } } const mentorColumns: ColumnDef[] = [ { id: 'select', meta: { cellClassName: cn('w-20') }, header: ({ table }) => ( ), cell: ({ row }) => ( ), }, { id: 'name', header: 'Name', accessorKey: 'fullname', }, { id: 'email', header: 'Email', accessorKey: 'email', }, { id: 'rating', header: 'Rating', accessorKey: 'rating', cell: ({ row }) => {row.original.rating ?? '-'}, }, { id: 'status', header: 'Status', accessorKey: 'status', cell: ({ row }) => { const status = row.original.status const statusColors: Record = { active: 'bg-success-200 text-success-500', pending: 'bg-warning-200 text-warning-700', inactive: 'bg-danger-200 text-danger-500', } return (
{status}
) }, }, { header: 'Action', meta: { cellClassName: cn('w-72') }, cell: ({ row }) => (
{deletingId === row.original.id ? ( <> ) : ( <> )}
), }, ] const menteeColumns: ColumnDef[] = [ { id: 'select', meta: { cellClassName: cn('w-20') }, header: ({ table }) => ( ), cell: ({ row }) => ( ), }, { id: 'name', header: 'Name', accessorKey: 'fullname', }, { id: 'email', header: 'Email', accessorKey: 'email', }, { id: 'status', header: 'Status', accessorKey: 'is_active', cell: ({ row }) => (
{row.original.is_active ? 'Active' : 'Inactive'}
), }, { header: 'Action', meta: { cellClassName: cn('w-72') }, cell: ({ row }) => ( ), }, ] const mentorTable = useReactTable({ data: mentors, columns: mentorColumns, state: { pagination, rowSelection }, enableRowSelection: true, onRowSelectionChange: setRowSelection, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), onPaginationChange: setPagination, pageCount: Math.ceil(mentorTotal / pagination.pageSize), manualPagination: true, }) const menteeTable = useReactTable({ data: mentees, columns: menteeColumns, state: { pagination, rowSelection }, enableRowSelection: true, onRowSelectionChange: setRowSelection, getCoreRowModel: getCoreRowModel(), getPaginationRowModel: getPaginationRowModel(), onPaginationChange: setPagination, pageCount: Math.ceil(menteeTotal / pagination.pageSize), manualPagination: true, }) return (

User Management

{(tab) => ( )}
setSearch(e.target.value)} />
{isLoading ? (
Loading...
) : activeTab === 'mentor' ? ( ) : ( )}
) }