update for customers,
This commit is contained in:
157
002_source/cms/src/app/dashboard/customers/customers.tsx
Normal file
157
002_source/cms/src/app/dashboard/customers/customers.tsx
Normal file
@@ -0,0 +1,157 @@
|
|||||||
|
// src/app/dashboard/customers/page.tsx
|
||||||
|
'use client';
|
||||||
|
import type { Customer } from '@/components/dashboard/customer/type.d';
|
||||||
|
import { dayjs } from '@/lib/dayjs';
|
||||||
|
|
||||||
|
export const SampleCustomers = [
|
||||||
|
{
|
||||||
|
id: 'USR-005',
|
||||||
|
name: 'Fran Perez',
|
||||||
|
avatar: '/assets/avatar-5.png',
|
||||||
|
email: 'fran.perez@domain.com',
|
||||||
|
phone: '(815) 704-0045',
|
||||||
|
quota: 50,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(1, 'hour').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-004',
|
||||||
|
name: 'Penjani Inyene',
|
||||||
|
avatar: '/assets/avatar-4.png',
|
||||||
|
email: 'penjani.inyene@domain.com',
|
||||||
|
phone: '(803) 937-8925',
|
||||||
|
quota: 100,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(3, 'hour').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-003',
|
||||||
|
name: 'Carson Darrin',
|
||||||
|
avatar: '/assets/avatar-3.png',
|
||||||
|
email: 'carson.darrin@domain.com',
|
||||||
|
phone: '(715) 278-5041',
|
||||||
|
quota: 10,
|
||||||
|
status: 'blocked',
|
||||||
|
createdAt: dayjs().subtract(1, 'hour').subtract(1, 'day').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-002',
|
||||||
|
name: 'Siegbert Gottfried',
|
||||||
|
avatar: '/assets/avatar-2.png',
|
||||||
|
email: 'siegbert.gottfried@domain.com',
|
||||||
|
phone: '(603) 766-0431',
|
||||||
|
quota: 0,
|
||||||
|
status: 'pending',
|
||||||
|
createdAt: dayjs().subtract(7, 'hour').subtract(1, 'day').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-001',
|
||||||
|
name: 'Miron Vitold',
|
||||||
|
avatar: '/assets/avatar-1.png',
|
||||||
|
email: 'miron.vitold@domain.com',
|
||||||
|
phone: '(425) 434-5535',
|
||||||
|
quota: 50,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(2, 'hour').subtract(2, 'day').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-005',
|
||||||
|
name: 'Fran Perez',
|
||||||
|
avatar: '/assets/avatar-5.png',
|
||||||
|
email: 'fran.perez@domain.com',
|
||||||
|
phone: '(815) 704-0045',
|
||||||
|
quota: 50,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(1, 'hour').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-004',
|
||||||
|
name: 'Penjani Inyene',
|
||||||
|
avatar: '/assets/avatar-4.png',
|
||||||
|
email: 'penjani.inyene@domain.com',
|
||||||
|
phone: '(803) 937-8925',
|
||||||
|
quota: 100,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(3, 'hour').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-003',
|
||||||
|
name: 'Carson Darrin',
|
||||||
|
avatar: '/assets/avatar-3.png',
|
||||||
|
email: 'carson.darrin@domain.com',
|
||||||
|
phone: '(715) 278-5041',
|
||||||
|
quota: 10,
|
||||||
|
status: 'blocked',
|
||||||
|
createdAt: dayjs().subtract(1, 'hour').subtract(1, 'day').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-002',
|
||||||
|
name: 'Siegbert Gottfried',
|
||||||
|
avatar: '/assets/avatar-2.png',
|
||||||
|
email: 'siegbert.gottfried@domain.com',
|
||||||
|
phone: '(603) 766-0431',
|
||||||
|
quota: 0,
|
||||||
|
status: 'pending',
|
||||||
|
createdAt: dayjs().subtract(7, 'hour').subtract(1, 'day').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-001',
|
||||||
|
name: 'Miron Vitold',
|
||||||
|
avatar: '/assets/avatar-1.png',
|
||||||
|
email: 'miron.vitold@domain.com',
|
||||||
|
phone: '(425) 434-5535',
|
||||||
|
quota: 50,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(2, 'hour').subtract(2, 'day').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-005',
|
||||||
|
name: 'Fran Perez',
|
||||||
|
avatar: '/assets/avatar-5.png',
|
||||||
|
email: 'fran.perez@domain.com',
|
||||||
|
phone: '(815) 704-0045',
|
||||||
|
quota: 50,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(1, 'hour').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-004',
|
||||||
|
name: 'Penjani Inyene',
|
||||||
|
avatar: '/assets/avatar-4.png',
|
||||||
|
email: 'penjani.inyene@domain.com',
|
||||||
|
phone: '(803) 937-8925',
|
||||||
|
quota: 100,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(3, 'hour').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-003',
|
||||||
|
name: 'Carson Darrin',
|
||||||
|
avatar: '/assets/avatar-3.png',
|
||||||
|
email: 'carson.darrin@domain.com',
|
||||||
|
phone: '(715) 278-5041',
|
||||||
|
quota: 10,
|
||||||
|
status: 'blocked',
|
||||||
|
createdAt: dayjs().subtract(1, 'hour').subtract(1, 'day').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-002',
|
||||||
|
name: 'Siegbert Gottfried',
|
||||||
|
avatar: '/assets/avatar-2.png',
|
||||||
|
email: 'siegbert.gottfried@domain.com',
|
||||||
|
phone: '(603) 766-0431',
|
||||||
|
quota: 0,
|
||||||
|
status: 'pending',
|
||||||
|
createdAt: dayjs().subtract(7, 'hour').subtract(1, 'day').toDate(),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
id: 'USR-001',
|
||||||
|
name: 'Miron Vitold',
|
||||||
|
avatar: '/assets/avatar-1.png',
|
||||||
|
email: 'miron.vitold@domain.com',
|
||||||
|
phone: '(425) 434-5535',
|
||||||
|
quota: 50,
|
||||||
|
status: 'active',
|
||||||
|
createdAt: dayjs().subtract(2, 'hour').subtract(2, 'day').toDate(),
|
||||||
|
},
|
||||||
|
] satisfies Customer[];
|
@@ -1,7 +1,14 @@
|
|||||||
|
// src/app/dashboard/customers/page.tsx
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
|
// RULES:
|
||||||
|
// contains list page for customers (Customers)
|
||||||
|
// contain definition to collection only
|
||||||
|
//
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
// import type { Metadata } from 'next';
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { COL_CUSTOMERS } from '@/constants';
|
||||||
|
import { LoadingButton } from '@mui/lab';
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
import Card from '@mui/material/Card';
|
import Card from '@mui/material/Card';
|
||||||
@@ -9,184 +16,138 @@ import Divider from '@mui/material/Divider';
|
|||||||
import Stack from '@mui/material/Stack';
|
import Stack from '@mui/material/Stack';
|
||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import { Plus as PlusIcon } from '@phosphor-icons/react/dist/ssr/Plus';
|
import { Plus as PlusIcon } from '@phosphor-icons/react/dist/ssr/Plus';
|
||||||
|
import type { ListResult, RecordModel } from 'pocketbase';
|
||||||
|
|
||||||
import { config } from '@/config';
|
import { config } from '@/config';
|
||||||
import { dayjs } from '@/lib/dayjs';
|
|
||||||
import { CustomersFilters } from '@/components/dashboard/customer/customers-filters';
|
import { CustomersFilters } from '@/components/dashboard/customer/customers-filters';
|
||||||
import type { Filters } from '@/components/dashboard/customer/customers-filters';
|
// import type { Filters } from '@/components/dashboard/customer/customers-filters';
|
||||||
import { CustomersPagination } from '@/components/dashboard/customer/customers-pagination';
|
import { CustomersPagination } from '@/components/dashboard/customer/customers-pagination';
|
||||||
import { CustomersSelectionProvider } from '@/components/dashboard/customer/customers-selection-context';
|
import { CustomersSelectionProvider } from '@/components/dashboard/customer/customers-selection-context';
|
||||||
import { CustomersTable } from '@/components/dashboard/customer/customers-table';
|
import { CustomersTable } from '@/components/dashboard/customer/customers-table';
|
||||||
import type { Customer } from '@/components/dashboard/customer/customers-table';
|
import type { Customer, Filters } from '@/components/dashboard/customer/type.d';
|
||||||
|
import { SampleCustomers } from './customers';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
// export const metadata = { title: `List | Customers | Dashboard | ${config.site.name}` } satisfies Metadata;
|
import { paths } from '@/paths';
|
||||||
|
import isDevelopment from '@/lib/check-is-development';
|
||||||
const customers = [
|
import { logger } from '@/lib/default-logger';
|
||||||
{
|
import { pb } from '@/lib/pb';
|
||||||
id: 'USR-005',
|
import { toast } from '@/components/core/toaster';
|
||||||
name: 'Fran Perez',
|
import ErrorDisplay from '@/components/dashboard/error';
|
||||||
avatar: '/assets/avatar-5.png',
|
import { defaultCustomer } from '@/components/dashboard/customer/_constants';
|
||||||
email: 'fran.perez@domain.com',
|
import FormLoading from '@/components/loading';
|
||||||
phone: '(815) 704-0045',
|
|
||||||
quota: 50,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(1, 'hour').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-004',
|
|
||||||
name: 'Penjani Inyene',
|
|
||||||
avatar: '/assets/avatar-4.png',
|
|
||||||
email: 'penjani.inyene@domain.com',
|
|
||||||
phone: '(803) 937-8925',
|
|
||||||
quota: 100,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(3, 'hour').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-003',
|
|
||||||
name: 'Carson Darrin',
|
|
||||||
avatar: '/assets/avatar-3.png',
|
|
||||||
email: 'carson.darrin@domain.com',
|
|
||||||
phone: '(715) 278-5041',
|
|
||||||
quota: 10,
|
|
||||||
status: 'blocked',
|
|
||||||
createdAt: dayjs().subtract(1, 'hour').subtract(1, 'day').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-002',
|
|
||||||
name: 'Siegbert Gottfried',
|
|
||||||
avatar: '/assets/avatar-2.png',
|
|
||||||
email: 'siegbert.gottfried@domain.com',
|
|
||||||
phone: '(603) 766-0431',
|
|
||||||
quota: 0,
|
|
||||||
status: 'pending',
|
|
||||||
createdAt: dayjs().subtract(7, 'hour').subtract(1, 'day').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-001',
|
|
||||||
name: 'Miron Vitold',
|
|
||||||
avatar: '/assets/avatar-1.png',
|
|
||||||
email: 'miron.vitold@domain.com',
|
|
||||||
phone: '(425) 434-5535',
|
|
||||||
quota: 50,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(2, 'hour').subtract(2, 'day').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-005',
|
|
||||||
name: 'Fran Perez',
|
|
||||||
avatar: '/assets/avatar-5.png',
|
|
||||||
email: 'fran.perez@domain.com',
|
|
||||||
phone: '(815) 704-0045',
|
|
||||||
quota: 50,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(1, 'hour').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-004',
|
|
||||||
name: 'Penjani Inyene',
|
|
||||||
avatar: '/assets/avatar-4.png',
|
|
||||||
email: 'penjani.inyene@domain.com',
|
|
||||||
phone: '(803) 937-8925',
|
|
||||||
quota: 100,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(3, 'hour').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-003',
|
|
||||||
name: 'Carson Darrin',
|
|
||||||
avatar: '/assets/avatar-3.png',
|
|
||||||
email: 'carson.darrin@domain.com',
|
|
||||||
phone: '(715) 278-5041',
|
|
||||||
quota: 10,
|
|
||||||
status: 'blocked',
|
|
||||||
createdAt: dayjs().subtract(1, 'hour').subtract(1, 'day').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-002',
|
|
||||||
name: 'Siegbert Gottfried',
|
|
||||||
avatar: '/assets/avatar-2.png',
|
|
||||||
email: 'siegbert.gottfried@domain.com',
|
|
||||||
phone: '(603) 766-0431',
|
|
||||||
quota: 0,
|
|
||||||
status: 'pending',
|
|
||||||
createdAt: dayjs().subtract(7, 'hour').subtract(1, 'day').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-001',
|
|
||||||
name: 'Miron Vitold',
|
|
||||||
avatar: '/assets/avatar-1.png',
|
|
||||||
email: 'miron.vitold@domain.com',
|
|
||||||
phone: '(425) 434-5535',
|
|
||||||
quota: 50,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(2, 'hour').subtract(2, 'day').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-005',
|
|
||||||
name: 'Fran Perez',
|
|
||||||
avatar: '/assets/avatar-5.png',
|
|
||||||
email: 'fran.perez@domain.com',
|
|
||||||
phone: '(815) 704-0045',
|
|
||||||
quota: 50,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(1, 'hour').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-004',
|
|
||||||
name: 'Penjani Inyene',
|
|
||||||
avatar: '/assets/avatar-4.png',
|
|
||||||
email: 'penjani.inyene@domain.com',
|
|
||||||
phone: '(803) 937-8925',
|
|
||||||
quota: 100,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(3, 'hour').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-003',
|
|
||||||
name: 'Carson Darrin',
|
|
||||||
avatar: '/assets/avatar-3.png',
|
|
||||||
email: 'carson.darrin@domain.com',
|
|
||||||
phone: '(715) 278-5041',
|
|
||||||
quota: 10,
|
|
||||||
status: 'blocked',
|
|
||||||
createdAt: dayjs().subtract(1, 'hour').subtract(1, 'day').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-002',
|
|
||||||
name: 'Siegbert Gottfried',
|
|
||||||
avatar: '/assets/avatar-2.png',
|
|
||||||
email: 'siegbert.gottfried@domain.com',
|
|
||||||
phone: '(603) 766-0431',
|
|
||||||
quota: 0,
|
|
||||||
status: 'pending',
|
|
||||||
createdAt: dayjs().subtract(7, 'hour').subtract(1, 'day').toDate(),
|
|
||||||
},
|
|
||||||
{
|
|
||||||
id: 'USR-001',
|
|
||||||
name: 'Miron Vitold',
|
|
||||||
avatar: '/assets/avatar-1.png',
|
|
||||||
email: 'miron.vitold@domain.com',
|
|
||||||
phone: '(425) 434-5535',
|
|
||||||
quota: 50,
|
|
||||||
status: 'active',
|
|
||||||
createdAt: dayjs().subtract(2, 'hour').subtract(2, 'day').toDate(),
|
|
||||||
},
|
|
||||||
] satisfies Customer[];
|
|
||||||
|
|
||||||
interface PageProps {
|
|
||||||
searchParams: { email?: string; phone?: string; sortDir?: 'asc' | 'desc'; status?: string };
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function Page({ searchParams }: PageProps): React.JSX.Element {
|
export default function Page({ searchParams }: PageProps): React.JSX.Element {
|
||||||
|
const { t } = useTranslation(['customers']);
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
const { email, phone, sortDir, status } = searchParams;
|
const { email, phone, sortDir, status } = searchParams;
|
||||||
|
|
||||||
const sortedCustomers = applySort(customers, sortDir);
|
const [lessonCategoriesData, setLessonCategoriesData] = React.useState<Customer[]>([]);
|
||||||
const filteredCustomers = applyFilters(sortedCustomers, { email, phone, status });
|
//
|
||||||
|
|
||||||
|
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(() => {
|
React.useEffect(() => {
|
||||||
console.log('helloworld');
|
let tempFilter = [],
|
||||||
}, []);
|
tempSortDir = '';
|
||||||
|
|
||||||
|
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]);
|
||||||
|
|
||||||
|
if (showLoading) return <FormLoading />;
|
||||||
|
|
||||||
|
if (showError.show)
|
||||||
|
return (
|
||||||
|
<ErrorDisplay
|
||||||
|
message={t('error.unable-to-process-request')}
|
||||||
|
code={-1}
|
||||||
|
details={showError.detail}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
@@ -204,35 +165,50 @@ export default function Page({ searchParams }: PageProps): React.JSX.Element {
|
|||||||
sx={{ alignItems: 'flex-start' }}
|
sx={{ alignItems: 'flex-start' }}
|
||||||
>
|
>
|
||||||
<Box sx={{ flex: '1 1 auto' }}>
|
<Box sx={{ flex: '1 1 auto' }}>
|
||||||
<Typography variant="h4">Customers</Typography>
|
<Typography variant="h4">{t('list.title')}</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
<Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>
|
<Box sx={{ display: 'flex', justifyContent: 'flex-end' }}>
|
||||||
<Button
|
<LoadingButton
|
||||||
|
loading={isLoadingAddPage}
|
||||||
|
onClick={(): void => {
|
||||||
|
setIsLoadingAddPage(true);
|
||||||
|
router.push(paths.dashboard.customers.create);
|
||||||
|
}}
|
||||||
startIcon={<PlusIcon />}
|
startIcon={<PlusIcon />}
|
||||||
variant="contained"
|
variant="contained"
|
||||||
>
|
>
|
||||||
Add
|
{t('list.add')}
|
||||||
</Button>
|
</LoadingButton>
|
||||||
</Box>
|
</Box>
|
||||||
</Stack>
|
</Stack>
|
||||||
<CustomersSelectionProvider customers={filteredCustomers}>
|
<CustomersSelectionProvider customers={f}>
|
||||||
<Card>
|
<Card>
|
||||||
<CustomersFilters
|
<CustomersFilters
|
||||||
filters={{ email, phone, status }}
|
filters={{ email, phone, status }}
|
||||||
|
fullData={lessonCategoriesData}
|
||||||
sortDir={sortDir}
|
sortDir={sortDir}
|
||||||
/>
|
/>
|
||||||
<Divider />
|
<Divider />
|
||||||
<Box sx={{ overflowX: 'auto' }}>
|
<Box sx={{ overflowX: 'auto' }}>
|
||||||
<CustomersTable rows={filteredCustomers} />
|
<CustomersTable
|
||||||
|
reloadRows={reloadRows}
|
||||||
|
rows={f}
|
||||||
|
/>
|
||||||
</Box>
|
</Box>
|
||||||
<Divider />
|
<Divider />
|
||||||
<CustomersPagination
|
<CustomersPagination
|
||||||
count={filteredCustomers.length + 100}
|
count={recordCount}
|
||||||
page={0}
|
page={currentPage}
|
||||||
|
rowsPerPage={rowsPerPage}
|
||||||
|
setPage={setCurrentPage}
|
||||||
|
setRowsPerPage={setRowsPerPage}
|
||||||
/>
|
/>
|
||||||
</Card>
|
</Card>
|
||||||
</CustomersSelectionProvider>
|
</CustomersSelectionProvider>
|
||||||
</Stack>
|
</Stack>
|
||||||
|
<Box sx={{ display: isDevelopment ? 'block' : 'none' }}>
|
||||||
|
<pre>{JSON.stringify(f, null, 2)}</pre>
|
||||||
|
</Box>
|
||||||
</Box>
|
</Box>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
@@ -272,3 +248,7 @@ function applyFilters(row: Customer[], { email, phone, status }: Filters): Custo
|
|||||||
return true;
|
return true;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
interface PageProps {
|
||||||
|
searchParams: { email?: string; phone?: string; sortDir?: 'asc' | 'desc'; status?: string };
|
||||||
|
}
|
||||||
|
@@ -0,0 +1,17 @@
|
|||||||
|
import { dayjs } from '@/lib/dayjs';
|
||||||
|
import type { Customer } from './type.d';
|
||||||
|
|
||||||
|
export const defaultCustomer: Customer = {
|
||||||
|
id: '',
|
||||||
|
name: '',
|
||||||
|
avatar: undefined,
|
||||||
|
email: '',
|
||||||
|
phone: undefined,
|
||||||
|
quota: 0,
|
||||||
|
status: 'pending',
|
||||||
|
createdAt: dayjs().toDate(),
|
||||||
|
};
|
||||||
|
|
||||||
|
export const emptyLpCategory: Customer = {
|
||||||
|
...defaultCustomer,
|
||||||
|
};
|
@@ -0,0 +1,125 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import * as React from 'react';
|
||||||
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { COL_LESSON_TYPES } from '@/constants';
|
||||||
|
import deleteQuizLPCategories from '@/db/QuizListenings/Delete';
|
||||||
|
import { LoadingButton } from '@mui/lab';
|
||||||
|
import { Button, Container, Modal, Paper } from '@mui/material';
|
||||||
|
import Avatar from '@mui/material/Avatar';
|
||||||
|
import Box from '@mui/material/Box';
|
||||||
|
import Stack from '@mui/material/Stack';
|
||||||
|
import Typography from '@mui/material/Typography';
|
||||||
|
import { Note as NoteIcon } from '@phosphor-icons/react/dist/ssr/Note';
|
||||||
|
import PocketBase from 'pocketbase';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
|
import { logger } from '@/lib/default-logger';
|
||||||
|
import { toast } from '@/components/core/toaster';
|
||||||
|
|
||||||
|
export default function ConfirmDeleteModal({
|
||||||
|
open,
|
||||||
|
setOpen,
|
||||||
|
idToDelete,
|
||||||
|
reloadRows,
|
||||||
|
}: {
|
||||||
|
open: boolean;
|
||||||
|
setOpen: (b: boolean) => void;
|
||||||
|
idToDelete: string;
|
||||||
|
reloadRows: () => void;
|
||||||
|
}): React.JSX.Element {
|
||||||
|
const { t } = useTranslation();
|
||||||
|
|
||||||
|
// const handleClose = () => setOpen(false);
|
||||||
|
function handleClose(): void {
|
||||||
|
setOpen(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
const [isDeleteing, setIsDeleteing] = React.useState(false);
|
||||||
|
const style = {
|
||||||
|
position: 'absolute',
|
||||||
|
top: '50%',
|
||||||
|
left: '50%',
|
||||||
|
transform: 'translate(-50%, -50%)',
|
||||||
|
};
|
||||||
|
|
||||||
|
function handleUserConfirmDelete(): void {
|
||||||
|
if (idToDelete) {
|
||||||
|
setIsDeleteing(true);
|
||||||
|
deleteQuizLPCategories(idToDelete)
|
||||||
|
.then(() => {
|
||||||
|
reloadRows();
|
||||||
|
handleClose();
|
||||||
|
toast(t('delete.success'));
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
// console.error(err)
|
||||||
|
logger.error(err);
|
||||||
|
toast(t('delete.error'));
|
||||||
|
})
|
||||||
|
.finally(() => {
|
||||||
|
setIsDeleteing(false);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div>
|
||||||
|
<Modal
|
||||||
|
open={open}
|
||||||
|
onClose={handleClose}
|
||||||
|
aria-labelledby="modal-modal-title"
|
||||||
|
aria-describedby="modal-modal-description"
|
||||||
|
>
|
||||||
|
<Box sx={style}>
|
||||||
|
<Container maxWidth="sm">
|
||||||
|
<Paper sx={{ border: '1px solid var(--mui-palette-divider)', boxShadow: 'var(--mui-shadows-16)' }}>
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
spacing={2}
|
||||||
|
sx={{ display: 'flex', p: 3 }}
|
||||||
|
>
|
||||||
|
<Avatar sx={{ bgcolor: 'var(--mui-palette-error-50)', color: 'var(--mui-palette-error-main)' }}>
|
||||||
|
<NoteIcon fontSize="var(--Icon-fontSize)" />
|
||||||
|
</Avatar>
|
||||||
|
<Stack spacing={3}>
|
||||||
|
<Stack spacing={1}>
|
||||||
|
<Typography variant="h5">{t('Delete Lesson Type ?')}</Typography>
|
||||||
|
<Typography
|
||||||
|
color="text.secondary"
|
||||||
|
variant="body2"
|
||||||
|
>
|
||||||
|
{t('Are you sure you want to delete lesson type ?')}
|
||||||
|
</Typography>
|
||||||
|
</Stack>
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
spacing={2}
|
||||||
|
sx={{ justifyContent: 'flex-end' }}
|
||||||
|
>
|
||||||
|
<Button
|
||||||
|
color="secondary"
|
||||||
|
onClick={handleClose}
|
||||||
|
>
|
||||||
|
{t('Cancel')}
|
||||||
|
</Button>
|
||||||
|
<LoadingButton
|
||||||
|
color="error"
|
||||||
|
variant="contained"
|
||||||
|
onClick={(e) => {
|
||||||
|
handleUserConfirmDelete();
|
||||||
|
}}
|
||||||
|
loading={isDeleteing}
|
||||||
|
>
|
||||||
|
{t('Delete')}
|
||||||
|
</LoadingButton>
|
||||||
|
</Stack>
|
||||||
|
</Stack>
|
||||||
|
</Stack>
|
||||||
|
</Paper>
|
||||||
|
</Container>
|
||||||
|
</Box>
|
||||||
|
</Modal>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
|
}
|
@@ -1,53 +1,72 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
// RULES:
|
||||||
|
// T.B.A.
|
||||||
|
//
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
|
import { getAllCustomersCount } from '@/db/Customers/GetAllCount';
|
||||||
|
|
||||||
import Button from '@mui/material/Button';
|
import Button from '@mui/material/Button';
|
||||||
import Chip from '@mui/material/Chip';
|
import Chip from '@mui/material/Chip';
|
||||||
import Divider from '@mui/material/Divider';
|
import Divider from '@mui/material/Divider';
|
||||||
import FormControl from '@mui/material/FormControl';
|
|
||||||
import OutlinedInput from '@mui/material/OutlinedInput';
|
|
||||||
import Select from '@mui/material/Select';
|
import Select from '@mui/material/Select';
|
||||||
import type { SelectChangeEvent } from '@mui/material/Select';
|
import type { SelectChangeEvent } from '@mui/material/Select';
|
||||||
import Stack from '@mui/material/Stack';
|
import Stack from '@mui/material/Stack';
|
||||||
import Tab from '@mui/material/Tab';
|
import Tab from '@mui/material/Tab';
|
||||||
import Tabs from '@mui/material/Tabs';
|
import Tabs from '@mui/material/Tabs';
|
||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
|
||||||
import { paths } from '@/paths';
|
import { paths } from '@/paths';
|
||||||
import { FilterButton, FilterPopover, useFilterContext } from '@/components/core/filter-button';
|
import { FilterButton } from '@/components/core/filter-button';
|
||||||
import { Option } from '@/components/core/option';
|
import { Option } from '@/components/core/option';
|
||||||
|
|
||||||
import { useCustomersSelection } from './customers-selection-context';
|
import { useCustomersSelection } from './customers-selection-context';
|
||||||
|
import GetBlockedCount from '@/db/Customers/GetBlockedCount';
|
||||||
|
import GetPendingCount from '@/db/Customers/GetPendingCount';
|
||||||
|
import GetActiveCount from '@/db/Customers/GetActiveCount';
|
||||||
|
import PhoneFilterPopover from './phone-filter-popover';
|
||||||
|
import EmailFilterPopover from './email-filter-popover';
|
||||||
|
import type { CustomersFiltersProps, Filters, SortDir } from './type.d';
|
||||||
|
|
||||||
// The tabs should be generated using API data.
|
export function CustomersFilters({
|
||||||
const tabs = [
|
filters = {},
|
||||||
{ label: 'All', value: '', count: 5 },
|
sortDir = 'desc',
|
||||||
{ label: 'Active', value: 'active', count: 3 },
|
fullData,
|
||||||
{ label: 'Pending', value: 'pending', count: 1 },
|
}: CustomersFiltersProps): React.JSX.Element {
|
||||||
{ label: 'Blocked', value: 'blocked', count: 1 },
|
const { t } = useTranslation();
|
||||||
] as const;
|
|
||||||
|
|
||||||
export interface Filters {
|
|
||||||
email?: string;
|
|
||||||
phone?: string;
|
|
||||||
status?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type SortDir = 'asc' | 'desc';
|
|
||||||
|
|
||||||
export interface CustomersFiltersProps {
|
|
||||||
filters?: Filters;
|
|
||||||
sortDir?: SortDir;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function CustomersFilters({ filters = {}, sortDir = 'desc' }: CustomersFiltersProps): React.JSX.Element {
|
|
||||||
const { email, phone, status } = filters;
|
const { email, phone, status } = filters;
|
||||||
|
|
||||||
|
const [totalCount, setTotalCount] = React.useState<number>(0);
|
||||||
|
const [activeCount, setActiveCount] = React.useState<number>(0);
|
||||||
|
const [pendingCount, setPendingCount] = React.useState<number>(0);
|
||||||
|
const [blockedCount, setBlockedCount] = React.useState<number>(0);
|
||||||
|
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
|
|
||||||
const selection = useCustomersSelection();
|
const selection = useCustomersSelection();
|
||||||
|
|
||||||
|
function getVisible(): number {
|
||||||
|
return fullData.reduce((count, item: CrQuestion) => {
|
||||||
|
return item.visible === 'visible' ? count + 1 : count;
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
function getHidden(): number {
|
||||||
|
return fullData.reduce((count, item: CrQuestion) => {
|
||||||
|
return item.visible === 'hidden' ? count + 1 : count;
|
||||||
|
}, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
// The tabs should be generated using API data.
|
||||||
|
const tabs = [
|
||||||
|
{ label: 'All', value: '', count: totalCount },
|
||||||
|
{ label: 'Active', value: 'active', count: activeCount },
|
||||||
|
{ label: 'Pending', value: 'pending', count: pendingCount },
|
||||||
|
{ label: 'Blocked', value: 'blocked', count: blockedCount },
|
||||||
|
] as const;
|
||||||
|
|
||||||
const updateSearchParams = React.useCallback(
|
const updateSearchParams = React.useCallback(
|
||||||
(newFilters: Filters, newSortDir: SortDir): void => {
|
(newFilters: Filters, newSortDir: SortDir): void => {
|
||||||
const searchParams = new URLSearchParams();
|
const searchParams = new URLSearchParams();
|
||||||
@@ -107,12 +126,43 @@ export function CustomersFilters({ filters = {}, sortDir = 'desc' }: CustomersFi
|
|||||||
|
|
||||||
const hasFilters = status || email || phone;
|
const hasFilters = status || email || phone;
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
const fetchCount = async (): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const tc = await getAllCustomersCount();
|
||||||
|
setTotalCount(tc);
|
||||||
|
|
||||||
|
const bc = await GetBlockedCount();
|
||||||
|
setBlockedCount(bc);
|
||||||
|
const pc = await GetPendingCount();
|
||||||
|
setPendingCount(pc);
|
||||||
|
const ac = await GetActiveCount();
|
||||||
|
setActiveCount(ac);
|
||||||
|
} catch (error) {
|
||||||
|
//
|
||||||
|
}
|
||||||
|
};
|
||||||
|
void fetchCount();
|
||||||
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
<Tabs onChange={handleStatusChange} sx={{ px: 3 }} value={status ?? ''} variant="scrollable">
|
<Tabs
|
||||||
|
onChange={handleStatusChange}
|
||||||
|
sx={{ px: 3 }}
|
||||||
|
value={status ?? ''}
|
||||||
|
variant="scrollable"
|
||||||
|
//
|
||||||
|
>
|
||||||
{tabs.map((tab) => (
|
{tabs.map((tab) => (
|
||||||
<Tab
|
<Tab
|
||||||
icon={<Chip label={tab.count} size="small" variant="soft" />}
|
icon={
|
||||||
|
<Chip
|
||||||
|
label={tab.count}
|
||||||
|
size="small"
|
||||||
|
variant="soft"
|
||||||
|
/>
|
||||||
|
}
|
||||||
iconPosition="end"
|
iconPosition="end"
|
||||||
key={tab.value}
|
key={tab.value}
|
||||||
label={tab.label}
|
label={tab.label}
|
||||||
@@ -123,8 +173,16 @@ export function CustomersFilters({ filters = {}, sortDir = 'desc' }: CustomersFi
|
|||||||
))}
|
))}
|
||||||
</Tabs>
|
</Tabs>
|
||||||
<Divider />
|
<Divider />
|
||||||
<Stack direction="row" spacing={2} sx={{ alignItems: 'center', flexWrap: 'wrap', px: 3, py: 2 }}>
|
<Stack
|
||||||
<Stack direction="row" spacing={2} sx={{ alignItems: 'center', flex: '1 1 auto', flexWrap: 'wrap' }}>
|
direction="row"
|
||||||
|
spacing={2}
|
||||||
|
sx={{ alignItems: 'center', flexWrap: 'wrap', px: 3, py: 2 }}
|
||||||
|
>
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
spacing={2}
|
||||||
|
sx={{ alignItems: 'center', flex: '1 1 auto', flexWrap: 'wrap' }}
|
||||||
|
>
|
||||||
<FilterButton
|
<FilterButton
|
||||||
displayValue={email}
|
displayValue={email}
|
||||||
label="Email"
|
label="Email"
|
||||||
@@ -137,6 +195,7 @@ export function CustomersFilters({ filters = {}, sortDir = 'desc' }: CustomersFi
|
|||||||
popover={<EmailFilterPopover />}
|
popover={<EmailFilterPopover />}
|
||||||
value={email}
|
value={email}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<FilterButton
|
<FilterButton
|
||||||
displayValue={phone}
|
displayValue={phone}
|
||||||
label="Phone number"
|
label="Phone number"
|
||||||
@@ -149,19 +208,35 @@ export function CustomersFilters({ filters = {}, sortDir = 'desc' }: CustomersFi
|
|||||||
popover={<PhoneFilterPopover />}
|
popover={<PhoneFilterPopover />}
|
||||||
value={phone}
|
value={phone}
|
||||||
/>
|
/>
|
||||||
|
|
||||||
{hasFilters ? <Button onClick={handleClearFilters}>Clear filters</Button> : null}
|
{hasFilters ? <Button onClick={handleClearFilters}>Clear filters</Button> : null}
|
||||||
</Stack>
|
</Stack>
|
||||||
{selection.selectedAny ? (
|
{selection.selectedAny ? (
|
||||||
<Stack direction="row" spacing={2} sx={{ alignItems: 'center' }}>
|
<Stack
|
||||||
<Typography color="text.secondary" variant="body2">
|
direction="row"
|
||||||
|
spacing={2}
|
||||||
|
sx={{ alignItems: 'center' }}
|
||||||
|
>
|
||||||
|
<Typography
|
||||||
|
color="text.secondary"
|
||||||
|
variant="body2"
|
||||||
|
>
|
||||||
{selection.selected.size} selected
|
{selection.selected.size} selected
|
||||||
</Typography>
|
</Typography>
|
||||||
<Button color="error" variant="contained">
|
<Button
|
||||||
|
color="error"
|
||||||
|
variant="contained"
|
||||||
|
>
|
||||||
Delete
|
Delete
|
||||||
</Button>
|
</Button>
|
||||||
</Stack>
|
</Stack>
|
||||||
) : null}
|
) : null}
|
||||||
<Select name="sort" onChange={handleSortChange} sx={{ maxWidth: '100%', width: '120px' }} value={sortDir}>
|
<Select
|
||||||
|
name="sort"
|
||||||
|
onChange={handleSortChange}
|
||||||
|
sx={{ maxWidth: '100%', width: '120px' }}
|
||||||
|
value={sortDir}
|
||||||
|
>
|
||||||
<Option value="desc">Newest</Option>
|
<Option value="desc">Newest</Option>
|
||||||
<Option value="asc">Oldest</Option>
|
<Option value="asc">Oldest</Option>
|
||||||
</Select>
|
</Select>
|
||||||
@@ -169,73 +244,3 @@ export function CustomersFilters({ filters = {}, sortDir = 'desc' }: CustomersFi
|
|||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function EmailFilterPopover(): React.JSX.Element {
|
|
||||||
const { anchorEl, onApply, onClose, open, value: initialValue } = useFilterContext();
|
|
||||||
const [value, setValue] = React.useState<string>('');
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
setValue((initialValue as string | undefined) ?? '');
|
|
||||||
}, [initialValue]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<FilterPopover anchorEl={anchorEl} onClose={onClose} open={open} title="Filter by email">
|
|
||||||
<FormControl>
|
|
||||||
<OutlinedInput
|
|
||||||
onChange={(event) => {
|
|
||||||
setValue(event.target.value);
|
|
||||||
}}
|
|
||||||
onKeyUp={(event) => {
|
|
||||||
if (event.key === 'Enter') {
|
|
||||||
onApply(value);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
value={value}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<Button
|
|
||||||
onClick={() => {
|
|
||||||
onApply(value);
|
|
||||||
}}
|
|
||||||
variant="contained"
|
|
||||||
>
|
|
||||||
Apply
|
|
||||||
</Button>
|
|
||||||
</FilterPopover>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
function PhoneFilterPopover(): React.JSX.Element {
|
|
||||||
const { anchorEl, onApply, onClose, open, value: initialValue } = useFilterContext();
|
|
||||||
const [value, setValue] = React.useState<string>('');
|
|
||||||
|
|
||||||
React.useEffect(() => {
|
|
||||||
setValue((initialValue as string | undefined) ?? '');
|
|
||||||
}, [initialValue]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<FilterPopover anchorEl={anchorEl} onClose={onClose} open={open} title="Filter by phone number">
|
|
||||||
<FormControl>
|
|
||||||
<OutlinedInput
|
|
||||||
onChange={(event) => {
|
|
||||||
setValue(event.target.value);
|
|
||||||
}}
|
|
||||||
onKeyUp={(event) => {
|
|
||||||
if (event.key === 'Enter') {
|
|
||||||
onApply(value);
|
|
||||||
}
|
|
||||||
}}
|
|
||||||
value={value}
|
|
||||||
/>
|
|
||||||
</FormControl>
|
|
||||||
<Button
|
|
||||||
onClick={() => {
|
|
||||||
onApply(value);
|
|
||||||
}}
|
|
||||||
variant="contained"
|
|
||||||
>
|
|
||||||
Apply
|
|
||||||
</Button>
|
|
||||||
</FilterPopover>
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
@@ -10,20 +10,39 @@ function noop(): void {
|
|||||||
interface CustomersPaginationProps {
|
interface CustomersPaginationProps {
|
||||||
count: number;
|
count: number;
|
||||||
page: number;
|
page: number;
|
||||||
|
//
|
||||||
|
setPage: (page: number) => void;
|
||||||
|
setRowsPerPage: (page: number) => void;
|
||||||
|
rowsPerPage: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CustomersPagination({ count, page }: CustomersPaginationProps): React.JSX.Element {
|
export function CustomersPagination({
|
||||||
|
count,
|
||||||
|
page,
|
||||||
|
//
|
||||||
|
setPage,
|
||||||
|
setRowsPerPage,
|
||||||
|
rowsPerPage,
|
||||||
|
}: CustomersPaginationProps): React.JSX.Element {
|
||||||
// You should implement the pagination using a similar logic as the filters.
|
// You should implement the pagination using a similar logic as the filters.
|
||||||
// Note that when page change, you should keep the filter search params.
|
// Note that when page change, you should keep the filter search params.
|
||||||
|
const handleChangePage = (event: unknown, newPage: number) => {
|
||||||
|
setPage(newPage);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handleChangeRowsPerPage = (event: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
setRowsPerPage(parseInt(event.target.value));
|
||||||
|
// console.log(parseInt(event.target.value));
|
||||||
|
};
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<TablePagination
|
<TablePagination
|
||||||
component="div"
|
component="div"
|
||||||
count={count}
|
count={count}
|
||||||
onPageChange={noop}
|
onPageChange={handleChangePage}
|
||||||
onRowsPerPageChange={noop}
|
onRowsPerPageChange={handleChangeRowsPerPage}
|
||||||
page={page}
|
page={page}
|
||||||
rowsPerPage={5}
|
rowsPerPage={rowsPerPage}
|
||||||
rowsPerPageOptions={[5, 10, 25]}
|
rowsPerPageOptions={[5, 10, 25]}
|
||||||
//
|
//
|
||||||
/>
|
/>
|
||||||
|
@@ -5,7 +5,7 @@ import * as React from 'react';
|
|||||||
import { useSelection } from '@/hooks/use-selection';
|
import { useSelection } from '@/hooks/use-selection';
|
||||||
import type { Selection } from '@/hooks/use-selection';
|
import type { Selection } from '@/hooks/use-selection';
|
||||||
|
|
||||||
import type { Customer } from './customers-table';
|
import type { Customer } from './type.d';
|
||||||
|
|
||||||
function noop(): void {
|
function noop(): void {
|
||||||
return undefined;
|
return undefined;
|
||||||
|
@@ -2,8 +2,10 @@
|
|||||||
|
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import RouterLink from 'next/link';
|
import RouterLink from 'next/link';
|
||||||
|
import { LoadingButton } from '@mui/lab';
|
||||||
import Avatar from '@mui/material/Avatar';
|
import Avatar from '@mui/material/Avatar';
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
|
import Button from '@mui/material/Button';
|
||||||
import Chip from '@mui/material/Chip';
|
import Chip from '@mui/material/Chip';
|
||||||
import IconButton from '@mui/material/IconButton';
|
import IconButton from '@mui/material/IconButton';
|
||||||
import LinearProgress from '@mui/material/LinearProgress';
|
import LinearProgress from '@mui/material/LinearProgress';
|
||||||
@@ -12,109 +14,184 @@ import Stack from '@mui/material/Stack';
|
|||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import { CheckCircle as CheckCircleIcon } from '@phosphor-icons/react/dist/ssr/CheckCircle';
|
import { CheckCircle as CheckCircleIcon } from '@phosphor-icons/react/dist/ssr/CheckCircle';
|
||||||
import { Clock as ClockIcon } from '@phosphor-icons/react/dist/ssr/Clock';
|
import { Clock as ClockIcon } from '@phosphor-icons/react/dist/ssr/Clock';
|
||||||
|
import { Images as ImagesIcon } from '@phosphor-icons/react/dist/ssr/Images';
|
||||||
import { Minus as MinusIcon } from '@phosphor-icons/react/dist/ssr/Minus';
|
import { Minus as MinusIcon } from '@phosphor-icons/react/dist/ssr/Minus';
|
||||||
import { PencilSimple as PencilSimpleIcon } from '@phosphor-icons/react/dist/ssr/PencilSimple';
|
import { PencilSimple as PencilSimpleIcon } from '@phosphor-icons/react/dist/ssr/PencilSimple';
|
||||||
|
import { TrashSimple as TrashSimpleIcon } from '@phosphor-icons/react/dist/ssr/TrashSimple';
|
||||||
|
import { useTranslation } from 'react-i18next';
|
||||||
|
import { toast } from 'sonner';
|
||||||
|
|
||||||
import { paths } from '@/paths';
|
import { paths } from '@/paths';
|
||||||
import { dayjs } from '@/lib/dayjs';
|
import { dayjs } from '@/lib/dayjs';
|
||||||
import { DataTable } from '@/components/core/data-table';
|
import { DataTable } from '@/components/core/data-table';
|
||||||
import type { ColumnDef } from '@/components/core/data-table';
|
import type { ColumnDef } from '@/components/core/data-table';
|
||||||
|
|
||||||
|
import ConfirmDeleteModal from './confirm-delete-modal';
|
||||||
import { useCustomersSelection } from './customers-selection-context';
|
import { useCustomersSelection } from './customers-selection-context';
|
||||||
|
import type { Customer } from './type.d';
|
||||||
|
|
||||||
export interface Customer {
|
function columns(handleDeleteClick: (testId: string) => void): ColumnDef<Customer>[] {
|
||||||
id: string;
|
return [
|
||||||
name: string;
|
{
|
||||||
avatar?: string;
|
formatter: (row): React.JSX.Element => (
|
||||||
email: string;
|
<Stack
|
||||||
phone?: string;
|
direction="row"
|
||||||
quota: number;
|
spacing={1}
|
||||||
status: 'pending' | 'active' | 'blocked';
|
sx={{ alignItems: 'center' }}
|
||||||
createdAt: Date;
|
>
|
||||||
}
|
<Avatar src={row.avatar} />{' '}
|
||||||
|
<div>
|
||||||
const columns = [
|
<Link
|
||||||
{
|
color="inherit"
|
||||||
formatter: (row): React.JSX.Element => (
|
component={RouterLink}
|
||||||
<Stack direction="row" spacing={1} sx={{ alignItems: 'center' }}>
|
href={paths.dashboard.customers.details(row.id)}
|
||||||
<Avatar src={row.avatar} />{' '}
|
sx={{ whiteSpace: 'nowrap' }}
|
||||||
<div>
|
variant="subtitle2"
|
||||||
<Link
|
>
|
||||||
color="inherit"
|
{row.name}
|
||||||
component={RouterLink}
|
</Link>
|
||||||
href={paths.dashboard.customers.details('1')}
|
<Typography
|
||||||
sx={{ whiteSpace: 'nowrap' }}
|
color="text.secondary"
|
||||||
variant="subtitle2"
|
variant="body2"
|
||||||
|
>
|
||||||
|
{row.email}
|
||||||
|
</Typography>
|
||||||
|
</div>
|
||||||
|
</Stack>
|
||||||
|
),
|
||||||
|
name: 'Name',
|
||||||
|
width: '250px',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
formatter: (row): React.JSX.Element => (
|
||||||
|
<Stack
|
||||||
|
direction="row"
|
||||||
|
spacing={2}
|
||||||
|
sx={{ alignItems: 'center' }}
|
||||||
|
>
|
||||||
|
<LinearProgress
|
||||||
|
sx={{ flex: '1 1 auto' }}
|
||||||
|
value={row.quota}
|
||||||
|
variant="determinate"
|
||||||
|
/>
|
||||||
|
<Typography
|
||||||
|
color="text.secondary"
|
||||||
|
variant="body2"
|
||||||
>
|
>
|
||||||
{row.name}
|
{new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 2 }).format(row.quota / 100)}
|
||||||
</Link>
|
|
||||||
<Typography color="text.secondary" variant="body2">
|
|
||||||
{row.email}
|
|
||||||
</Typography>
|
</Typography>
|
||||||
</div>
|
</Stack>
|
||||||
</Stack>
|
),
|
||||||
),
|
name: 'Quota',
|
||||||
name: 'Name',
|
width: '150px',
|
||||||
width: '250px',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
formatter: (row): React.JSX.Element => (
|
|
||||||
<Stack direction="row" spacing={2} sx={{ alignItems: 'center' }}>
|
|
||||||
<LinearProgress sx={{ flex: '1 1 auto' }} value={row.quota} variant="determinate" />
|
|
||||||
<Typography color="text.secondary" variant="body2">
|
|
||||||
{new Intl.NumberFormat('en-US', { style: 'percent', maximumFractionDigits: 2 }).format(row.quota / 100)}
|
|
||||||
</Typography>
|
|
||||||
</Stack>
|
|
||||||
),
|
|
||||||
name: 'Quota',
|
|
||||||
width: '250px',
|
|
||||||
},
|
|
||||||
{ field: 'phone', name: 'Phone number', width: '150px' },
|
|
||||||
{
|
|
||||||
formatter(row) {
|
|
||||||
return dayjs(row.createdAt).format('MMM D, YYYY h:mm A');
|
|
||||||
},
|
},
|
||||||
name: 'Created at',
|
{ field: 'phone', name: 'Phone number', width: '150px' },
|
||||||
width: '200px',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
formatter: (row): React.JSX.Element => {
|
|
||||||
const mapping = {
|
|
||||||
active: { label: 'Active', icon: <CheckCircleIcon color="var(--mui-palette-success-main)" weight="fill" /> },
|
|
||||||
blocked: { label: 'Blocked', icon: <MinusIcon color="var(--mui-palette-error-main)" /> },
|
|
||||||
pending: { label: 'Pending', icon: <ClockIcon color="var(--mui-palette-warning-main)" weight="fill" /> },
|
|
||||||
} as const;
|
|
||||||
const { label, icon } = mapping[row.status] ?? { label: 'Unknown', icon: null };
|
|
||||||
|
|
||||||
return <Chip icon={icon} label={label} size="small" variant="outlined" />;
|
{
|
||||||
|
formatter: (row): React.JSX.Element => {
|
||||||
|
// eslint-disable-next-line react-hooks/rules-of-hooks
|
||||||
|
|
||||||
|
const mapping = {
|
||||||
|
active: {
|
||||||
|
label: 'Active',
|
||||||
|
icon: (
|
||||||
|
<CheckCircleIcon
|
||||||
|
color="var(--mui-palette-success-main)"
|
||||||
|
weight="fill"
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
blocked: { label: 'Blocked', icon: <MinusIcon color="var(--mui-palette-error-main)" /> },
|
||||||
|
pending: {
|
||||||
|
label: 'Pending',
|
||||||
|
icon: (
|
||||||
|
<ClockIcon
|
||||||
|
color="var(--mui-palette-warning-main)"
|
||||||
|
weight="fill"
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
},
|
||||||
|
} as const;
|
||||||
|
const { label, icon } = mapping[row.status] ?? { label: 'Unknown', icon: null };
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Chip
|
||||||
|
icon={icon}
|
||||||
|
label={label}
|
||||||
|
size="small"
|
||||||
|
variant="outlined"
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
},
|
||||||
|
name: 'Status',
|
||||||
|
width: '150px',
|
||||||
},
|
},
|
||||||
name: 'Status',
|
{
|
||||||
width: '150px',
|
formatter(row) {
|
||||||
},
|
return dayjs(row.createdAt).format('MMM D, YYYY');
|
||||||
{
|
},
|
||||||
formatter: (): React.JSX.Element => (
|
name: 'Created at',
|
||||||
<IconButton component={RouterLink} href={paths.dashboard.customers.details('1')}>
|
width: '150px',
|
||||||
<PencilSimpleIcon />
|
},
|
||||||
</IconButton>
|
{
|
||||||
),
|
formatter: (row): React.JSX.Element => (
|
||||||
name: 'Actions',
|
<Stack
|
||||||
hideName: true,
|
direction="row"
|
||||||
width: '100px',
|
spacing={1}
|
||||||
align: 'right',
|
>
|
||||||
},
|
<LoadingButton
|
||||||
] satisfies ColumnDef<Customer>[];
|
//
|
||||||
|
color="secondary"
|
||||||
|
component={RouterLink}
|
||||||
|
href={paths.dashboard.cr_questions.details(row.id)}
|
||||||
|
>
|
||||||
|
<PencilSimpleIcon size={24} />
|
||||||
|
</LoadingButton>
|
||||||
|
<LoadingButton
|
||||||
|
color="error"
|
||||||
|
disabled={row.isEmpty}
|
||||||
|
onClick={() => {
|
||||||
|
handleDeleteClick(row.id);
|
||||||
|
}}
|
||||||
|
>
|
||||||
|
<TrashSimpleIcon size={24} />
|
||||||
|
</LoadingButton>
|
||||||
|
</Stack>
|
||||||
|
),
|
||||||
|
name: 'Actions',
|
||||||
|
hideName: true,
|
||||||
|
align: 'right',
|
||||||
|
},
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
export interface CustomersTableProps {
|
export interface CustomersTableProps {
|
||||||
rows: Customer[];
|
rows: Customer[];
|
||||||
|
reloadRows: () => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export function CustomersTable({ rows }: CustomersTableProps): React.JSX.Element {
|
export function CustomersTable({ rows, reloadRows }: CustomersTableProps): React.JSX.Element {
|
||||||
|
const { t } = useTranslation(['customers']);
|
||||||
const { deselectAll, deselectOne, selectAll, selectOne, selected } = useCustomersSelection();
|
const { deselectAll, deselectOne, selectAll, selectOne, selected } = useCustomersSelection();
|
||||||
|
|
||||||
|
const [idToDelete, setIdToDelete] = React.useState('');
|
||||||
|
const [open, setOpen] = React.useState(false);
|
||||||
|
|
||||||
|
function handleDeleteClick(testId: string): void {
|
||||||
|
setOpen(true);
|
||||||
|
setIdToDelete(testId);
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<React.Fragment>
|
<React.Fragment>
|
||||||
|
<ConfirmDeleteModal
|
||||||
|
idToDelete={idToDelete}
|
||||||
|
open={open}
|
||||||
|
reloadRows={reloadRows}
|
||||||
|
setOpen={setOpen}
|
||||||
|
/>
|
||||||
<DataTable<Customer>
|
<DataTable<Customer>
|
||||||
columns={columns}
|
columns={columns(handleDeleteClick)}
|
||||||
onDeselectAll={deselectAll}
|
onDeselectAll={deselectAll}
|
||||||
onDeselectOne={(_, row) => {
|
onDeselectOne={(_, row) => {
|
||||||
deselectOne(row.id);
|
deselectOne(row.id);
|
||||||
@@ -129,8 +206,13 @@ export function CustomersTable({ rows }: CustomersTableProps): React.JSX.Element
|
|||||||
/>
|
/>
|
||||||
{!rows.length ? (
|
{!rows.length ? (
|
||||||
<Box sx={{ p: 3 }}>
|
<Box sx={{ p: 3 }}>
|
||||||
<Typography color="text.secondary" sx={{ textAlign: 'center' }} variant="body2">
|
<Typography
|
||||||
No customers found
|
color="text.secondary"
|
||||||
|
sx={{ textAlign: 'center' }}
|
||||||
|
variant="body2"
|
||||||
|
>
|
||||||
|
{/* TODO: update this */}
|
||||||
|
{t('no-record-found')}
|
||||||
</Typography>
|
</Typography>
|
||||||
</Box>
|
</Box>
|
||||||
) : null}
|
) : null}
|
||||||
|
@@ -0,0 +1,50 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import * as React from 'react';
|
||||||
|
|
||||||
|
import Button from '@mui/material/Button';
|
||||||
|
import FormControl from '@mui/material/FormControl';
|
||||||
|
import OutlinedInput from '@mui/material/OutlinedInput';
|
||||||
|
|
||||||
|
import { FilterPopover, useFilterContext } from '@/components/core/filter-button';
|
||||||
|
|
||||||
|
// EmailFilterPopover -> email-filter-popover.tsx
|
||||||
|
export default function EmailFilterPopover(): React.JSX.Element {
|
||||||
|
const { anchorEl, onApply, onClose, open, value: initialValue } = useFilterContext();
|
||||||
|
const [value, setValue] = React.useState<string>('');
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
setValue((initialValue as string | undefined) ?? '');
|
||||||
|
}, [initialValue]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FilterPopover
|
||||||
|
anchorEl={anchorEl}
|
||||||
|
onClose={onClose}
|
||||||
|
open={open}
|
||||||
|
title="Filter by email"
|
||||||
|
>
|
||||||
|
<FormControl>
|
||||||
|
<OutlinedInput
|
||||||
|
onChange={(event) => {
|
||||||
|
setValue(event.target.value);
|
||||||
|
}}
|
||||||
|
onKeyUp={(event) => {
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
onApply(value);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
value={value}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
onApply(value);
|
||||||
|
}}
|
||||||
|
variant="contained"
|
||||||
|
>
|
||||||
|
Apply
|
||||||
|
</Button>
|
||||||
|
</FilterPopover>
|
||||||
|
);
|
||||||
|
}
|
@@ -0,0 +1,50 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import * as React from 'react';
|
||||||
|
|
||||||
|
import Button from '@mui/material/Button';
|
||||||
|
import FormControl from '@mui/material/FormControl';
|
||||||
|
import OutlinedInput from '@mui/material/OutlinedInput';
|
||||||
|
|
||||||
|
import { FilterPopover, useFilterContext } from '@/components/core/filter-button';
|
||||||
|
|
||||||
|
// phone-filter-popover.tsx
|
||||||
|
export default function PhoneFilterPopover(): React.JSX.Element {
|
||||||
|
const { anchorEl, onApply, onClose, open, value: initialValue } = useFilterContext();
|
||||||
|
const [value, setValue] = React.useState<string>('');
|
||||||
|
|
||||||
|
React.useEffect(() => {
|
||||||
|
setValue((initialValue as string | undefined) ?? '');
|
||||||
|
}, [initialValue]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<FilterPopover
|
||||||
|
anchorEl={anchorEl}
|
||||||
|
onClose={onClose}
|
||||||
|
open={open}
|
||||||
|
title="Filter by phone number"
|
||||||
|
>
|
||||||
|
<FormControl>
|
||||||
|
<OutlinedInput
|
||||||
|
onChange={(event) => {
|
||||||
|
setValue(event.target.value);
|
||||||
|
}}
|
||||||
|
onKeyUp={(event) => {
|
||||||
|
if (event.key === 'Enter') {
|
||||||
|
onApply(value);
|
||||||
|
}
|
||||||
|
}}
|
||||||
|
value={value}
|
||||||
|
/>
|
||||||
|
</FormControl>
|
||||||
|
<Button
|
||||||
|
onClick={() => {
|
||||||
|
onApply(value);
|
||||||
|
}}
|
||||||
|
variant="contained"
|
||||||
|
>
|
||||||
|
Apply
|
||||||
|
</Button>
|
||||||
|
</FilterPopover>
|
||||||
|
);
|
||||||
|
}
|
46
002_source/cms/src/components/dashboard/customer/type.d.tsx
Normal file
46
002_source/cms/src/components/dashboard/customer/type.d.tsx
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
'use client';
|
||||||
|
|
||||||
|
import { SortDir } from './phone-filter-popover';
|
||||||
|
import { Customer } from './type.d';
|
||||||
|
|
||||||
|
export interface Customer {
|
||||||
|
id: string;
|
||||||
|
name: string;
|
||||||
|
avatar?: string;
|
||||||
|
email: string;
|
||||||
|
phone?: string;
|
||||||
|
quota: number;
|
||||||
|
status: 'pending' | 'active' | 'blocked';
|
||||||
|
createdAt: Date;
|
||||||
|
updatedAt?: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface CreateFormProps {
|
||||||
|
name: string;
|
||||||
|
avatar?: string;
|
||||||
|
email: string;
|
||||||
|
phone?: string;
|
||||||
|
quota: number;
|
||||||
|
status: 'pending' | 'active' | 'blocked';
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface EditFormProps {
|
||||||
|
name: string;
|
||||||
|
avatar?: string;
|
||||||
|
email: string;
|
||||||
|
phone?: string;
|
||||||
|
quota: number;
|
||||||
|
status: 'pending' | 'active' | 'blocked';
|
||||||
|
}
|
||||||
|
export interface CustomersFiltersProps {
|
||||||
|
filters?: Filters;
|
||||||
|
sortDir?: SortDir;
|
||||||
|
fullData: Customer[];
|
||||||
|
}
|
||||||
|
export interface Filters {
|
||||||
|
email?: string;
|
||||||
|
phone?: string;
|
||||||
|
status?: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type SortDir = 'asc' | 'desc';
|
Reference in New Issue
Block a user