import { PriceList } from "@medusajs/medusa" import { useEffect, useState } from "react" import { Column, HeaderGroup, Row, TableOptions, usePagination, useRowSelect, useSortBy, UseSortByColumnProps, useTable, } from "react-table" import { useTranslation } from "react-i18next" import { useDebounce } from "../../../hooks/use-debounce" import Table, { TableProps } from "../../molecules/table" import TableContainer from "../../organisms/table-container" import { usePriceListFilters } from "./use-price-list-filters" /* ******************************************** */ /* ************** TABLE ELEMENTS ************** */ /* ******************************************** */ type HeaderCellProps = { col: HeaderGroup & UseSortByColumnProps } /* * Renders react-table cell for the price lists table. */ function PriceListTableHeaderCell(props: HeaderCellProps) { return ( {props.col.render("Header")} ) } type HeaderRowProps = { headerGroup: HeaderGroup } /* * Renders react-table header row for the price list table. */ function PriceListTableHeaderRow(props: HeaderRowProps) { return ( {props.headerGroup.headers.map((col) => ( ))} ) } type PriceListTableRowProps = { row: Row } /* * Render react-table row for the price lists table. */ function PriceListTableRow(props: PriceListTableRowProps) { const { row } = props return ( {row.cells.map((cell, index) => cell.render("Cell", { index }))} ) } /* ******************************************** */ /* ************* TABLE CONTAINERS ************* */ /* ******************************************** */ type PriceListTableProps = ReturnType & { isLoading?: boolean priceLists: PriceList[] columns: Array> count: number options: Omit & { filter: Pick } } /* * Root component of the price lists table. */ export function PriceListTable(props: PriceListTableProps) { const [query, setQuery] = useState("") const { priceLists, queryObject, count, paginate, setQuery: setFreeText, columns, options, isLoading, } = props const tableConfig: TableOptions = { columns: columns, data: priceLists || [], initialState: { pageSize: queryObject.limit, pageIndex: queryObject.offset / queryObject.limit, }, pageCount: Math.ceil(count / queryObject.limit), manualPagination: true, autoResetPage: false, } const { t } = useTranslation() const table = useTable(tableConfig, useSortBy, usePagination, useRowSelect) // ********* HANDLERS ********* const handleNext = () => { if (!table.canNextPage) { return } paginate(1) table.nextPage() } const handlePrev = () => { if (!table.canPreviousPage) { return } paginate(-1) table.previousPage() } const debouncedSearchTerm = useDebounce(query, 500) useEffect(() => { setFreeText(debouncedSearchTerm) table.gotoPage(0) // eslint-disable-next-line react-hooks/exhaustive-deps }, [debouncedSearchTerm]) // const debouncedSearch = React.useMemo(() => debounce(handleSearch, 300), []) // ********* RENDER ********* return ( {/* HEAD */} {table.headerGroups?.map((headerGroup, ind) => ( ))} {/* BODY */} {table.rows.map((row) => { table.prepareRow(row) return })}
) }