initial commit
This commit is contained in:
@@ -0,0 +1,263 @@
|
||||
// React Imports
|
||||
import { useState } from 'react'
|
||||
|
||||
// MUI Imports
|
||||
import Button from '@mui/material/Button'
|
||||
import Drawer from '@mui/material/Drawer'
|
||||
import Divider from '@mui/material/Divider'
|
||||
import IconButton from '@mui/material/IconButton'
|
||||
import MenuItem from '@mui/material/MenuItem'
|
||||
import Switch from '@mui/material/Switch'
|
||||
import Typography from '@mui/material/Typography'
|
||||
|
||||
// Third-party Imports
|
||||
import PerfectScrollbar from 'react-perfect-scrollbar'
|
||||
import { useForm, Controller } from 'react-hook-form'
|
||||
|
||||
// Type Imports
|
||||
import type { Customer } from '@/types/apps/ecommerceTypes'
|
||||
|
||||
// Component Imports
|
||||
import CustomTextField from '@core/components/mui/TextField'
|
||||
|
||||
type Props = {
|
||||
open: boolean
|
||||
handleClose: () => void
|
||||
setData: (data: Customer[]) => void
|
||||
customerData?: Customer[]
|
||||
}
|
||||
|
||||
type FormValidateType = {
|
||||
fullName: string
|
||||
email: string
|
||||
country: string
|
||||
}
|
||||
|
||||
type FormNonValidateType = {
|
||||
contact: string
|
||||
address1: string
|
||||
address2: string
|
||||
town: string
|
||||
state: string
|
||||
postcode: string
|
||||
}
|
||||
|
||||
type countryType = {
|
||||
country: string
|
||||
}
|
||||
|
||||
export const country: { [key: string]: countryType } = {
|
||||
india: { country: 'India' },
|
||||
australia: { country: 'Australia' },
|
||||
france: { country: 'France' },
|
||||
brazil: { country: 'Brazil' },
|
||||
us: { country: 'United States' },
|
||||
china: { country: 'China' }
|
||||
}
|
||||
|
||||
// Vars
|
||||
const initialData = {
|
||||
contact: '',
|
||||
address1: '',
|
||||
address2: '',
|
||||
town: '',
|
||||
state: '',
|
||||
postcode: ''
|
||||
}
|
||||
|
||||
const AddCustomerDrawer = (props: Props) => {
|
||||
// Props
|
||||
const { open, handleClose, setData, customerData } = props
|
||||
|
||||
// States
|
||||
const [formData, setFormData] = useState<FormNonValidateType>(initialData)
|
||||
|
||||
// Hooks
|
||||
const {
|
||||
control,
|
||||
reset: resetForm,
|
||||
handleSubmit,
|
||||
formState: { errors }
|
||||
} = useForm<FormValidateType>({
|
||||
defaultValues: {
|
||||
fullName: '',
|
||||
email: '',
|
||||
country: ''
|
||||
}
|
||||
})
|
||||
|
||||
const onSubmit = (data: FormValidateType) => {
|
||||
const newData: Customer = {
|
||||
id: (customerData?.length && customerData?.length + 1) || 1,
|
||||
customer: data.fullName,
|
||||
customerId: customerData?.[Math.floor(Math.random() * 100) + 1].customerId ?? '1',
|
||||
email: data.email,
|
||||
country: `${country[data.country].country}`,
|
||||
countryCode: 'st',
|
||||
countryFlag: `/images/cards/${data.country}.png`,
|
||||
order: Math.floor(Math.random() * 1000) + 1,
|
||||
totalSpent: Math.floor(Math.random() * (1000000 - 100) + 100) / 100,
|
||||
avatar: `/images/avatars/${Math.floor(Math.random() * 8) + 1}.png`
|
||||
}
|
||||
|
||||
setData([...(customerData ?? []), newData])
|
||||
resetForm({ fullName: '', email: '', country: '' })
|
||||
setFormData(initialData)
|
||||
handleClose()
|
||||
}
|
||||
|
||||
const handleReset = () => {
|
||||
handleClose()
|
||||
resetForm({ fullName: '', email: '', country: '' })
|
||||
setFormData(initialData)
|
||||
}
|
||||
|
||||
return (
|
||||
<Drawer
|
||||
open={open}
|
||||
anchor='right'
|
||||
variant='temporary'
|
||||
onClose={handleReset}
|
||||
ModalProps={{ keepMounted: true }}
|
||||
sx={{ '& .MuiDrawer-paper': { width: { xs: 300, sm: 400 } } }}
|
||||
>
|
||||
<div className='flex items-center justify-between pli-6 plb-5'>
|
||||
<Typography variant='h5'>Add a Customer</Typography>
|
||||
<IconButton size='small' onClick={handleReset}>
|
||||
<i className='tabler-x text-2xl' />
|
||||
</IconButton>
|
||||
</div>
|
||||
<Divider />
|
||||
<PerfectScrollbar options={{ wheelPropagation: false, suppressScrollX: true }}>
|
||||
<div className='p-6'>
|
||||
<form onSubmit={handleSubmit(data => onSubmit(data))} className='flex flex-col gap-5'>
|
||||
<Typography color='text.primary' className='font-medium'>
|
||||
Basic Information
|
||||
</Typography>
|
||||
<Controller
|
||||
name='fullName'
|
||||
control={control}
|
||||
rules={{ required: true }}
|
||||
render={({ field }) => (
|
||||
<CustomTextField
|
||||
{...field}
|
||||
fullWidth
|
||||
label='Name'
|
||||
placeholder='John Doe'
|
||||
{...(errors.fullName && { error: true, helperText: 'This field is required.' })}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name='email'
|
||||
control={control}
|
||||
rules={{ required: true }}
|
||||
render={({ field }) => (
|
||||
<CustomTextField
|
||||
{...field}
|
||||
fullWidth
|
||||
type='email'
|
||||
label='Email'
|
||||
placeholder='johndoe@gmail.com'
|
||||
{...(errors.email && { error: true, helperText: 'This field is required.' })}
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
<Controller
|
||||
name='country'
|
||||
control={control}
|
||||
rules={{ required: true }}
|
||||
render={({ field }) => (
|
||||
<CustomTextField
|
||||
select
|
||||
fullWidth
|
||||
id='country'
|
||||
label='Country'
|
||||
{...field}
|
||||
{...(errors.country && { error: true, helperText: 'This field is required.' })}
|
||||
>
|
||||
<MenuItem value='india'>India</MenuItem>
|
||||
<MenuItem value='australia'>Australia</MenuItem>
|
||||
<MenuItem value='france'>France</MenuItem>
|
||||
<MenuItem value='brazil'>Brazil</MenuItem>
|
||||
<MenuItem value='us'>USA</MenuItem>
|
||||
<MenuItem value='china'>China</MenuItem>
|
||||
</CustomTextField>
|
||||
)}
|
||||
/>
|
||||
<Typography color='text.primary' className='font-medium'>
|
||||
Shipping Information
|
||||
</Typography>
|
||||
<CustomTextField
|
||||
fullWidth
|
||||
label='Address Line 1'
|
||||
name='address1'
|
||||
placeholder='45 Roker Terrace'
|
||||
value={formData.address1}
|
||||
onChange={e => setFormData({ ...formData, address1: e.target.value })}
|
||||
/>
|
||||
<CustomTextField
|
||||
fullWidth
|
||||
label='Address Line 2'
|
||||
name='address2'
|
||||
placeholder='Street 69'
|
||||
value={formData.address2}
|
||||
onChange={e => setFormData({ ...formData, address2: e.target.value })}
|
||||
/>
|
||||
<CustomTextField
|
||||
fullWidth
|
||||
label='Town'
|
||||
name='town'
|
||||
placeholder='New York'
|
||||
value={formData.town}
|
||||
onChange={e => setFormData({ ...formData, town: e.target.value })}
|
||||
/>
|
||||
<CustomTextField
|
||||
fullWidth
|
||||
label='State/Province'
|
||||
name='state'
|
||||
placeholder='Southern tip'
|
||||
value={formData.state}
|
||||
onChange={e => setFormData({ ...formData, state: e.target.value })}
|
||||
/>
|
||||
<CustomTextField
|
||||
fullWidth
|
||||
label='Post Code'
|
||||
name='postcode'
|
||||
placeholder='734990'
|
||||
value={formData.postcode}
|
||||
onChange={e => setFormData({ ...formData, postcode: e.target.value })}
|
||||
/>
|
||||
<CustomTextField
|
||||
label='Mobile'
|
||||
type='number'
|
||||
fullWidth
|
||||
placeholder='+(123) 456-7890'
|
||||
value={formData.contact}
|
||||
onChange={e => setFormData({ ...formData, contact: e.target.value })}
|
||||
/>
|
||||
<div className='flex justify-between'>
|
||||
<div className='flex flex-col items-start gap-1'>
|
||||
<Typography color='text.primary' className='font-medium'>
|
||||
Use as a billing address?
|
||||
</Typography>
|
||||
<Typography variant='body2'>Please check budget for more info.</Typography>
|
||||
</div>
|
||||
<Switch defaultChecked />
|
||||
</div>
|
||||
<div className='flex items-center gap-4'>
|
||||
<Button variant='contained' type='submit'>
|
||||
Add
|
||||
</Button>
|
||||
<Button variant='tonal' color='error' type='reset' onClick={handleReset}>
|
||||
Discard
|
||||
</Button>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</PerfectScrollbar>
|
||||
</Drawer>
|
||||
)
|
||||
}
|
||||
|
||||
export default AddCustomerDrawer
|
||||
@@ -0,0 +1,379 @@
|
||||
'use client'
|
||||
|
||||
// React Imports
|
||||
import { useState, useEffect, useMemo } from 'react'
|
||||
|
||||
// Next Imports
|
||||
import Link from 'next/link'
|
||||
import { useParams } from 'next/navigation'
|
||||
|
||||
// MUI Imports
|
||||
import Card from '@mui/material/Card'
|
||||
import CardContent from '@mui/material/CardContent'
|
||||
import Button from '@mui/material/Button'
|
||||
import Typography from '@mui/material/Typography'
|
||||
import Checkbox from '@mui/material/Checkbox'
|
||||
import TablePagination from '@mui/material/TablePagination'
|
||||
import type { TextFieldProps } from '@mui/material/TextField'
|
||||
import MenuItem from '@mui/material/MenuItem'
|
||||
|
||||
// Third-party Imports
|
||||
import classnames from 'classnames'
|
||||
import { rankItem } from '@tanstack/match-sorter-utils'
|
||||
import {
|
||||
createColumnHelper,
|
||||
flexRender,
|
||||
getCoreRowModel,
|
||||
useReactTable,
|
||||
getFilteredRowModel,
|
||||
getFacetedRowModel,
|
||||
getFacetedUniqueValues,
|
||||
getFacetedMinMaxValues,
|
||||
getPaginationRowModel,
|
||||
getSortedRowModel
|
||||
} from '@tanstack/react-table'
|
||||
import type { ColumnDef, FilterFn } from '@tanstack/react-table'
|
||||
import type { RankingInfo } from '@tanstack/match-sorter-utils'
|
||||
|
||||
// Type Imports
|
||||
import type { ThemeColor } from '@core/types'
|
||||
import type { Customer } from '@/types/apps/ecommerceTypes'
|
||||
import type { Locale } from '@configs/i18n'
|
||||
|
||||
// Component Imports
|
||||
import AddCustomerDrawer from './AddCustomerDrawer'
|
||||
import CustomAvatar from '@core/components/mui/Avatar'
|
||||
import CustomTextField from '@core/components/mui/TextField'
|
||||
import TablePaginationComponent from '@components/TablePaginationComponent'
|
||||
|
||||
// Util Imports
|
||||
import { getInitials } from '@/utils/getInitials'
|
||||
import { getLocalizedUrl } from '@/utils/i18n'
|
||||
|
||||
// Style Imports
|
||||
import tableStyles from '@core/styles/table.module.css'
|
||||
|
||||
declare module '@tanstack/table-core' {
|
||||
interface FilterFns {
|
||||
fuzzy: FilterFn<unknown>
|
||||
}
|
||||
interface FilterMeta {
|
||||
itemRank: RankingInfo
|
||||
}
|
||||
}
|
||||
|
||||
type PayementStatusType = {
|
||||
text: string
|
||||
color: ThemeColor
|
||||
}
|
||||
|
||||
type StatusChipColorType = {
|
||||
color: ThemeColor
|
||||
}
|
||||
|
||||
export const paymentStatus: { [key: number]: PayementStatusType } = {
|
||||
1: { text: 'Paid', color: 'success' },
|
||||
2: { text: 'Pending', color: 'warning' },
|
||||
3: { text: 'Cancelled', color: 'secondary' },
|
||||
4: { text: 'Failed', color: 'error' }
|
||||
}
|
||||
|
||||
export const statusChipColor: { [key: string]: StatusChipColorType } = {
|
||||
Delivered: { color: 'success' },
|
||||
'Out for Delivery': { color: 'primary' },
|
||||
'Ready to Pickup': { color: 'info' },
|
||||
Dispatched: { color: 'warning' }
|
||||
}
|
||||
|
||||
type ECommerceOrderTypeWithAction = Customer & {
|
||||
action?: string
|
||||
}
|
||||
|
||||
const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
|
||||
// Rank the item
|
||||
const itemRank = rankItem(row.getValue(columnId), value)
|
||||
|
||||
// Store the itemRank info
|
||||
addMeta({
|
||||
itemRank
|
||||
})
|
||||
|
||||
// Return if the item should be filtered in/out
|
||||
return itemRank.passed
|
||||
}
|
||||
|
||||
const DebouncedInput = ({
|
||||
value: initialValue,
|
||||
onChange,
|
||||
debounce = 500,
|
||||
...props
|
||||
}: {
|
||||
value: string | number
|
||||
onChange: (value: string | number) => void
|
||||
debounce?: number
|
||||
} & Omit<TextFieldProps, 'onChange'>) => {
|
||||
// States
|
||||
const [value, setValue] = useState(initialValue)
|
||||
|
||||
useEffect(() => {
|
||||
setValue(initialValue)
|
||||
}, [initialValue])
|
||||
|
||||
useEffect(() => {
|
||||
const timeout = setTimeout(() => {
|
||||
onChange(value)
|
||||
}, debounce)
|
||||
|
||||
return () => clearTimeout(timeout)
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [value])
|
||||
|
||||
return <CustomTextField {...props} value={value} onChange={e => setValue(e.target.value)} />
|
||||
}
|
||||
|
||||
// Column Definitions
|
||||
const columnHelper = createColumnHelper<ECommerceOrderTypeWithAction>()
|
||||
|
||||
const CustomerListTable = ({ customerData }: { customerData?: Customer[] }) => {
|
||||
// States
|
||||
const [customerUserOpen, setCustomerUserOpen] = useState(false)
|
||||
const [rowSelection, setRowSelection] = useState({})
|
||||
const [data, setData] = useState(...[customerData])
|
||||
const [globalFilter, setGlobalFilter] = useState('')
|
||||
|
||||
// Hooks
|
||||
const { lang: locale } = useParams()
|
||||
|
||||
const columns = useMemo<ColumnDef<ECommerceOrderTypeWithAction, any>[]>(
|
||||
() => [
|
||||
{
|
||||
id: 'select',
|
||||
header: ({ table }) => (
|
||||
<Checkbox
|
||||
{...{
|
||||
checked: table.getIsAllRowsSelected(),
|
||||
indeterminate: table.getIsSomeRowsSelected(),
|
||||
onChange: table.getToggleAllRowsSelectedHandler()
|
||||
}}
|
||||
/>
|
||||
),
|
||||
cell: ({ row }) => (
|
||||
<Checkbox
|
||||
{...{
|
||||
checked: row.getIsSelected(),
|
||||
disabled: !row.getCanSelect(),
|
||||
indeterminate: row.getIsSomeSelected(),
|
||||
onChange: row.getToggleSelectedHandler()
|
||||
}}
|
||||
/>
|
||||
)
|
||||
},
|
||||
columnHelper.accessor('customer', {
|
||||
header: 'Customers',
|
||||
cell: ({ row }) => (
|
||||
<div className='flex items-center gap-3'>
|
||||
{getAvatar({ avatar: row.original.avatar, customer: row.original.customer })}
|
||||
<div className='flex flex-col items-start'>
|
||||
<Typography
|
||||
component={Link}
|
||||
color='text.primary'
|
||||
href={getLocalizedUrl(`/apps/ecommerce/customers/details/${row.original.customerId}`, locale as Locale)}
|
||||
className='font-medium hover:text-primary'
|
||||
>
|
||||
{row.original.customer}
|
||||
</Typography>
|
||||
<Typography variant='body2'>{row.original.email}</Typography>
|
||||
</div>
|
||||
</div>
|
||||
)
|
||||
}),
|
||||
columnHelper.accessor('customerId', {
|
||||
header: 'Customer Id',
|
||||
cell: ({ row }) => <Typography color='text.primary'>#{row.original.customerId}</Typography>
|
||||
}),
|
||||
columnHelper.accessor('country', {
|
||||
header: 'Country',
|
||||
cell: ({ row }) => (
|
||||
<div className='flex items-center gap-2'>
|
||||
<img src={row.original.countryFlag} height={22} />
|
||||
<Typography>{row.original.country}</Typography>
|
||||
</div>
|
||||
)
|
||||
}),
|
||||
columnHelper.accessor('order', {
|
||||
header: 'Orders',
|
||||
cell: ({ row }) => <Typography>{row.original.order}</Typography>
|
||||
}),
|
||||
columnHelper.accessor('totalSpent', {
|
||||
header: 'Total Spent',
|
||||
cell: ({ row }) => (
|
||||
<Typography className='font-medium' color='text.primary'>
|
||||
${row.original.totalSpent.toLocaleString()}
|
||||
</Typography>
|
||||
)
|
||||
})
|
||||
],
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[]
|
||||
)
|
||||
|
||||
const table = useReactTable({
|
||||
data: data as Customer[],
|
||||
columns,
|
||||
filterFns: {
|
||||
fuzzy: fuzzyFilter
|
||||
},
|
||||
state: {
|
||||
rowSelection,
|
||||
globalFilter
|
||||
},
|
||||
initialState: {
|
||||
pagination: {
|
||||
pageSize: 10
|
||||
}
|
||||
},
|
||||
enableRowSelection: true, //enable row selection for all rows
|
||||
// enableRowSelection: row => row.original.age > 18, // or enable row selection conditionally per row
|
||||
globalFilterFn: fuzzyFilter,
|
||||
onRowSelectionChange: setRowSelection,
|
||||
getCoreRowModel: getCoreRowModel(),
|
||||
onGlobalFilterChange: setGlobalFilter,
|
||||
getFilteredRowModel: getFilteredRowModel(),
|
||||
getSortedRowModel: getSortedRowModel(),
|
||||
getPaginationRowModel: getPaginationRowModel(),
|
||||
getFacetedRowModel: getFacetedRowModel(),
|
||||
getFacetedUniqueValues: getFacetedUniqueValues(),
|
||||
getFacetedMinMaxValues: getFacetedMinMaxValues()
|
||||
})
|
||||
|
||||
const getAvatar = (params: Pick<Customer, 'avatar' | 'customer'>) => {
|
||||
const { avatar, customer } = params
|
||||
|
||||
if (avatar) {
|
||||
return <CustomAvatar src={avatar} skin='light' size={34} />
|
||||
} else {
|
||||
return (
|
||||
<CustomAvatar skin='light' size={34}>
|
||||
{getInitials(customer as string)}
|
||||
</CustomAvatar>
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
return (
|
||||
<>
|
||||
<Card>
|
||||
<CardContent className='flex justify-between flex-wrap max-sm:flex-col sm:items-center gap-4'>
|
||||
<DebouncedInput
|
||||
value={globalFilter ?? ''}
|
||||
onChange={value => setGlobalFilter(String(value))}
|
||||
placeholder='Search'
|
||||
className='max-sm:is-full'
|
||||
/>
|
||||
<div className='flex max-sm:flex-col items-start sm:items-center gap-4 max-sm:is-full'>
|
||||
<CustomTextField
|
||||
select
|
||||
value={table.getState().pagination.pageSize}
|
||||
onChange={e => table.setPageSize(Number(e.target.value))}
|
||||
className='is-full sm:is-[70px]'
|
||||
>
|
||||
<MenuItem value='10'>10</MenuItem>
|
||||
<MenuItem value='25'>25</MenuItem>
|
||||
<MenuItem value='50'>50</MenuItem>
|
||||
<MenuItem value='100'>100</MenuItem>
|
||||
</CustomTextField>
|
||||
<Button
|
||||
variant='tonal'
|
||||
className='max-sm:is-full'
|
||||
color='secondary'
|
||||
startIcon={<i className='tabler-upload' />}
|
||||
>
|
||||
Export
|
||||
</Button>
|
||||
<Button
|
||||
variant='contained'
|
||||
color='primary'
|
||||
className='max-sm:is-full'
|
||||
startIcon={<i className='tabler-plus' />}
|
||||
onClick={() => setCustomerUserOpen(!customerUserOpen)}
|
||||
>
|
||||
Add Customer
|
||||
</Button>
|
||||
</div>
|
||||
</CardContent>
|
||||
<div className='overflow-x-auto'>
|
||||
<table className={tableStyles.table}>
|
||||
<thead>
|
||||
{table.getHeaderGroups().map(headerGroup => (
|
||||
<tr key={headerGroup.id}>
|
||||
{headerGroup.headers.map(header => (
|
||||
<th key={header.id}>
|
||||
{header.isPlaceholder ? null : (
|
||||
<>
|
||||
<div
|
||||
className={classnames({
|
||||
'flex items-center': header.column.getIsSorted(),
|
||||
'cursor-pointer select-none': header.column.getCanSort()
|
||||
})}
|
||||
onClick={header.column.getToggleSortingHandler()}
|
||||
>
|
||||
{flexRender(header.column.columnDef.header, header.getContext())}
|
||||
{{
|
||||
asc: <i className='tabler-chevron-up text-xl' />,
|
||||
desc: <i className='tabler-chevron-down text-xl' />
|
||||
}[header.column.getIsSorted() as 'asc' | 'desc'] ?? null}
|
||||
</div>
|
||||
</>
|
||||
)}
|
||||
</th>
|
||||
))}
|
||||
</tr>
|
||||
))}
|
||||
</thead>
|
||||
{table.getFilteredRowModel().rows.length === 0 ? (
|
||||
<tbody>
|
||||
<tr>
|
||||
<td colSpan={table.getVisibleFlatColumns().length} className='text-center'>
|
||||
No data available
|
||||
</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
) : (
|
||||
<tbody>
|
||||
{table
|
||||
.getRowModel()
|
||||
.rows.slice(0, table.getState().pagination.pageSize)
|
||||
.map(row => {
|
||||
return (
|
||||
<tr key={row.id} className={classnames({ selected: row.getIsSelected() })}>
|
||||
{row.getVisibleCells().map(cell => (
|
||||
<td key={cell.id}>{flexRender(cell.column.columnDef.cell, cell.getContext())}</td>
|
||||
))}
|
||||
</tr>
|
||||
)
|
||||
})}
|
||||
</tbody>
|
||||
)}
|
||||
</table>
|
||||
</div>
|
||||
<TablePagination
|
||||
component={() => <TablePaginationComponent table={table} />}
|
||||
count={table.getFilteredRowModel().rows.length}
|
||||
rowsPerPage={table.getState().pagination.pageSize}
|
||||
page={table.getState().pagination.pageIndex}
|
||||
onPageChange={(_, page) => {
|
||||
table.setPageIndex(page)
|
||||
}}
|
||||
/>
|
||||
</Card>
|
||||
<AddCustomerDrawer
|
||||
open={customerUserOpen}
|
||||
handleClose={() => setCustomerUserOpen(!customerUserOpen)}
|
||||
setData={setData}
|
||||
customerData={data}
|
||||
/>
|
||||
</>
|
||||
)
|
||||
}
|
||||
|
||||
export default CustomerListTable
|
||||
Reference in New Issue
Block a user