import { UseMutateFunction } from "@tanstack/react-query" import { HeaderGroup, Row, usePagination, useSortBy, useTable, } from "react-table" import { Customer } from "@medusajs/medusa" import { useTranslation } from "react-i18next" import { useNavigate } from "react-router-dom" import useQueryFilters from "../../../hooks/use-query-filters" import DetailsIcon from "../../fundamentals/details-icon" import TrashIcon from "../../fundamentals/icons/trash-icon" import Table from "../../molecules/table" import { FilteringOptionProps } from "../../molecules/table/filtering-option" import TableContainer from "../../organisms/table-container" import { CUSTOMER_GROUPS_CUSTOMERS_LIST_TABLE_COLUMNS } from "./config" /* ********************************** */ /* ************** TYPES ************* */ /* ********************************** */ type CustomersListTableHeaderRowProps = { headerGroup: HeaderGroup } interface CustomersListTableRowProps { row: Row removeCustomers: Function } type CustomersListTableProps = ReturnType & { count: number groupId: string customers: Customer[] filteringOptions: FilteringOptionProps[] removeCustomers: UseMutateFunction isLoading?: boolean } /* ********************************************* */ /* ************** TABLE COMPONENTS ************* */ /* ********************************************* */ /* * Renders customer group customers list header row. */ function CustomersListTableHeaderRow(props: CustomersListTableHeaderRowProps) { const { headerGroup } = props return ( {props.headerGroup.headers.map((col, index) => { const { render, getHeaderProps, getSortByToggleProps } = col const className = index ? "w-[100px]" : "w-[60px]" return ( {render("Header")} ) })} ) } interface CustomersListTableRowProps { row: Row removeCustomers: Function } /* * Renders customer group customers list table row. */ function CustomersListTableRow(props: CustomersListTableRowProps) { const { row, removeCustomers } = props const navigate = useNavigate() const { t } = useTranslation() const actions = [ { label: t("customer-group-table-details", "Details"), onClick: () => navigate(`/a/customers/${row.original.id}`), icon: , }, // { // label: "Send an email", // onClick: () => window.open(`mailto:${row.original.email}`), // icon: , // }, { label: t( "customer-group-table-delete-from-the-group", "Delete from the group" ), variant: "danger", onClick: () => removeCustomers({ customer_ids: [{ id: row.original.id }], }), icon: , }, ] return ( {props.row.cells.map((cell, index) => ( {cell.render("Cell", { index })} ))} ) } /* * Render a list of customers that belong to a customer group. */ function CustomersListTable(props: CustomersListTableProps) { const { t } = useTranslation() const { customers, removeCustomers, setQuery, paginate, filteringOptions, queryObject, count, isLoading, } = props const tableConfig = { data: customers, columns: CUSTOMER_GROUPS_CUSTOMERS_LIST_TABLE_COLUMNS, initialState: { pageSize: queryObject.limit, pageIndex: queryObject.offset / queryObject.limit, }, pageCount: Math.ceil(count / queryObject.limit), manualPagination: true, autoResetPage: false, } const table = useTable(tableConfig, useSortBy, usePagination) // ********* HANDLERS ********* const handleNext = () => { if (!table.canNextPage) { return } paginate(1) table.nextPage() } const handlePrev = () => { if (!table.canPreviousPage) { return } paginate(-1) table.previousPage() } const handleSearch = (text: string) => { setQuery(text) if (text) { table.gotoPage(0) } } return ( {table.headerGroups?.map((headerGroup, index) => ( ))} {table.rows.map((row) => { table.prepareRow(row) return ( ) })}
) } export default CustomersListTable