Displaying a list of items on a Nextjs page from Supabase
Budget: $10 – $30 USD
I need someone to help me remotely on anydesk to solve my nextjs problem. I have a nextjs page to display a list of items from supabase. And for some reasons, I can't get this list.
Here is a sample of my code:
export function PatientTable() {
const [patients, setPatients] = React.useState([])
React.useEffect(() => {
getPatients()
}, [])
async function getPatients() {
console.log('getPatients')
const { data, error } = await supabaseClient.from('patient').select()
if (!error) setPatients(data as [])
}
const [sorting, setSorting] = React.useState<SortingState>([])
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
[]
)
const [columnVisibility, setColumnVisibility] =
React.useState<VisibilityState>({})
const [rowSelection, setRowSelection] = React.useState({})
const table = useReactTable({
data: patients,
columns,
onSortingChange: setSorting,
onColumnFiltersChange: setColumnFilters,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
getSortedRowModel: getSortedRowModel(),
getFilteredRowModel: getFilteredRowModel(),
onColumnVisibilityChange: setColumnVisibility,
onRowSelectionChange: setRowSelection,
state: {
sorting,
columnFilters,
columnVisibility,
rowSelection,
},
})
return (
<>
<div className='flex items-center flex-wrap gap-2 px-4'>
<Input
placeholder='Search patient...'
value={(table.getColumn('name')?.getFilterValue() as string) || ''}
onChange={(event) =>
table.getColumn('name')?.setFilterValue(event.target.value)
}
className='max-w-sm min-w-[200px] h-10'
/>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant='outline'>
Columns <ChevronDown className='ml-2 h-4 w-4' />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align='end'>
{table
.getAllColumns()
.filter((column) => column.getCanHide())
.map((column) => {
return (
<DropdownMenuCheckboxItem
key={column.id}
className='capitalize'
checked={column.getIsVisible()}
onCheckedChange={(value) =>
column.toggleVisibility(!!value)
}
>
{column.id}
</DropdownMenuCheckboxItem>
)
})}
</DropdownMenuContent>
</DropdownMenu>
</div>
<div>
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && 'selected'}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className='h-24 text-center'
>
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className='flex items-center flex-wrap gap-4 px-4 py-4'>
<div className='flex-1 text-sm text-muted-foreground whitespace-nowrap'>
{table.getFilteredSelectedRowModel().rows.length} of{' '}
{table.getFilteredRowModel().rows.length} row(s) selected.
</div>
<div className='flex gap-2 items-center'>
<Button
variant='outline'
size='icon'
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
className='h-8 w-8'
>
<Icon
icon='heroicons:chevron-left'
className='w-5 h-5 rtl:rotate-180'
/>
</Button>
{table.getPageOptions().map((page, pageIdx) => (
<Button
key={`basic-data-table-${pageIdx}`}
onClick={() => table.setPageIndex(pageIdx)}
className={cn('w-8 h-8')}
>
{page + 1}
</Button>
))}
<Button
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
variant='outline'
size='icon'
className='h-8 w-8'
>
<Icon
icon='heroicons:chevron-right'
className='w-5 h-5 rtl:rotate-180'
/>
</Button>
</div>
</div>
</>
)
}
Here is a sample of my code:
export function PatientTable() {
const [patients, setPatients] = React.useState([])
React.useEffect(() => {
getPatients()
}, [])
async function getPatients() {
console.log('getPatients')
const { data, error } = await supabaseClient.from('patient').select()
if (!error) setPatients(data as [])
}
const [sorting, setSorting] = React.useState<SortingState>([])
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
[]
)
const [columnVisibility, setColumnVisibility] =
React.useState<VisibilityState>({})
const [rowSelection, setRowSelection] = React.useState({})
const table = useReactTable({
data: patients,
columns,
onSortingChange: setSorting,
onColumnFiltersChange: setColumnFilters,
getCoreRowModel: getCoreRowModel(),
getPaginationRowModel: getPaginationRowModel(),
getSortedRowModel: getSortedRowModel(),
getFilteredRowModel: getFilteredRowModel(),
onColumnVisibilityChange: setColumnVisibility,
onRowSelectionChange: setRowSelection,
state: {
sorting,
columnFilters,
columnVisibility,
rowSelection,
},
})
return (
<>
<div className='flex items-center flex-wrap gap-2 px-4'>
<Input
placeholder='Search patient...'
value={(table.getColumn('name')?.getFilterValue() as string) || ''}
onChange={(event) =>
table.getColumn('name')?.setFilterValue(event.target.value)
}
className='max-w-sm min-w-[200px] h-10'
/>
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant='outline'>
Columns <ChevronDown className='ml-2 h-4 w-4' />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align='end'>
{table
.getAllColumns()
.filter((column) => column.getCanHide())
.map((column) => {
return (
<DropdownMenuCheckboxItem
key={column.id}
className='capitalize'
checked={column.getIsVisible()}
onCheckedChange={(value) =>
column.toggleVisibility(!!value)
}
>
{column.id}
</DropdownMenuCheckboxItem>
)
})}
</DropdownMenuContent>
</DropdownMenu>
</div>
<div>
<Table>
<TableHeader>
{table.getHeaderGroups().map((headerGroup) => (
<TableRow key={headerGroup.id}>
{headerGroup.headers.map((header) => {
return (
<TableHead key={header.id}>
{header.isPlaceholder
? null
: flexRender(
header.column.columnDef.header,
header.getContext()
)}
</TableHead>
)
})}
</TableRow>
))}
</TableHeader>
<TableBody>
{table.getRowModel().rows?.length ? (
table.getRowModel().rows.map((row) => (
<TableRow
key={row.id}
data-state={row.getIsSelected() && 'selected'}
>
{row.getVisibleCells().map((cell) => (
<TableCell key={cell.id}>
{flexRender(
cell.column.columnDef.cell,
cell.getContext()
)}
</TableCell>
))}
</TableRow>
))
) : (
<TableRow>
<TableCell
colSpan={columns.length}
className='h-24 text-center'
>
No results.
</TableCell>
</TableRow>
)}
</TableBody>
</Table>
</div>
<div className='flex items-center flex-wrap gap-4 px-4 py-4'>
<div className='flex-1 text-sm text-muted-foreground whitespace-nowrap'>
{table.getFilteredSelectedRowModel().rows.length} of{' '}
{table.getFilteredRowModel().rows.length} row(s) selected.
</div>
<div className='flex gap-2 items-center'>
<Button
variant='outline'
size='icon'
onClick={() => table.previousPage()}
disabled={!table.getCanPreviousPage()}
className='h-8 w-8'
>
<Icon
icon='heroicons:chevron-left'
className='w-5 h-5 rtl:rotate-180'
/>
</Button>
{table.getPageOptions().map((page, pageIdx) => (
<Button
key={`basic-data-table-${pageIdx}`}
onClick={() => table.setPageIndex(pageIdx)}
className={cn('w-8 h-8')}
>
{page + 1}
</Button>
))}
<Button
onClick={() => table.nextPage()}
disabled={!table.getCanNextPage()}
variant='outline'
size='icon'
className='h-8 w-8'
>
<Icon
icon='heroicons:chevron-right'
className='w-5 h-5 rtl:rotate-180'
/>
</Button>
</div>
</div>
</>
)
}