'use client'; import isEqual from 'lodash/isEqual'; import { useState, useCallback } from 'react'; // @mui import { alpha } from '@mui/material/styles'; import Tab from '@mui/material/Tab'; import Tabs from '@mui/material/Tabs'; import Card from '@mui/material/Card'; import Table from '@mui/material/Table'; import Button from '@mui/material/Button'; import Container from '@mui/material/Container'; import TableBody from '@mui/material/TableBody'; import TableContainer from '@mui/material/TableContainer'; // routes import { paths } from 'src/routes/paths'; import { RouterLink } from 'src/routes/components'; // hooks import { useBoolean } from 'src/hooks/use-boolean'; // components import Label from 'src/components/label'; import Iconify from 'src/components/iconify'; import Scrollbar from 'src/components/scrollbar'; import { useSettingsContext } from 'src/components/settings'; import CustomBreadcrumbs from 'src/components/custom-breadcrumbs'; import { useTable, getComparator, emptyRows, TableNoData, TableEmptyRows, TableHeadCustom, TablePaginationCustom, } from 'src/components/table'; // types import { Customer, CustomerTableFilters, CustomerTableFilterValue } from 'src/schemas'; // import { useGetCustomers } from 'src/api/customer'; import CustomerTableRow from '../customer-table-row'; import CustomerTableFiltersResult from '../customer-table-filters-result'; import { USER_STATUS_OPTIONS } from '../customer-quick-edit-form'; // ---------------------------------------------------------------------- const STATUS_OPTIONS = [{ value: 'all', label: 'All' }, ...USER_STATUS_OPTIONS]; const TABLE_HEAD = [ { id: 'name', label: 'Name' }, { id: 'representative', label: 'Representative', width: 220 }, { id: 'email', label: 'Email' }, { id: 'status', label: 'Status', width: 100 }, { id: '', width: 88 }, ]; const defaultFilters: CustomerTableFilters = { name: '', role: [], status: 'all', }; // ---------------------------------------------------------------------- export default function CustomerListView() { const table = useTable({ defaultDense: true, defaultRowsPerPage: 25 }); const settings = useSettingsContext(); // const router = useRouter(); const confirm = useBoolean(); const { customers } = useGetCustomers(); // const [tableData, setTableData] = useState(customers || []); const [filters, setFilters] = useState(defaultFilters); const dataFiltered = applyFilter({ inputData: customers, comparator: getComparator(table.order, table.orderBy), filters, }); const dataInPage = dataFiltered.slice( table.page * table.rowsPerPage, table.page * table.rowsPerPage + table.rowsPerPage ); const denseHeight = table.dense ? 52 : 72; const canReset = !isEqual(defaultFilters, filters); const notFound = (!dataFiltered.length && canReset) || !dataFiltered.length; const handleFilters = useCallback( (name: string, value: CustomerTableFilterValue) => { table.onResetPage(); setFilters((prevState) => ({ ...prevState, [name]: value, })); }, [table] ); // const handleDeleteRow = useCallback( // (id: string) => { // const deleteRow = customers.filter((row) => row.id !== id); // table.onUpdatePageDeleteRow(dataInPage.length); // }, // [dataInPage.length, table] // ); // const handleDeleteRows = useCallback(() => { // const deleteRows = tableData.filter((row) => !table.selected.includes(row.id)); // setTableData(deleteRows); // table.onUpdatePageDeleteRows({ // totalRows: tableData.length, // totalRowsInPage: dataInPage.length, // totalRowsFiltered: dataFiltered.length, // }); // }, [dataFiltered.length, dataInPage.length, table, tableData]); // const handleEditRow = useCallback( // (id: string) => { // router.push(paths.dashboard.user.edit(id)); // }, // [router] // ); const handleFilterStatus = useCallback( (event: React.SyntheticEvent, newValue: string) => { handleFilters('status', newValue); }, [handleFilters] ); const handleResetFilters = useCallback(() => { setFilters(defaultFilters); }, []); return ( <> } > New Customer } sx={{ mb: { xs: 3, md: 5 }, }} /> `inset 0 -2px 0 0 ${alpha(theme.palette.grey[500], 0.08)}`, }} > {STATUS_OPTIONS.map((tab) => ( {tab.value === 'all' && customers.length} {tab.value === 'active' && customers.filter((customer) => customer.status === 'active').length} {tab.value === 'banned' && customers.filter((customer) => customer.status === 'banned').length} {tab.value === 'inactive' && customers.filter((customer) => customer.status === 'inactive').length} } /> ))} {canReset && ( )} {dataFiltered .slice( table.page * table.rowsPerPage, table.page * table.rowsPerPage + table.rowsPerPage ) .map((row) => ( {}} /> ))}
{/* Are you sure want to delete {table.selected.length} items? } action={ } /> */} ); } // ---------------------------------------------------------------------- function applyFilter({ inputData, comparator, filters, }: { inputData: Customer[]; comparator: (a: any, b: any) => number; filters: CustomerTableFilters; }) { const { name, status, role } = filters; const stabilizedThis = inputData.map((el, index) => [el, index] as const); stabilizedThis.sort((a, b) => { const order = comparator(a[0], b[0]); if (order !== 0) return order; return a[1] - b[1]; }); inputData = stabilizedThis.map((el) => el[0]); if (name) { inputData = inputData.filter( (user) => user.name.toLowerCase().indexOf(name.toLowerCase()) !== -1 ); } if (status !== 'all') { inputData = inputData.filter((user) => user.status === status); } return inputData; }