|
| 1 | +import { |
| 2 | + ArrowLeftIcon, |
| 3 | + ArrowRightIcon, |
| 4 | + DoubleArrowLeftIcon, |
| 5 | + DoubleArrowRightIcon, |
| 6 | +} from '@radix-ui/react-icons'; |
| 7 | +import { |
| 8 | + ColumnDef, |
| 9 | + ColumnFiltersState, |
| 10 | + flexRender, |
| 11 | + getCoreRowModel, |
| 12 | + getFilteredRowModel, |
| 13 | + getPaginationRowModel, |
| 14 | + useReactTable, |
| 15 | +} from '@tanstack/react-table'; |
| 16 | +import { type FC, useState } from 'react'; |
| 17 | + |
| 18 | +import { Button } from '@/components/ui/button'; |
| 19 | +import { Pagination, PaginationContent, PaginationItem } from '@/components/ui/pagination'; |
| 20 | +import { |
| 21 | + Table, |
| 22 | + TableBody, |
| 23 | + TableCell, |
| 24 | + TableHead, |
| 25 | + TableHeader, |
| 26 | + TableRow, |
| 27 | +} from '@/components/ui/table'; |
| 28 | +import type { IInterviewRoom } from '@/types/collab-types'; |
| 29 | + |
| 30 | +type InterviewsTableProps = { |
| 31 | + columns: Array<ColumnDef<IInterviewRoom>>; |
| 32 | + data: Array<IInterviewRoom>; |
| 33 | + isError: boolean; |
| 34 | +}; |
| 35 | + |
| 36 | +export const InterviewsTable: FC<InterviewsTableProps> = ({ columns, data, isError }) => { |
| 37 | + const [pagination, setPagination] = useState({ |
| 38 | + pageIndex: 0, |
| 39 | + pageSize: 10, |
| 40 | + }); |
| 41 | + |
| 42 | + const [columnFilters, setColumnFilters] = useState<ColumnFiltersState>([]); |
| 43 | + |
| 44 | + const table = useReactTable({ |
| 45 | + data, |
| 46 | + columns, |
| 47 | + state: { pagination, columnFilters }, |
| 48 | + filterFns: {}, |
| 49 | + getPaginationRowModel: getPaginationRowModel(), |
| 50 | + getFilteredRowModel: getFilteredRowModel(), |
| 51 | + getCoreRowModel: getCoreRowModel(), |
| 52 | + onColumnFiltersChange: setColumnFilters, |
| 53 | + onPaginationChange: setPagination, |
| 54 | + }); |
| 55 | + |
| 56 | + return ( |
| 57 | + <div className='flex size-full flex-col'> |
| 58 | + {/* <div className='flex items-center py-4'> |
| 59 | + <div className='mr-2'> |
| 60 | + <Select onValueChange={handleStatusFilterChange}> |
| 61 | + <SelectTrigger className='w-[140px]'> |
| 62 | + <SelectValue placeholder='Status' /> |
| 63 | + </SelectTrigger> |
| 64 | + <SelectContent> |
| 65 | + <SelectItem value='all'>All</SelectItem> |
| 66 | + <SelectItem value='attempted'>Attempted</SelectItem> |
| 67 | + <SelectItem value='not-attempted'>Not attempted</SelectItem> |
| 68 | + </SelectContent> |
| 69 | + </Select> |
| 70 | + </div> |
| 71 | + <div className='mr-2'> |
| 72 | + <Select onValueChange={handleDifficultyFilterChange}> |
| 73 | + <SelectTrigger className='w-[140px]'> |
| 74 | + <SelectValue placeholder='Difficulty' /> |
| 75 | + </SelectTrigger> |
| 76 | + <SelectContent> |
| 77 | + <SelectItem value='all'>All</SelectItem> |
| 78 | + <SelectItem value='Easy'>Easy</SelectItem> |
| 79 | + <SelectItem value='Medium'>Medium</SelectItem> |
| 80 | + <SelectItem value='Hard'>Hard</SelectItem> |
| 81 | + </SelectContent> |
| 82 | + </Select> |
| 83 | + </div> |
| 84 | + <Input |
| 85 | + placeholder='Search questions...' |
| 86 | + value={(table.getColumn('title')?.getFilterValue() as string) ?? ''} |
| 87 | + onChange={(event) => table.getColumn('title')?.setFilterValue(event.target.value)} |
| 88 | + className='max-w-sm' |
| 89 | + /> |
| 90 | + </div> */} |
| 91 | + <div className='border-border rounded-md border'> |
| 92 | + <Table> |
| 93 | + <TableHeader> |
| 94 | + {table.getHeaderGroups().map((headerGroup) => ( |
| 95 | + <TableRow |
| 96 | + className='border-border/60 bg-primary-foreground text-primary' |
| 97 | + key={headerGroup.id} |
| 98 | + > |
| 99 | + {headerGroup.headers.map((header) => { |
| 100 | + return ( |
| 101 | + <TableHead key={header.id}> |
| 102 | + {header.isPlaceholder |
| 103 | + ? null |
| 104 | + : flexRender(header.column.columnDef.header, header.getContext())} |
| 105 | + </TableHead> |
| 106 | + ); |
| 107 | + })} |
| 108 | + </TableRow> |
| 109 | + ))} |
| 110 | + </TableHeader> |
| 111 | + <TableBody> |
| 112 | + {!isError && table.getRowModel().rows?.length ? ( |
| 113 | + table.getRowModel().rows.map((row) => ( |
| 114 | + <TableRow key={row.id} className='border-border/60 even:bg-secondary/10'> |
| 115 | + {row.getVisibleCells().map((cell) => ( |
| 116 | + <TableCell key={cell.id}> |
| 117 | + {flexRender(cell.column.columnDef.cell, cell.getContext())} |
| 118 | + </TableCell> |
| 119 | + ))} |
| 120 | + </TableRow> |
| 121 | + )) |
| 122 | + ) : ( |
| 123 | + <TableRow> |
| 124 | + <TableCell colSpan={columns.length} className='h-24 text-center'> |
| 125 | + No results. |
| 126 | + </TableCell> |
| 127 | + </TableRow> |
| 128 | + )} |
| 129 | + </TableBody> |
| 130 | + </Table> |
| 131 | + </div> |
| 132 | + <Pagination className='flex items-center justify-end space-x-2 py-4'> |
| 133 | + <PaginationContent> |
| 134 | + <PaginationItem> |
| 135 | + <Button |
| 136 | + variant='outline' |
| 137 | + size='sm' |
| 138 | + onClick={() => table.firstPage()} |
| 139 | + disabled={!table.getCanPreviousPage()} |
| 140 | + > |
| 141 | + <DoubleArrowLeftIcon /> |
| 142 | + </Button> |
| 143 | + </PaginationItem> |
| 144 | + <PaginationItem className='mr-1'> |
| 145 | + <Button |
| 146 | + variant='outline' |
| 147 | + size='sm' |
| 148 | + onClick={() => table.previousPage()} |
| 149 | + disabled={!table.getCanPreviousPage()} |
| 150 | + > |
| 151 | + <ArrowLeftIcon /> |
| 152 | + </Button> |
| 153 | + </PaginationItem> |
| 154 | + <PaginationItem className='text-sm'> |
| 155 | + {table.getState().pagination.pageIndex + 1} of {table.getPageCount()} |
| 156 | + </PaginationItem> |
| 157 | + <PaginationItem className='ml-1'> |
| 158 | + <Button |
| 159 | + variant='outline' |
| 160 | + size='sm' |
| 161 | + onClick={() => table.nextPage()} |
| 162 | + disabled={!table.getCanNextPage()} |
| 163 | + > |
| 164 | + <ArrowRightIcon /> |
| 165 | + </Button> |
| 166 | + </PaginationItem> |
| 167 | + <PaginationItem> |
| 168 | + <Button |
| 169 | + variant='outline' |
| 170 | + size='sm' |
| 171 | + onClick={() => table.lastPage()} |
| 172 | + disabled={!table.getCanNextPage()} |
| 173 | + > |
| 174 | + <DoubleArrowRightIcon /> |
| 175 | + </Button> |
| 176 | + </PaginationItem> |
| 177 | + </PaginationContent> |
| 178 | + </Pagination> |
| 179 | + </div> |
| 180 | + ); |
| 181 | +}; |
0 commit comments