import { TableOptions, usePagination, useRowSelect, useTable, } from "react-table" import React, { forwardRef, useEffect, useImperativeHandle } from "react" import { SalesChannel } from "@medusajs/medusa" import { useTranslation, Translation } from "react-i18next" import Table from "../../../components/molecules/table" import TableContainer from "../../../components/organisms/table-container" import IndeterminateCheckbox from "../../../components/molecules/indeterminate-checkbox" import { TablePagination } from "../../../components/organisms/table-container/pagination" import clsx from "clsx" const LIMIT = 12 const COLUMNS = [ { width: 30, id: "selection", Header: ({ getToggleAllPageRowsSelectedProps }) => ( ), Cell: ({ row }) => { return ( e.stopPropagation()} className="flex w-[30px] justify-center" > ) }, }, { Header: {(t) => t("tables-title", "Title")}, accessor: "name", maxWidth: 160, width: 160, Cell: ({ row }) => { return ( {row.original.name} ) }, }, { Header: ( {(t) => t("tables-description", "Description")} ), accessor: "description", Cell: ({ row }) => { return ( {row.original.description} ) }, }, ] type SalesChannelTableProps = { query: string selectedChannels: Map setSelectedChannels: ( setter: (oldState: Map) => void ) => {} offset: number setOffset: (offset: number) => void data: SalesChannel[] isLoading: boolean count: number } /** * Sales channels select table. */ const SalesChannelTable = forwardRef( (props: SalesChannelTableProps, ref: React.Ref) => { const { t } = useTranslation() const { query, offset, data, isLoading, count, setOffset, selectedChannels, setSelectedChannels, } = props const tableConfig: TableOptions = { data, // @ts-ignore columns: COLUMNS, autoResetPage: false, manualPagination: true, autoResetSelectedRows: false, initialState: { pageIndex: Math.floor(offset / LIMIT), pageSize: LIMIT, selectedRowIds: Object.keys(selectedChannels).reduce((prev, k) => { prev[k] = true return prev }, {}), }, pageCount: Math.ceil(count / LIMIT), getRowId: (row) => row.id, } const table = useTable(tableConfig, usePagination, useRowSelect) useEffect(() => { setSelectedChannels((oldState) => { const newState = {} Object.keys(table.state.selectedRowIds).forEach((k) => { newState[k] = oldState[k] || data.find((d) => d.id === k) }) return newState }) }, [table.state.selectedRowIds, data]) useEffect(() => { setOffset(0) table.gotoPage(0) }, [query]) const handleNext = () => { if (table.canNextPage) { setOffset(offset + LIMIT) table.nextPage() } } const handlePrev = () => { if (table.canPreviousPage) { setOffset(Math.max(offset - LIMIT, 0)) table.previousPage() } } useImperativeHandle(ref, () => ({ toggleAllRowsSelected: table.toggleAllRowsSelected, toggleRowSelected: table.toggleRowSelected, })) return ( <> {table.headerGroups?.map((headerGroup) => ( {headerGroup.headers.map((col) => ( {col.render("Header")} ))} ))} {table.rows.map((row) => { table.prepareRow(row) return ( table.toggleRowSelected(row.id)} className={clsx("cursor-pointer", { "bg-grey-5": row.isSelected, })} {...row.getRowProps()} > {row.cells.map((cell) => { return ( {cell.render("Cell")} ) })} ) })}
{!isLoading && !data?.length && (
{t("tables-no-added-sales-channels", "No added sales channels")}
)}
) } ) export default SalesChannelTable