Untitled

 avatar
unknown
plain_text
a year ago
2.4 kB
4
Indexable
import Action, { Option } from '@/app/admin/(private)/components/Actions'
import { Response } from '@/types'
import { MantineReactTable, useMantineReactTable, type MRT_ColumnDef } from 'mantine-react-table'
import { useSession } from 'next-auth/react'
import { usePathname, useRouter, useSearchParams } from 'next/navigation'
import { useMemo } from 'react'

type props = {
  response: Response
  structure: any
  options: Option[]
  tableProperties?: any
  mainAction?: React.ReactNode
}

export default function Table({ response, structure, options, tableProperties = {}, mainAction }: props) {
  const { data: session }: any = useSession()
  const { replace } = useRouter()
  const pathname = usePathname()
  const searchParams = useSearchParams()

  // const pageNo: number = parseInt(searchParams.get('page') || '1') - 1
  // const limit: number = parseInt(searchParams.get('limit') || '10')

  const { data, pagination } = response
  /*** comment **/
  // const [page, setPage] = useState<MRT_PaginationState>({
  //   pageIndex: paginate?.page as number,
  //   pageSize: paginate?.limit as number,
  // })

  // const handleFilter = (keyword: string) => {
  //   replace(`${pathname}?search=${keyword || ''}`)
  // }

  // useEffect(() => {
  //   const paginatedPath = addQueryParams(pathname, page.pageIndex + 1, page.pageSize)
  //   replace(paginatedPath)
  // }, [page.pageIndex, page.pageSize, pathname, replace, session?.accessToken])

  const columns = useMemo<MRT_ColumnDef[]>(() => structure, [structure])

  const table = useMantineReactTable({
    columns,
    data,
    manualPagination: pagination,
    mantinePaginationProps: {
      rowsPerPageOptions: ['10', '25', '40'],
      withEdges: false,
    },
    rowCount: pagination && pagination.totalDocuments,
    // /***comment**/
    // state: { pagination: page },
    // onPaginationChange: setPagination,
    enableFullScreenToggle: false,
    enableDensityToggle: false,
    enableHiding: false,
    ...tableProperties,
    enableRowActions: true,
    positionActionsColumn: 'last',
    manualFiltering: true,
    // onGlobalFilterChange: handleFilter,
    renderRowActions: ({ row }) => <Action rowData={row.original} options={options} mainAction={mainAction} />,
  })

  return <MantineReactTable table={table} />
}
Editor is loading...
Leave a Comment