257 lines
7.8 KiB
TypeScript
257 lines
7.8 KiB
TypeScript
// src/app/dashboard/customers/page.tsx
|
|
'use client';
|
|
|
|
// RULES:
|
|
// contains list page for customers (Customers)
|
|
// contain definition to collection only
|
|
//
|
|
import * as React from 'react';
|
|
import { useRouter } from 'next/navigation';
|
|
import { COL_CUSTOMERS } from '@/constants';
|
|
import { LoadingButton } from '@mui/lab';
|
|
import Box from '@mui/material/Box';
|
|
import Button from '@mui/material/Button';
|
|
import Card from '@mui/material/Card';
|
|
import Divider from '@mui/material/Divider';
|
|
import Stack from '@mui/material/Stack';
|
|
import Typography from '@mui/material/Typography';
|
|
import { Plus as PlusIcon } from '@phosphor-icons/react/dist/ssr/Plus';
|
|
import type { ListResult, RecordModel } from 'pocketbase';
|
|
|
|
import { config } from '@/config';
|
|
import { CustomersFilters } from '@/components/dashboard/customer/customers-filters';
|
|
// import type { Filters } from '@/components/dashboard/customer/customers-filters';
|
|
import { CustomersPagination } from '@/components/dashboard/customer/customers-pagination';
|
|
import { CustomersSelectionProvider } from '@/components/dashboard/customer/customers-selection-context';
|
|
import { CustomersTable } from '@/components/dashboard/customer/customers-table';
|
|
import type { Customer, Filters } from '@/components/dashboard/customer/type.d';
|
|
import { SampleCustomers } from './SampleCustomers';
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { paths } from '@/paths';
|
|
import isDevelopment from '@/lib/check-is-development';
|
|
import { logger } from '@/lib/default-logger';
|
|
import { pb } from '@/lib/pb';
|
|
import { toast } from '@/components/core/toaster';
|
|
import ErrorDisplay from '@/components/dashboard/error';
|
|
import { defaultCustomer } from '@/components/dashboard/customer/_constants';
|
|
import FormLoading from '@/components/loading';
|
|
|
|
export default function Page({ searchParams }: PageProps): React.JSX.Element {
|
|
const { t } = useTranslation(['customers']);
|
|
const router = useRouter();
|
|
|
|
const { email, phone, sortDir, status } = searchParams;
|
|
|
|
const [lessonCategoriesData, setLessonCategoriesData] = React.useState<Customer[]>([]);
|
|
//
|
|
|
|
const [isLoadingAddPage, setIsLoadingAddPage] = React.useState<boolean>(false);
|
|
const [showLoading, setShowLoading] = React.useState<boolean>(true);
|
|
const [showError, setShowError] = React.useState({ show: false, detail: '' });
|
|
//
|
|
const [rowsPerPage, setRowsPerPage] = React.useState<number>(5);
|
|
//
|
|
const [f, setF] = React.useState<Customer[]>([]);
|
|
const [currentPage, setCurrentPage] = React.useState<number>(0);
|
|
//
|
|
const [recordCount, setRecordCount] = React.useState<number>(0);
|
|
const [listOption, setListOption] = React.useState({});
|
|
const [listSort, setListSort] = React.useState({});
|
|
|
|
//
|
|
// const sortedCustomers = applySort(SampleCustomers, sortDir);
|
|
// const filteredCustomers = applyFilters(sortedCustomers, { email, phone, status });
|
|
|
|
//
|
|
const reloadRows = async (): Promise<void> => {
|
|
try {
|
|
const models: ListResult<RecordModel> = await pb
|
|
.collection(COL_CUSTOMERS)
|
|
.getList(currentPage + 1, rowsPerPage, listOption);
|
|
const { items, totalItems } = models;
|
|
const tempLessonTypes: Customer[] = items.map((lt) => {
|
|
return { ...defaultCustomer, ...lt };
|
|
});
|
|
|
|
setLessonCategoriesData(tempLessonTypes);
|
|
setRecordCount(totalItems);
|
|
setF(tempLessonTypes);
|
|
// console.log({ currentPage, f });
|
|
} catch (error) {
|
|
//
|
|
logger.error(error);
|
|
setShowError({
|
|
//
|
|
show: true,
|
|
detail: JSON.stringify(error, null, 2),
|
|
});
|
|
} finally {
|
|
setShowLoading(false);
|
|
}
|
|
};
|
|
|
|
const [lastListOption, setLastListOption] = React.useState({});
|
|
const isFirstRun = React.useRef(false);
|
|
React.useEffect(() => {
|
|
if (!isFirstRun.current) {
|
|
isFirstRun.current = true;
|
|
} else if (JSON.stringify(listOption) !== JSON.stringify(lastListOption)) {
|
|
// reset page number as tab changes
|
|
setLastListOption(listOption);
|
|
setCurrentPage(0);
|
|
void reloadRows();
|
|
} else {
|
|
void reloadRows();
|
|
}
|
|
}, [currentPage, rowsPerPage, listOption]);
|
|
|
|
React.useEffect(() => {
|
|
let tempFilter = [],
|
|
tempSortDir = '';
|
|
|
|
if (status) {
|
|
tempFilter.push(`status = "${status}"`);
|
|
}
|
|
|
|
if (sortDir) {
|
|
tempSortDir = `-created`;
|
|
}
|
|
|
|
if (email) {
|
|
tempFilter.push(`email ~ "%${email}%"`);
|
|
}
|
|
if (phone) {
|
|
tempFilter.push(`phone ~ "%${phone}%"`);
|
|
}
|
|
|
|
let preFinalListOption = {};
|
|
if (tempFilter.length > 0) {
|
|
preFinalListOption = { filter: tempFilter.join(' && ') };
|
|
}
|
|
if (tempSortDir.length > 0) {
|
|
preFinalListOption = { ...preFinalListOption, sort: tempSortDir };
|
|
}
|
|
setListOption(preFinalListOption);
|
|
// setListOption({
|
|
// filter: tempFilter.join(' && '),
|
|
// sort: tempSortDir,
|
|
// //
|
|
// });
|
|
}, [sortDir, email, phone, status]);
|
|
|
|
if (showLoading) return <FormLoading />;
|
|
|
|
if (showError.show)
|
|
return (
|
|
<ErrorDisplay
|
|
message={t('error.unable-to-process-request')}
|
|
code={-1}
|
|
details={showError.detail}
|
|
/>
|
|
);
|
|
|
|
return (
|
|
<Box
|
|
sx={{
|
|
maxWidth: 'var(--Content-maxWidth)',
|
|
m: 'var(--Content-margin)',
|
|
p: 'var(--Content-padding)',
|
|
width: 'var(--Content-width)',
|
|
}}
|
|
>
|
|
<Stack spacing={4}>
|
|
<Stack
|
|
direction={{ xs: 'column', sm: 'row' }}
|
|
spacing={3}
|
|
sx={{ alignItems: 'flex-start' }}
|
|
>
|
|
<Box sx={{ flex: '1 1 auto' }}>
|
|
<Typography variant="h4">{t('list.title')}</Typography>
|
|
</Box>
|
|
<Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>
|
|
<LoadingButton
|
|
loading={isLoadingAddPage}
|
|
onClick={(): void => {
|
|
setIsLoadingAddPage(true);
|
|
router.push(paths.dashboard.customers.create);
|
|
}}
|
|
startIcon={<PlusIcon />}
|
|
variant="contained"
|
|
>
|
|
{t('list.add')}
|
|
</LoadingButton>
|
|
</Box>
|
|
</Stack>
|
|
<CustomersSelectionProvider customers={f}>
|
|
<Card>
|
|
<CustomersFilters
|
|
filters={{ email, phone, status }}
|
|
fullData={lessonCategoriesData}
|
|
sortDir={sortDir}
|
|
/>
|
|
<Divider />
|
|
<Box sx={{ overflowX: 'auto' }}>
|
|
<CustomersTable
|
|
reloadRows={reloadRows}
|
|
rows={f}
|
|
/>
|
|
</Box>
|
|
<Divider />
|
|
<CustomersPagination
|
|
count={recordCount}
|
|
page={currentPage}
|
|
rowsPerPage={rowsPerPage}
|
|
setPage={setCurrentPage}
|
|
setRowsPerPage={setRowsPerPage}
|
|
/>
|
|
</Card>
|
|
</CustomersSelectionProvider>
|
|
</Stack>
|
|
<Box sx={{ display: isDevelopment ? 'block' : 'none' }}>
|
|
<pre>{JSON.stringify(f, null, 2)}</pre>
|
|
</Box>
|
|
</Box>
|
|
);
|
|
}
|
|
|
|
// Sorting and filtering has to be done on the server.
|
|
|
|
function applySort(row: Customer[], sortDir: 'asc' | 'desc' | undefined): Customer[] {
|
|
return row.sort((a, b) => {
|
|
if (sortDir === 'asc') {
|
|
return a.createdAt.getTime() - b.createdAt.getTime();
|
|
}
|
|
|
|
return b.createdAt.getTime() - a.createdAt.getTime();
|
|
});
|
|
}
|
|
|
|
function applyFilters(row: Customer[], { email, phone, status }: Filters): Customer[] {
|
|
return row.filter((item) => {
|
|
if (email) {
|
|
if (!item.email?.toLowerCase().includes(email.toLowerCase())) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (phone) {
|
|
if (!item.phone?.toLowerCase().includes(phone.toLowerCase())) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
if (status) {
|
|
if (item.status !== status) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
});
|
|
}
|
|
|
|
interface PageProps {
|
|
searchParams: { email?: string; phone?: string; sortDir?: 'asc' | 'desc'; status?: string };
|
|
}
|