import { Customer } from "@medusajs/medusa" import { useAdminCustomerGroups, useAdminCustomers } from "medusa-react" import { useEffect, useState } from "react" import { useTranslation } from "react-i18next" import { HeaderGroup, Row, usePagination, useRowSelect, useTable, } from "react-table" import useQueryFilters from "../../../hooks/use-query-filters" import Button from "../../fundamentals/button" import Modal from "../../molecules/modal" import Table from "../../molecules/table" import TableContainer from "../../organisms/table-container" import { CUSTOMER_GROUPS_CUSTOMERS_TABLE_COLUMNS } from "./config" /** * Default filtering config for querying customers endpoint. */ const defaultQueryProps = { additionalFilters: { expand: "groups" }, limit: 15, } type EditCustomersTableHeaderRowProps = { headerGroup: HeaderGroup } /* * Edit customers table header row. */ function EditCustomersTableHeaderRow(props: EditCustomersTableHeaderRowProps) { const { headerGroup } = props return ( {headerGroup.headers.map((col, index) => ( {col.render("Header")} ))} ) } type EditCustomersTableRowProps = { row: Row } /* * Edit customers table row. */ function EditCustomersTableRow(props: EditCustomersTableRowProps) { return ( {props.row.cells.map((cell, index) => ( {cell.render("Cell", { index })} ))} ) } type EditCustomersTableProps = { onClose: () => void handleSubmit: () => void selectedCustomerIds: string[] setSelectedCustomerIds: (customerIds: string[]) => void } /* * Container for the "edit customers" table. */ function EditCustomersTable(props: EditCustomersTableProps) { const { t } = useTranslation() const { setSelectedCustomerIds, selectedCustomerIds, handleSubmit, onClose } = props const { paginate, setQuery, setFilters, filters, queryObject } = useQueryFilters(defaultQueryProps) const [numPages, setNumPages] = useState(0) const [activeGroupId, setActiveGroupId] = useState() const { customer_groups } = useAdminCustomerGroups({ expand: "customers" }) const { customers = [], count = 0, isLoading, } = useAdminCustomers({ ...queryObject, groups: activeGroupId ? [activeGroupId] : null, }) useEffect(() => { if (typeof count !== "undefined") { const controlledPageCount = Math.ceil(count / queryObject.limit) setNumPages(controlledPageCount) } }, [count]) const tableConfig = { columns: CUSTOMER_GROUPS_CUSTOMERS_TABLE_COLUMNS, data: customers, initialState: { pageSize: queryObject.limit, pageIndex: queryObject.offset / queryObject.limit, selectedRowIds: selectedCustomerIds.reduce((prev, id) => { prev[id] = true return prev }, {}), }, pageCount: numPages, autoResetSelectedRows: false, manualPagination: true, autoResetPage: false, getRowId: (row) => row.id, } const table = useTable(tableConfig, usePagination, useRowSelect) useEffect(() => { setSelectedCustomerIds(Object.keys(table.state.selectedRowIds)) }, [table.state.selectedRowIds]) useEffect(() => { setFilters("offset", 0) table.gotoPage(0) }, [activeGroupId]) const filteringOptions = [ { title: t("customer-group-table-groups", "Groups"), options: [ { title: t("customer-group-table-all", "All"), onClick: () => setActiveGroupId(null), }, ...(customer_groups || []).map((g) => ({ title: g.name, count: g.customers.length, onClick: () => setActiveGroupId(g.id), })), ], }, ] 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 (

{t("customer-group-table-edit-customers", "Edit Customers")}

{table.headerGroups?.map((headerGroup) => ( ))} {table.rows.map((row) => { table.prepareRow(row) return })}
) } export default EditCustomersTable