fix: user management & profit chart

This commit is contained in:
ferdiansyah783 2025-08-10 15:47:04 +07:00
parent de93de2e6d
commit 48570c018f
23 changed files with 989 additions and 599 deletions

View File

@ -0,0 +1,31 @@
// Next Imports
// Type Imports
// Component Imports
import OrderDetails from '@views/apps/ecommerce/orders/details'
/**
* ! If you need data using an API call, uncomment the below API code, update the `process.env.API_URL` variable in the
* ! `.env` file found at root of your project and also update the API endpoints like `/apps/ecommerce` in below example.
* ! Also, remove the above server action import and the action itself from the `src/app/server/actions.ts` file to clean up unused code
* ! because we've used the server action for getting our static data.
*/
/* const getEcommerceData = async () => {
// Vars
const res = await fetch(`${process.env.API_URL}/apps/ecommerce`)
if (!res.ok) {
throw new Error('Failed to fetch ecommerce data')
}
return res.json()
} */
const OrderDetailsPage = async () => {
return <OrderDetails />
}
export default OrderDetailsPage

View File

@ -21,7 +21,7 @@ import UserList from '@views/apps/user/list'
const UserListApp = async () => { const UserListApp = async () => {
return <UserList userData={[]} /> return <UserList />
} }
export default UserListApp export default UserListApp

View File

@ -5,15 +5,13 @@ import Grid from '@mui/material/Grid2'
// Component Imports // Component Imports
import DistributedBarChartOrder from '@views/dashboards/crm/DistributedBarChartOrder' import DistributedBarChartOrder from '@views/dashboards/crm/DistributedBarChartOrder'
import EarningReportsWithTabs from '@views/dashboards/crm/EarningReportsWithTabs'
// Server Action Imports // Server Action Imports
import Loading from '../../../../../../components/layout/shared/Loading' import Loading from '../../../../../../components/layout/shared/Loading'
import { useProfitLossAnalytics } from '../../../../../../services/queries/analytics' import { useProfitLossAnalytics } from '../../../../../../services/queries/analytics'
import { import { DailyData, ProductDataReport, ProfitLossReport } from '../../../../../../types/services/analytic'
ProductDataReport, import EarningReportsWithTabs from '../../../../../../views/dashboards/crm/EarningReportsWithTabs'
ProfitLossReport import MultipleSeries from '../../../../../../views/dashboards/profit-loss/EarningReportWithTabs'
} from '../../../../../../types/services/analytic'
function formatMetricName(metric: string): string { function formatMetricName(metric: string): string {
const nameMap: { [key: string]: string } = { const nameMap: { [key: string]: string } = {
@ -41,7 +39,7 @@ const DashboardProfitLoss = () => {
}) })
} }
const metrics = ['revenue', 'cost', 'gross_profit', 'net_profit'] const metrics = ['cost', 'revenue', 'gross_profit', 'net_profit']
const transformSalesData = (data: ProfitLossReport) => { const transformSalesData = (data: ProfitLossReport) => {
return [ return [
@ -50,7 +48,7 @@ const DashboardProfitLoss = () => {
avatarIcon: 'tabler-package', avatarIcon: 'tabler-package',
date: data.product_data.map((d: ProductDataReport) => d.product_name), date: data.product_data.map((d: ProductDataReport) => d.product_name),
series: [{ data: data.product_data.map((d: ProductDataReport) => d.revenue) }] series: [{ data: data.product_data.map((d: ProductDataReport) => d.revenue) }]
}, }
// { // {
// type: 'profits', // type: 'profits',
// avatarIcon: 'tabler-currency-dollar', // avatarIcon: 'tabler-currency-dollar',
@ -63,6 +61,20 @@ const DashboardProfitLoss = () => {
] ]
} }
const transformMultipleData = (data: ProfitLossReport) => {
return [
{
type: 'profits',
avatarIcon: 'tabler-currency-dollar',
date: data.data.map((d: DailyData) => formatDate(d.date)),
series: metrics.map(metric => ({
name: formatMetricName(metric as string),
data: data.data.map((item: any) => item[metric] as number)
}))
}
]
}
if (isLoading) return <Loading /> if (isLoading) return <Loading />
return ( return (
@ -110,6 +122,9 @@ const DashboardProfitLoss = () => {
<Grid size={{ xs: 12, lg: 12 }}> <Grid size={{ xs: 12, lg: 12 }}>
<EarningReportsWithTabs data={transformSalesData(data!)} /> <EarningReportsWithTabs data={transformSalesData(data!)} />
</Grid> </Grid>
<Grid size={{ xs: 12, lg: 12 }}>
<MultipleSeries data={transformMultipleData(data!)} />
</Grid>
</Grid> </Grid>
) )
} }

View File

@ -109,6 +109,7 @@ const VerticalMenu = ({ dictionary, scrollMenu }: Props) => {
</SubMenu> </SubMenu>
<SubMenu label={dictionary['navigation'].orders}> <SubMenu label={dictionary['navigation'].orders}>
<MenuItem href={`/${locale}/apps/ecommerce/orders/list`}>{dictionary['navigation'].list}</MenuItem> <MenuItem href={`/${locale}/apps/ecommerce/orders/list`}>{dictionary['navigation'].list}</MenuItem>
<MenuItem className='hidden' href={`/${locale}/apps/ecommerce/orders/${params.id}/details`}>{dictionary['navigation'].details}</MenuItem>
</SubMenu> </SubMenu>
<SubMenu label={dictionary['navigation'].customers}> <SubMenu label={dictionary['navigation'].customers}>
<MenuItem href={`/${locale}/apps/ecommerce/customers/list`}>{dictionary['navigation'].list}</MenuItem> <MenuItem href={`/${locale}/apps/ecommerce/customers/list`}>{dictionary['navigation'].list}</MenuItem>

View File

@ -5,13 +5,15 @@ import productReducer from '@/redux-store/slices/product'
import customerReducer from '@/redux-store/slices/customer' import customerReducer from '@/redux-store/slices/customer'
import paymentMethodReducer from '@/redux-store/slices/paymentMethod' import paymentMethodReducer from '@/redux-store/slices/paymentMethod'
import ingredientReducer from '@/redux-store/slices/ingredient' import ingredientReducer from '@/redux-store/slices/ingredient'
import orderReducer from '@/redux-store/slices/order'
export const store = configureStore({ export const store = configureStore({
reducer: { reducer: {
productReducer, productReducer,
customerReducer, customerReducer,
paymentMethodReducer, paymentMethodReducer,
ingredientReducer ingredientReducer,
orderReducer
}, },
middleware: getDefaultMiddleware => getDefaultMiddleware({ serializableCheck: false }) middleware: getDefaultMiddleware => getDefaultMiddleware({ serializableCheck: false })
}) })

View File

@ -0,0 +1,64 @@
// Third-party Imports
import type { PayloadAction } from '@reduxjs/toolkit'
import { createSlice } from '@reduxjs/toolkit'
// Type Imports
// Data Imports
import { Order } from '../../types/services/order'
const initialState: { currentOrder: Order } = {
currentOrder: {
id: '',
order_number: '',
outlet_id: '',
user_id: '',
table_number: '',
order_type: '',
status: '',
subtotal: 0,
tax_amount: 0,
discount_amount: 0,
total_amount: 0,
total_cost: 0,
remaining_amount: 0,
payment_status: '',
refund_amount: 0,
is_void: false,
is_refund: false,
notes: '',
metadata: {
customer_name: '',
last_split_amount: 0,
last_split_customer_id: '',
last_split_customer_name: '',
last_split_payment_id: '',
last_split_quantities: {},
last_split_type: ''
},
created_at: '',
updated_at: '',
order_items: [],
payments: [],
total_paid: 0,
payment_count: 0,
split_type: ''
}
}
export const orderSlice = createSlice({
name: 'order',
initialState,
reducers: {
setOrder: (state, action: PayloadAction<Order>) => {
state.currentOrder = action.payload
},
resetOrder: state => {
state.currentOrder = initialState.currentOrder
}
}
})
export const { setOrder, resetOrder } = orderSlice.actions
export default orderSlice.reducer

View File

@ -1,27 +1,52 @@
import { useMutation, useQueryClient } from '@tanstack/react-query' import { useMutation, useQueryClient } from '@tanstack/react-query'
import { CustomerRequest } from '../../types/services/customer'
import { api } from '../api' import { api } from '../api'
import { User } from '../../types/services/user' import { toast } from 'react-toastify'
type CreateUserPayload = { export const useCustomersMutation = () => {
name: string
email: string
}
const useUsersMutation = () => {
const queryClient = useQueryClient() const queryClient = useQueryClient()
const createUser = useMutation<User, Error, CreateUserPayload>({ const createCustomer = useMutation({
mutationFn: async newUser => { mutationFn: async (newCustomer: CustomerRequest) => {
const response = await api.post('/users', newUser) const response = await api.post('/customers', newCustomer)
return response.data return response.data
}, },
onSuccess: () => { onSuccess: () => {
// Optional: refetch 'users' list after success toast.success('Customer created successfully!')
queryClient.invalidateQueries({ queryKey: ['users'] }) queryClient.invalidateQueries({ queryKey: ['customers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Create failed')
} }
}) })
return { createUser } const updateCustomer = useMutation({
} mutationFn: async ({ id, payload }: { id: string; payload: CustomerRequest }) => {
const response = await api.put(`/customers/${id}`, payload)
return response.data
},
onSuccess: () => {
toast.success('Customer updated successfully!')
queryClient.invalidateQueries({ queryKey: ['customers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Update failed')
}
})
export default useUsersMutation const deleteCustomer = useMutation({
mutationFn: async (id: string) => {
const response = await api.delete(`/customers/${id}`)
return response.data
},
onSuccess: () => {
toast.success('Customer deleted successfully!')
queryClient.invalidateQueries({ queryKey: ['customers'] })
},
onError: (error: any) => {
toast.error(error.response?.data?.errors?.[0]?.cause || 'Delete failed')
}
})
return { createCustomer, updateCustomer, deleteCustomer }
}

View File

@ -1,5 +1,5 @@
import { useQuery } from '@tanstack/react-query' import { useQuery } from '@tanstack/react-query'
import { Orders } from '../../types/services/order' import { Order, Orders } from '../../types/services/order'
import { api } from '../api' import { api } from '../api'
interface OrdersQueryParams { interface OrdersQueryParams {
@ -34,3 +34,13 @@ export function useOrders(params: OrdersQueryParams = {}) {
} }
}) })
} }
export function useOrder(id: string) {
return useQuery<Order>({
queryKey: ['orders', id],
queryFn: async () => {
const res = await api.get(`/orders/${id}`)
return res.data.data
}
})
}

View File

@ -1,14 +1,36 @@
import { useQuery } from '@tanstack/react-query' import { useQuery } from '@tanstack/react-query'
import { Users } from '../../types/services/user'
import { api } from '../api' import { api } from '../api'
import { User } from '../../types/services/user'
interface UsersQueryParams {
page?: number
limit?: number
search?: string
}
export function useUsers() { export function useUsers(params: UsersQueryParams = {}) {
return useQuery<User[]>({ const { page = 1, limit = 10, search = '', ...filters } = params
queryKey: ['users'],
return useQuery<Users>({
queryKey: ['users', { page, limit, search, ...filters }],
queryFn: async () => { queryFn: async () => {
const res = await api.get('/users') const queryParams = new URLSearchParams()
return res.data
}, queryParams.append('page', page.toString())
queryParams.append('limit', limit.toString())
if (search) {
queryParams.append('search', search)
}
Object.entries(filters).forEach(([key, value]) => {
if (value !== undefined && value !== null && value !== '') {
queryParams.append(key, value.toString())
}
})
const res = await api.get(`/users?${queryParams.toString()}`)
return res.data.data
}
}) })
} }

View File

@ -1,12 +1,72 @@
export interface Orders { export type LastSplitQuantity = {
orders: Order[] quantity: number
total_count: number total_amount: number
page: number unit_price: number
limit: number
total_pages: number
} }
export interface Order { export type OrderMetadata = {
customer_name: string
last_split_amount: number
last_split_customer_id: string
last_split_customer_name: string
last_split_payment_id: string
last_split_quantities: Record<string, LastSplitQuantity>
last_split_type: string
}
export type OrderItem = {
id: string
order_id: string
product_id: string
product_name: string
product_variant_id: string | null
quantity: number
unit_price: number
total_price: number
modifiers: unknown[] // Adjust if modifiers have a defined structure
notes: string
status: string
created_at: string
updated_at: string
printer_type: string
paid_quantity: number
}
export type PaymentOrderItem = {
id: string
payment_id: string
order_item_id: string
amount: number
created_at: string
updated_at: string
}
export type PaymentMetadata = {
customer_id: string
customer_name: string
split_type: string
}
export type Payment = {
id: string
order_id: string
payment_method_id: string
payment_method_name: string
payment_method_type: string
amount: number
status: string
split_number: number
split_total: number
split_type: string
split_description: string
refund_amount: number
metadata: PaymentMetadata
created_at: string
updated_at: string
payment_order_items: PaymentOrderItem[]
}
export type Order = {
id: string id: string
order_number: string order_number: string
outlet_id: string outlet_id: string
@ -18,28 +78,27 @@ export interface Order {
tax_amount: number tax_amount: number
discount_amount: number discount_amount: number
total_amount: number total_amount: number
total_cost: number
remaining_amount: number
payment_status: string
refund_amount: number
is_void: boolean
is_refund: boolean
notes: string | null notes: string | null
metadata: { metadata: OrderMetadata
customer_name: string
}
created_at: string created_at: string
updated_at: string updated_at: string
order_items: OrderItem[] order_items: OrderItem[]
payments: Payment[]
total_paid: number
payment_count: number
split_type: string
} }
export interface OrderItem { export type Orders = {
id: string orders: Order[],
order_id: string total_count: number
product_id: string page: number
product_name: string limit: number
product_variant_id: string | null total_pages: number
product_variant_name?: string
quantity: number
unit_price: number
total_price: number
modifiers: any[]
notes: string
status: string
created_at: string
updated_at: string
} }

View File

@ -1,5 +1,35 @@
export type User = { export type User = {
id: string id: string;
name: string organization_id: string;
email: string outlet_id: string;
name: string;
email: string;
role: string;
permissions: Record<string, unknown>;
is_active: boolean;
created_at: string; // ISO date string
updated_at: string; // ISO date string
};
type Pagination = {
total_count: number;
page: number;
limit: number;
total_pages: number;
};
export type Users = {
users: User[];
pagination: Pagination;
};
export type UserRequest = {
organization_id: string;
outlet_id: string;
name: string;
email: string;
password: string;
role: string;
permissions: Record<string, unknown>;
is_active: boolean;
} }

View File

@ -2,64 +2,79 @@
import Card from '@mui/material/Card' import Card from '@mui/material/Card'
import CardContent from '@mui/material/CardContent' import CardContent from '@mui/material/CardContent'
import Typography from '@mui/material/Typography' import Typography from '@mui/material/Typography'
import type { TypographyProps } from '@mui/material/Typography'
// Type Imports // Type Imports
import type { ThemeColor } from '@core/types' import type { ThemeColor } from '@core/types'
import classnames from 'classnames'
// Component Imports // Component Imports
import AddAddress from '@components/dialogs/add-edit-address' import CustomAvatar from '../../../../../@core/components/mui/Avatar'
import OpenDialogOnElementClick from '@components/dialogs/OpenDialogOnElementClick' import { Order } from '../../../../../types/services/order'
import { formatCurrency } from '../../../../../utils/transform'
// Vars type PayementStatusType = {
const data = { text: string
firstName: 'Roker', color: ThemeColor
lastName: 'Terrace', colorClassName: string
email: 'sbaser0@boston.com',
country: 'UK',
address1: 'Latheronwheel',
address2: 'KW5 8NW, London',
landmark: 'Near Water Plant',
city: 'London',
state: 'Capholim',
zipCode: '403114',
taxId: 'TAX-875623',
vatNumber: 'SDF754K77',
contact: '+1 (609) 972-22-22'
} }
const BillingAddress = () => { const statusChipColor: { [key: string]: PayementStatusType } = {
// Vars pending: {
const typographyProps = (children: string, color: ThemeColor, className: string): TypographyProps => ({ color: 'warning',
children, text: 'Pending',
color, colorClassName: 'text-warning'
className },
}) completed: {
color: 'success',
text: 'Paid',
colorClassName: 'text-success'
},
cancelled: {
color: 'error',
text: 'Cancelled',
colorClassName: 'text-error'
}
}
const BillingAddress = ({ data }: { data: Order }) => {
return ( return (
<Card> <Card>
<CardContent className='flex flex-col gap-6'> <CardContent className='flex flex-col gap-6'>
<div className='flex flex-col gap-2'> <div className='flex flex-col gap-2'>
<div className='flex justify-between items-center'> <div className='flex justify-between items-center'>
<Typography variant='h5'>Billing Address</Typography> <Typography variant='h5'>
<OpenDialogOnElementClick Payment Details ({data.payments.length} {data.payments.length === 1 ? 'Payment' : 'Payments'})
element={Typography} </Typography>
elementProps={typographyProps('Edit', 'primary', 'cursor-pointer font-medium')}
dialog={AddAddress}
dialogProps={{ type: 'Add address for billing address', data }}
/>
</div> </div>
</div>
{data.payments.map((payment, index) => (
<div key={index}>
<div className='flex items-center gap-3'>
<CustomAvatar skin='light' color='secondary' size={40}>
<i className='tabler-credit-card' />
</CustomAvatar>
<div className='flex flex-col'> <div className='flex flex-col'>
<Typography>45 Roker Terrace</Typography> <div className='font-medium flex items-center gap-3'>
<Typography>Latheronwheel</Typography> <Typography color='text.primary'>{payment.payment_method_name}</Typography>
<Typography>KW5 8NW, London</Typography> <div className='flex items-center gap-1'>
<Typography>UK</Typography> <i
className={classnames(
'tabler-circle-filled bs-1.5 is-1.5',
statusChipColor[payment.status].colorClassName
)}
/>
<Typography color={`${statusChipColor[payment.status].color}.main`} className='font-medium text-xs'>
{statusChipColor[payment.status].text}
</Typography>
</div> </div>
</div> </div>
<div className='flex flex-col items-start gap-1'> <Typography color='text.secondary' className='font-medium'>
<Typography variant='h5'>Mastercard</Typography> {formatCurrency(payment.amount)}
<Typography>Card Number: ******4291</Typography> </Typography>
</div> </div>
</div>
</div>
))}
</CardContent> </CardContent>
</Card> </Card>
) )

View File

@ -1,21 +1,18 @@
// MUI Imports // MUI Imports
import Avatar from '@mui/material/Avatar'
import Card from '@mui/material/Card' import Card from '@mui/material/Card'
import CardContent from '@mui/material/CardContent' import CardContent from '@mui/material/CardContent'
import Avatar from '@mui/material/Avatar'
import Typography from '@mui/material/Typography' import Typography from '@mui/material/Typography'
import type { TypographyProps } from '@mui/material/Typography'
// Type Imports // Type Imports
import type { ThemeColor } from '@core/types'
import type { OrderType } from '@/types/apps/ecommerceTypes' import type { OrderType } from '@/types/apps/ecommerceTypes'
// Component Imports // Component Imports
import CustomAvatar from '@core/components/mui/Avatar' import CustomAvatar from '@core/components/mui/Avatar'
import EditUserInfo from '@components/dialogs/edit-user-info'
import OpenDialogOnElementClick from '@components/dialogs/OpenDialogOnElementClick'
// Util Imports // Util Imports
import { getInitials } from '@/utils/getInitials' import { getInitials } from '@/utils/getInitials'
import { Order } from '../../../../../types/services/order'
const getAvatar = (params: Pick<OrderType, 'avatar' | 'customer'>) => { const getAvatar = (params: Pick<OrderType, 'avatar' | 'customer'>) => {
const { avatar, customer } = params const { avatar, customer } = params
@ -42,25 +39,17 @@ const userData = {
useAsBillingAddress: true useAsBillingAddress: true
} }
const CustomerDetails = ({ orderData }: { orderData?: OrderType }) => { const CustomerDetails = ({ orderData }: { orderData?: Order }) => {
// Vars
const typographyProps = (children: string, color: ThemeColor, className: string): TypographyProps => ({
children,
color,
className
})
return ( return (
<Card> <Card>
<CardContent className='flex flex-col gap-6'> <CardContent className='flex flex-col gap-6'>
<Typography variant='h5'>Customer details</Typography> <Typography variant='h5'>Customer details</Typography>
<div className='flex items-center gap-3'> <div className='flex items-center gap-3'>
{getAvatar({ avatar: orderData?.avatar ?? '', customer: orderData?.customer ?? '' })} {getAvatar({ avatar: '', customer: orderData?.metadata.customer_name ?? '' })}
<div className='flex flex-col'> <div className='flex flex-col'>
<Typography color='text.primary' className='font-medium'> <Typography color='text.primary' className='font-medium'>
{orderData?.customer} {orderData?.metadata.customer_name}
</Typography> </Typography>
<Typography>Customer ID: #47389</Typography>
</div> </div>
</div> </div>
<div className='flex items-center gap-3'> <div className='flex items-center gap-3'>
@ -68,24 +57,9 @@ const CustomerDetails = ({ orderData }: { orderData?: OrderType }) => {
<i className='tabler-shopping-cart' /> <i className='tabler-shopping-cart' />
</CustomAvatar> </CustomAvatar>
<Typography color='text.primary' className='font-medium'> <Typography color='text.primary' className='font-medium'>
12 Orders {orderData?.order_items.length} {orderData?.order_items.length === 1 ? 'Order' : 'Orders'}
</Typography> </Typography>
</div> </div>
<div className='flex flex-col gap-1'>
<div className='flex justify-between items-center'>
<Typography color='text.primary' className='font-medium'>
Contact info
</Typography>
<OpenDialogOnElementClick
element={Typography}
elementProps={typographyProps('Edit', 'primary', 'cursor-pointer font-medium')}
dialog={EditUserInfo}
dialogProps={{ data: userData }}
/>
</div>
<Typography>Email: {orderData?.email}</Typography>
<Typography>Mobile: +1 (609) 972-22-22</Typography>
</div>
</CardContent> </CardContent>
</Card> </Card>
) )

View File

@ -1,16 +1,17 @@
// MUI Imports // MUI Imports
import type { ButtonProps } from '@mui/material/Button'
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 Typography from '@mui/material/Typography' import Typography from '@mui/material/Typography'
import type { ButtonProps } from '@mui/material/Button'
// Type Imports // Type Imports
import type { ThemeColor } from '@core/types' import type { ThemeColor } from '@core/types'
import type { OrderType } from '@/types/apps/ecommerceTypes'
// Component Imports // Component Imports
import ConfirmationDialog from '@components/dialogs/confirmation-dialog' import ConfirmationDialog from '@components/dialogs/confirmation-dialog'
import OpenDialogOnElementClick from '@components/dialogs/OpenDialogOnElementClick' import OpenDialogOnElementClick from '@components/dialogs/OpenDialogOnElementClick'
import { Order } from '../../../../../types/services/order'
import { formatDate } from '../../../../../utils/transform'
type PayementStatusType = { type PayementStatusType = {
text: string text: string
@ -22,20 +23,20 @@ type StatusChipColorType = {
} }
export const paymentStatus: { [key: number]: PayementStatusType } = { export const paymentStatus: { [key: number]: PayementStatusType } = {
1: { text: 'Paid', color: 'success' }, 1: { text: 'paid', color: 'success' },
2: { text: 'Pending', color: 'warning' }, 2: { text: 'pending', color: 'warning' },
3: { text: 'Cancelled', color: 'secondary' }, 3: { text: 'cancelled', color: 'secondary' },
4: { text: 'Failed', color: 'error' } 4: { text: 'failed', color: 'error' }
} }
export const statusChipColor: { [key: string]: StatusChipColorType } = { export const statusChipColor: { [key: string]: StatusChipColorType } = {
Delivered: { color: 'success' }, 'pending': { color: 'warning' },
'Out for Delivery': { color: 'primary' }, 'completed': { color: 'success' },
'Ready to Pickup': { color: 'info' }, 'partial': { color: 'secondary' },
Dispatched: { color: 'warning' } 'cancelled': { color: 'error' }
} }
const OrderDetailHeader = ({ orderData, order }: { orderData?: OrderType; order: string }) => { const OrderDetailHeader = ({ orderData }: { orderData?: Order }) => {
// Vars // Vars
const buttonProps = (children: string, color: ThemeColor, variant: ButtonProps['variant']): ButtonProps => ({ const buttonProps = (children: string, color: ThemeColor, variant: ButtonProps['variant']): ButtonProps => ({
children, children,
@ -47,7 +48,7 @@ const OrderDetailHeader = ({ orderData, order }: { orderData?: OrderType; order:
<div className='flex flex-wrap justify-between sm:items-center max-sm:flex-col gap-y-4'> <div className='flex flex-wrap justify-between sm:items-center max-sm:flex-col gap-y-4'>
<div className='flex flex-col items-start gap-1'> <div className='flex flex-col items-start gap-1'>
<div className='flex items-center gap-2'> <div className='flex items-center gap-2'>
<Typography variant='h5'>{`Order #${order}`}</Typography> <Typography variant='h5'>{`Order #${orderData?.order_number}`}</Typography>
<Chip <Chip
variant='tonal' variant='tonal'
label={orderData?.status} label={orderData?.status}
@ -56,12 +57,12 @@ const OrderDetailHeader = ({ orderData, order }: { orderData?: OrderType; order:
/> />
<Chip <Chip
variant='tonal' variant='tonal'
label={paymentStatus[orderData?.payment ?? 0].text} label={orderData?.payment_status || ''}
color={paymentStatus[orderData?.payment ?? 0].color} color={statusChipColor[orderData?.payment_status || ''].color}
size='small' size='small'
/> />
</div> </div>
<Typography>{`${new Date(orderData?.date ?? '').toDateString()}, ${orderData?.time} (ET)`}</Typography> <Typography>{`${formatDate(orderData!.created_at)}`}</Typography>
</div> </div>
<OpenDialogOnElementClick <OpenDialogOnElementClick
element={Button} element={Button}

View File

@ -1,37 +1,39 @@
'use client' 'use client'
// React Imports // React Imports
import { useState, useMemo } from 'react' import { useMemo, useState } from 'react'
// MUI Imports // MUI Imports
import Card from '@mui/material/Card' import Card from '@mui/material/Card'
import CardHeader from '@mui/material/CardHeader'
import CardContent from '@mui/material/CardContent' import CardContent from '@mui/material/CardContent'
import CardHeader from '@mui/material/CardHeader'
import Checkbox from '@mui/material/Checkbox' import Checkbox from '@mui/material/Checkbox'
import Typography from '@mui/material/Typography' import Typography from '@mui/material/Typography'
// Third-party Imports // Third-party Imports
import classnames from 'classnames'
import { rankItem } from '@tanstack/match-sorter-utils' import { rankItem } from '@tanstack/match-sorter-utils'
import type { ColumnDef, FilterFn } from '@tanstack/react-table'
import { import {
createColumnHelper, createColumnHelper,
flexRender, flexRender,
getCoreRowModel, getCoreRowModel,
useReactTable, getFacetedMinMaxValues,
getFilteredRowModel,
getFacetedRowModel, getFacetedRowModel,
getFacetedUniqueValues, getFacetedUniqueValues,
getFacetedMinMaxValues, getFilteredRowModel,
getPaginationRowModel, getPaginationRowModel,
getSortedRowModel getSortedRowModel,
useReactTable
} from '@tanstack/react-table' } from '@tanstack/react-table'
import type { ColumnDef, FilterFn } from '@tanstack/react-table' import classnames from 'classnames'
// Component Imports // Component Imports
import Link from '@components/Link'
// Style Imports // Style Imports
import tableStyles from '@core/styles/table.module.css' import tableStyles from '@core/styles/table.module.css'
import { ThemeColor } from '../../../../../@core/types'
import { Order, OrderItem } from '../../../../../types/services/order'
import { formatCurrency } from '../../../../../utils/transform'
const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => { const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
// Rank the item // Rank the item
@ -47,57 +49,44 @@ const fuzzyFilter: FilterFn<any> = (row, columnId, value, addMeta) => {
} }
type dataType = { type dataType = {
productName: string product_name: string
productImage: string status: string
brand: string unit_price: number
price: number
quantity: number quantity: number
total: number total_price: number
} }
const orderData: dataType[] = [ type PayementStatusType = {
{ text: string
productName: 'OnePlus 7 Pro', color: ThemeColor
productImage: '/images/apps/ecommerce/product-21.png', colorClassName: string
brand: 'OnePluse', }
price: 799,
quantity: 1, const statusChipColor: { [key: string]: PayementStatusType } = {
total: 799 pending: {
color: 'warning',
text: 'Pending',
colorClassName: 'text-warning'
}, },
{ paid: {
productName: 'Magic Mouse', color: 'success',
productImage: '/images/apps/ecommerce/product-22.png', text: 'Paid',
brand: 'Google', colorClassName: 'text-success'
price: 89,
quantity: 1,
total: 89
}, },
{ cancelled: {
productName: 'Wooden Chair', color: 'error',
productImage: '/images/apps/ecommerce/product-23.png', text: 'Cancelled',
brand: 'Insofar', colorClassName: 'text-error'
price: 289,
quantity: 2,
total: 578
},
{
productName: 'Air Jorden',
productImage: '/images/apps/ecommerce/product-24.png',
brand: 'Nike',
price: 299,
quantity: 2,
total: 598
} }
] }
// Column Definitions // Column Definitions
const columnHelper = createColumnHelper<dataType>() const columnHelper = createColumnHelper<dataType>()
const OrderTable = () => { const OrderTable = ({ data }: { data: OrderItem[] }) => {
// States // States
const [rowSelection, setRowSelection] = useState({}) const [rowSelection, setRowSelection] = useState({})
// eslint-disable-next-line @typescript-eslint/no-unused-vars // eslint-disable-next-line @typescript-eslint/no-unused-vars
const [data, setData] = useState(...[orderData])
const [globalFilter, setGlobalFilter] = useState('') const [globalFilter, setGlobalFilter] = useState('')
const columns = useMemo<ColumnDef<dataType, any>[]>( const columns = useMemo<ColumnDef<dataType, any>[]>(
@ -124,31 +113,43 @@ const OrderTable = () => {
/> />
) )
}, },
columnHelper.accessor('productName', { columnHelper.accessor('product_name', {
header: 'Product', header: 'Product',
cell: ({ row }) => ( cell: ({ row }) => (
<div className='flex items-center gap-3'> <div className='flex items-center gap-3'>
<img src={row.original.productImage} alt={row.original.productName} height={34} className='rounded' />
<div className='flex flex-col items-start'> <div className='flex flex-col items-start'>
<Typography color='text.primary' className='font-medium'> <Typography color='text.primary' className='font-medium'>
{row.original.productName} {row.original.product_name}
</Typography> </Typography>
<Typography variant='body2'>{row.original.brand}</Typography> <div className='flex items-center gap-1'>
<i
className={classnames(
'tabler-circle-filled bs-2.5 is-2.5',
statusChipColor[row.original.status].colorClassName
)}
/>
<Typography
color={`${statusChipColor[row.original.status].color}.main`}
className='font-medium text-xs'
>
{statusChipColor[row.original.status].text}
</Typography>
</div>
</div> </div>
</div> </div>
) )
}), }),
columnHelper.accessor('price', { columnHelper.accessor('unit_price', {
header: 'Price', header: 'Price',
cell: ({ row }) => <Typography>{`$${row.original.price}`}</Typography> cell: ({ row }) => <Typography>{formatCurrency(row.original.unit_price)}</Typography>
}), }),
columnHelper.accessor('quantity', { columnHelper.accessor('quantity', {
header: 'Qty', header: 'Qty',
cell: ({ row }) => <Typography>{`${row.original.quantity}`}</Typography> cell: ({ row }) => <Typography>{`${row.original.quantity}`}</Typography>
}), }),
columnHelper.accessor('total', { columnHelper.accessor('total_price', {
header: 'Total', header: 'Total',
cell: ({ row }) => <Typography>{`$${row.original.total}`}</Typography> cell: ({ row }) => <Typography>{formatCurrency(row.original.total_price)}</Typography>
}) })
], ],
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
@ -156,7 +157,7 @@ const OrderTable = () => {
) )
const table = useReactTable({ const table = useReactTable({
data: data as dataType[], data: data as OrderItem[],
columns, columns,
filterFns: { filterFns: {
fuzzy: fuzzyFilter fuzzy: fuzzyFilter
@ -243,18 +244,11 @@ const OrderTable = () => {
) )
} }
const OrderDetailsCard = () => { const OrderDetailsCard = ({ data }: { data: Order }) => {
return ( return (
<Card> <Card>
<CardHeader <CardHeader title='Order Details' />
title='Order Details' <OrderTable data={data.order_items} />
action={
<Typography component={Link} color='primary.main' className='font-medium'>
Edit
</Typography>
}
/>
<OrderTable />
<CardContent className='flex justify-end'> <CardContent className='flex justify-end'>
<div> <div>
<div className='flex items-center gap-12'> <div className='flex items-center gap-12'>
@ -262,15 +256,15 @@ const OrderDetailsCard = () => {
Subtotal: Subtotal:
</Typography> </Typography>
<Typography color='text.primary' className='font-medium'> <Typography color='text.primary' className='font-medium'>
$2,093 {formatCurrency(data.subtotal)}
</Typography> </Typography>
</div> </div>
<div className='flex items-center gap-12'> <div className='flex items-center gap-12'>
<Typography color='text.primary' className='min-is-[100px]'> <Typography color='text.primary' className='min-is-[100px]'>
Shipping Fee: Discount
</Typography> </Typography>
<Typography color='text.primary' className='font-medium'> <Typography color='text.primary' className='font-medium'>
$2 {formatCurrency(data.discount_amount)}
</Typography> </Typography>
</div> </div>
<div className='flex items-center gap-12'> <div className='flex items-center gap-12'>
@ -278,7 +272,7 @@ const OrderDetailsCard = () => {
Tax: Tax:
</Typography> </Typography>
<Typography color='text.primary' className='font-medium'> <Typography color='text.primary' className='font-medium'>
$28 {formatCurrency(data.tax_amount)}
</Typography> </Typography>
</div> </div>
<div className='flex items-center gap-12'> <div className='flex items-center gap-12'>
@ -286,7 +280,7 @@ const OrderDetailsCard = () => {
Total: Total:
</Typography> </Typography>
<Typography color='text.primary' className='font-medium'> <Typography color='text.primary' className='font-medium'>
$2113 {formatCurrency(data.total_amount)}
</Typography> </Typography>
</div> </div>
</div> </div>

View File

@ -1,43 +1,59 @@
'use client'
// MUI Imports // MUI Imports
import Grid from '@mui/material/Grid2' import Grid from '@mui/material/Grid2'
// Type Imports // Type Imports
import type { OrderType } from '@/types/apps/ecommerceTypes'
// Component Imports // Component Imports
import { redirect, useParams } from 'next/navigation'
import Loading from '../../../../../components/layout/shared/Loading'
import { useOrder } from '../../../../../services/queries/orders'
import BillingAddress from './BillingAddressCard'
import CustomerDetails from './CustomerDetailsCard'
import OrderDetailHeader from './OrderDetailHeader' import OrderDetailHeader from './OrderDetailHeader'
import OrderDetailsCard from './OrderDetailsCard' import OrderDetailsCard from './OrderDetailsCard'
import ShippingActivity from './ShippingActivityCard'
import CustomerDetails from './CustomerDetailsCard'
import ShippingAddress from './ShippingAddressCard' import ShippingAddress from './ShippingAddressCard'
import BillingAddress from './BillingAddressCard'
const OrderDetails = ({ orderData, order }: { orderData?: OrderType; order: string }) => { const OrderDetails = () => {
const params = useParams()
const { data, isLoading } = useOrder(params.id as string)
if (isLoading) {
return <Loading />
}
if (!data) {
redirect('not-found')
}
return ( return (
<Grid container spacing={6}> <Grid container spacing={6}>
<Grid size={{ xs: 12 }}> <Grid size={{ xs: 12 }}>
<OrderDetailHeader orderData={orderData} order={order} /> <OrderDetailHeader orderData={data} />
</Grid> </Grid>
<Grid size={{ xs: 12, md: 8 }}> <Grid size={{ xs: 12, md: 8 }}>
<Grid container spacing={6}> <Grid container spacing={6}>
<Grid size={{ xs: 12 }}> <Grid size={{ xs: 12 }}>
<OrderDetailsCard /> <OrderDetailsCard data={data} />
</Grid>
<Grid size={{ xs: 12 }}>
<ShippingActivity order={order} />
</Grid> </Grid>
{/* <Grid size={{ xs: 12 }}>
<ShippingActivity order={data.order_number} />
</Grid> */}
</Grid> </Grid>
</Grid> </Grid>
<Grid size={{ xs: 12, md: 4 }}> <Grid size={{ xs: 12, md: 4 }}>
<Grid container spacing={6}> <Grid container spacing={6}>
<Grid size={{ xs: 12 }}> <Grid size={{ xs: 12 }}>
<CustomerDetails orderData={orderData} /> <CustomerDetails orderData={data} />
</Grid> </Grid>
<Grid size={{ xs: 12 }}> {/* <Grid size={{ xs: 12 }}>
<ShippingAddress /> <ShippingAddress />
</Grid> </Grid> */}
<Grid size={{ xs: 12 }}> <Grid size={{ xs: 12 }}>
<BillingAddress /> <BillingAddress data={data} />
</Grid> </Grid>
</Grid> </Grid>
</Grid> </Grid>

View File

@ -215,7 +215,7 @@ const OrderListTable = () => {
text: 'View', text: 'View',
icon: 'tabler-eye', icon: 'tabler-eye',
href: getLocalizedUrl( href: getLocalizedUrl(
`/apps/ecommerce/orders/details/${row.original.order_number}`, `/apps/ecommerce/orders/${row.original.id}/details`,
locale as Locale locale as Locale
), ),
linkProps: { className: 'flex items-center gap-2 is-full plb-2 pli-4' } linkProps: { className: 'flex items-center gap-2 is-full plb-2 pli-4' }

View File

@ -3,94 +3,50 @@ import { useState } from 'react'
// MUI Imports // MUI Imports
import Button from '@mui/material/Button' import Button from '@mui/material/Button'
import Divider from '@mui/material/Divider'
import Drawer from '@mui/material/Drawer' import Drawer from '@mui/material/Drawer'
import IconButton from '@mui/material/IconButton' import IconButton from '@mui/material/IconButton'
import MenuItem from '@mui/material/MenuItem' import MenuItem from '@mui/material/MenuItem'
import Typography from '@mui/material/Typography' import Typography from '@mui/material/Typography'
import Divider from '@mui/material/Divider'
// Third-party Imports // Third-party Imports
import { useForm, Controller } from 'react-hook-form' import { Controller, useForm } from 'react-hook-form'
// Types Imports // Types Imports
import type { UsersType } from '@/types/apps/userTypes' import type { UsersType } from '@/types/apps/userTypes'
// Component Imports // Component Imports
import CustomTextField from '@core/components/mui/TextField' import CustomTextField from '@core/components/mui/TextField'
import { UserRequest } from '../../../../types/services/user'
import { Switch } from '@mui/material'
type Props = { type Props = {
open: boolean open: boolean
handleClose: () => void handleClose: () => void
userData?: UsersType[]
setData: (data: UsersType[]) => void
}
type FormValidateType = {
fullName: string
username: string
email: string
role: string
plan: string
status: string
}
type FormNonValidateType = {
company: string
country: string
contact: string
} }
// Vars // Vars
const initialData = { const initialData = {
company: '', name: '',
country: '', email: '',
contact: '' password: '',
role: '',
permissions: {},
is_active: true,
organization_id: '',
outlet_id: '',
} }
const AddUserDrawer = (props: Props) => { const AddUserDrawer = (props: Props) => {
// Props // Props
const { open, handleClose, userData, setData } = props const { open, handleClose } = props
// States // States
const [formData, setFormData] = useState<FormNonValidateType>(initialData) const [formData, setFormData] = useState<UserRequest>(initialData)
// Hooks const onSubmit = () => {
const {
control,
reset: resetForm,
handleSubmit,
formState: { errors }
} = useForm<FormValidateType>({
defaultValues: {
fullName: '',
username: '',
email: '',
role: '',
plan: '',
status: ''
}
})
const onSubmit = (data: FormValidateType) => {
const newUser: UsersType = {
id: (userData?.length && userData?.length + 1) || 1,
avatar: `/images/avatars/${Math.floor(Math.random() * 8) + 1}.png`,
fullName: data.fullName,
username: data.username,
email: data.email,
role: data.role,
currentPlan: data.plan,
status: data.status,
company: formData.company,
country: formData.country,
contact: formData.contact,
billing: userData?.[Math.floor(Math.random() * 50) + 1].billing ?? 'Auto Debit'
}
setData([...(userData ?? []), newUser])
handleClose() handleClose()
setFormData(initialData) setFormData(initialData)
resetForm({ fullName: '', username: '', email: '', role: '', plan: '', status: '' })
} }
const handleReset = () => { const handleReset = () => {
@ -98,6 +54,13 @@ const AddUserDrawer = (props: Props) => {
setFormData(initialData) setFormData(initialData)
} }
const handleInputChange = (e: any) => {
setFormData({
...formData,
[e.target.name]: e.target.value
})
}
return ( return (
<Drawer <Drawer
open={open} open={open}
@ -115,144 +78,45 @@ const AddUserDrawer = (props: Props) => {
</div> </div>
<Divider /> <Divider />
<div> <div>
<form onSubmit={handleSubmit(data => onSubmit(data))} className='flex flex-col gap-6 p-6'> <form onSubmit={onSubmit} className='flex flex-col gap-6 p-6'>
<Controller
name='fullName'
control={control}
rules={{ required: true }}
render={({ field }) => (
<CustomTextField <CustomTextField
{...field}
fullWidth fullWidth
label='Full Name' label='Name'
placeholder='John Doe' placeholder='John Doe'
{...(errors.fullName && { error: true, helperText: 'This field is required.' })} name='name'
value={formData.name}
onChange={handleInputChange}
/> />
)}
/>
<Controller
name='username'
control={control}
rules={{ required: true }}
render={({ field }) => (
<CustomTextField <CustomTextField
{...field}
fullWidth
label='Username'
placeholder='johndoe'
{...(errors.username && { error: true, helperText: 'This field is required.' })}
/>
)}
/>
<Controller
name='email'
control={control}
rules={{ required: true }}
render={({ field }) => (
<CustomTextField
{...field}
fullWidth fullWidth
type='email' type='email'
label='Email' label='Email'
placeholder='johndoe@gmail.com' placeholder='johndoe@email'
{...(errors.email && { error: true, helperText: 'This field is required.' })} name='email'
/> value={formData.email}
)} onChange={handleInputChange}
/>
<Controller
name='role'
control={control}
rules={{ required: true }}
render={({ field }) => (
<CustomTextField
select
fullWidth
id='select-role'
label='Select Role'
{...field}
{...(errors.role && { error: true, helperText: 'This field is required.' })}
>
<MenuItem value='admin'>Admin</MenuItem>
<MenuItem value='author'>Author</MenuItem>
<MenuItem value='editor'>Editor</MenuItem>
<MenuItem value='maintainer'>Maintainer</MenuItem>
<MenuItem value='subscriber'>Subscriber</MenuItem>
</CustomTextField>
)}
/>
<Controller
name='plan'
control={control}
rules={{ required: true }}
render={({ field }) => (
<CustomTextField
select
fullWidth
id='select-plan'
label='Select Plan'
{...field}
slotProps={{
htmlInput: { placeholder: 'Select Plan' }
}}
{...(errors.plan && { error: true, helperText: 'This field is required.' })}
>
<MenuItem value='basic'>Basic</MenuItem>
<MenuItem value='company'>Company</MenuItem>
<MenuItem value='enterprise'>Enterprise</MenuItem>
<MenuItem value='team'>Team</MenuItem>
</CustomTextField>
)}
/>
<Controller
name='status'
control={control}
rules={{ required: true }}
render={({ field }) => (
<CustomTextField
select
fullWidth
id='select-status'
label='Select Status'
{...field}
{...(errors.status && { error: true, helperText: 'This field is required.' })}
>
<MenuItem value='pending'>Pending</MenuItem>
<MenuItem value='active'>Active</MenuItem>
<MenuItem value='inactive'>Inactive</MenuItem>
</CustomTextField>
)}
/> />
<CustomTextField <CustomTextField
label='Company'
fullWidth fullWidth
placeholder='Company PVT LTD' type='password'
value={formData.company} label='Password'
onChange={e => setFormData({ ...formData, company: e.target.value })} placeholder='********'
name='password'
value={formData.password}
onChange={handleInputChange}
/> />
<CustomTextField <div className='flex items-center'>
select <div className='flex flex-col items-start gap-1'>
fullWidth <Typography color='text.primary' className='font-medium'>
id='country' Active
value={formData.country} </Typography>
onChange={e => setFormData({ ...formData, country: e.target.value })} </div>
label='Select Country' <Switch
slotProps={{ checked={formData.is_active}
htmlInput: { placeholder: 'Country' } name='is_active'
}} onChange={e => setFormData({ ...formData, is_active: e.target.checked })}
>
<MenuItem value='India'>India</MenuItem>
<MenuItem value='USA'>USA</MenuItem>
<MenuItem value='Australia'>Australia</MenuItem>
<MenuItem value='Germany'>Germany</MenuItem>
</CustomTextField>
<CustomTextField
label='Contact'
type='number'
fullWidth
placeholder='(397) 294-5153'
value={formData.contact}
onChange={e => setFormData({ ...formData, contact: e.target.value })}
/> />
</div>
<div className='flex items-center gap-4'> <div className='flex items-center gap-4'>
<Button variant='contained' type='submit'> <Button variant='contained' type='submit'>
Submit Submit

View File

@ -1,7 +1,7 @@
'use client' 'use client'
// React Imports // React Imports
import { useEffect, useMemo, useState } from 'react' import { useCallback, useEffect, useMemo, useState } from 'react'
// Next Imports // Next Imports
import Link from 'next/link' import Link from 'next/link'
@ -23,31 +23,17 @@ import Typography from '@mui/material/Typography'
import type { RankingInfo } from '@tanstack/match-sorter-utils' import type { RankingInfo } from '@tanstack/match-sorter-utils'
import { rankItem } from '@tanstack/match-sorter-utils' import { rankItem } from '@tanstack/match-sorter-utils'
import type { ColumnDef, FilterFn } from '@tanstack/react-table' import type { ColumnDef, FilterFn } from '@tanstack/react-table'
import { import { createColumnHelper, flexRender, getCoreRowModel, useReactTable } from '@tanstack/react-table'
createColumnHelper,
flexRender,
getCoreRowModel,
getFacetedMinMaxValues,
getFacetedRowModel,
getFacetedUniqueValues,
getFilteredRowModel,
getPaginationRowModel,
getSortedRowModel,
useReactTable
} from '@tanstack/react-table'
import classnames from 'classnames' import classnames from 'classnames'
// Type Imports // Type Imports
import type { UsersType } from '@/types/apps/userTypes' import type { UsersType } from '@/types/apps/userTypes'
import type { Locale } from '@configs/i18n' import type { Locale } from '@configs/i18n'
import type { ThemeColor } from '@core/types'
// Component Imports // Component Imports
import CustomAvatar from '@core/components/mui/Avatar' import CustomAvatar from '@core/components/mui/Avatar'
import CustomTextField from '@core/components/mui/TextField' import CustomTextField from '@core/components/mui/TextField'
import OptionMenu from '@core/components/option-menu' import OptionMenu from '@core/components/option-menu'
import AddUserDrawer from './AddUserDrawer'
import TableFilters from './TableFilters'
// Util Imports // Util Imports
import { getInitials } from '@/utils/getInitials' import { getInitials } from '@/utils/getInitials'
@ -55,6 +41,12 @@ import { getLocalizedUrl } from '@/utils/i18n'
// Style Imports // Style Imports
import tableStyles from '@core/styles/table.module.css' import tableStyles from '@core/styles/table.module.css'
import { Box, CircularProgress, TablePagination } from '@mui/material'
import Loading from '../../../../components/layout/shared/Loading'
import TablePaginationComponent from '../../../../components/TablePaginationComponent'
import { useUsers } from '../../../../services/queries/users'
import { User } from '../../../../types/services/user'
import AddUserDrawer from './AddUserDrawer'
declare module '@tanstack/table-core' { declare module '@tanstack/table-core' {
interface FilterFns { interface FilterFns {
@ -65,18 +57,14 @@ declare module '@tanstack/table-core' {
} }
} }
type UsersTypeWithAction = UsersType & { type UsersTypeWithAction = User & {
action?: string actions?: string
} }
type UserRoleType = { type UserRoleType = {
[key: string]: { icon: string; color: string } [key: string]: { icon: string; color: string }
} }
type UserStatusType = {
[key: string]: ThemeColor
}
// Styled Components // Styled Components
const Icon = styled('i')({}) const Icon = styled('i')({})
@ -127,30 +115,54 @@ const userRoleObj: UserRoleType = {
admin: { icon: 'tabler-crown', color: 'error' }, admin: { icon: 'tabler-crown', color: 'error' },
author: { icon: 'tabler-device-desktop', color: 'warning' }, author: { icon: 'tabler-device-desktop', color: 'warning' },
editor: { icon: 'tabler-edit', color: 'info' }, editor: { icon: 'tabler-edit', color: 'info' },
maintainer: { icon: 'tabler-chart-pie', color: 'success' }, cashier: { icon: 'tabler-chart-pie', color: 'success' },
subscriber: { icon: 'tabler-user', color: 'primary' } subscriber: { icon: 'tabler-user', color: 'primary' }
} }
const userStatusObj: UserStatusType = {
active: 'success',
pending: 'warning',
inactive: 'secondary'
}
// Column Definitions // Column Definitions
const columnHelper = createColumnHelper<UsersTypeWithAction>() const columnHelper = createColumnHelper<UsersTypeWithAction>()
const UserListTable = ({ tableData }: { tableData?: UsersType[] }) => { const UserListTable = () => {
// States // States
const [addUserOpen, setAddUserOpen] = useState(false) const [addUserOpen, setAddUserOpen] = useState(false)
const [rowSelection, setRowSelection] = useState({}) const [rowSelection, setRowSelection] = useState({})
const [data, setData] = useState(...[tableData]) const [currentPage, setCurrentPage] = useState(1)
const [filteredData, setFilteredData] = useState(data) const [pageSize, setPageSize] = useState(10)
const [globalFilter, setGlobalFilter] = useState('') const [openConfirm, setOpenConfirm] = useState(false)
const [customerId, setCustomerId] = useState('')
const [search, setSearch] = useState('')
// Hooks // Hooks
const { lang: locale } = useParams() const { lang: locale } = useParams()
const { data, isLoading, error, isFetching } = useUsers({
page: currentPage,
limit: pageSize,
search
})
// const { deleteCustomer } = useCustomersMutation()
const users = data?.users ?? []
const totalCount = data?.pagination.total_count ?? 0
const handlePageChange = useCallback((event: unknown, newPage: number) => {
setCurrentPage(newPage)
}, [])
// Handle page size change
const handlePageSizeChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
const newPageSize = parseInt(event.target.value, 10)
setPageSize(newPageSize)
setCurrentPage(1) // Reset to first page
}, [])
// const handleDelete = () => {
// deleteCustomer.mutate(customerId, {
// onSuccess: () => setOpenConfirm(false)
// })
// }
const columns = useMemo<ColumnDef<UsersTypeWithAction, any>[]>( const columns = useMemo<ColumnDef<UsersTypeWithAction, any>[]>(
() => [ () => [
{ {
@ -175,16 +187,15 @@ const UserListTable = ({ tableData }: { tableData?: UsersType[] }) => {
/> />
) )
}, },
columnHelper.accessor('fullName', { columnHelper.accessor('name', {
header: 'User', header: 'User',
cell: ({ row }) => ( cell: ({ row }) => (
<div className='flex items-center gap-4'> <div className='flex items-center gap-4'>
{getAvatar({ avatar: row.original.avatar, fullName: row.original.fullName })} {getAvatar({ avatar: '', fullName: row.original.name })}
<div className='flex flex-col'> <div className='flex flex-col'>
<Typography color='text.primary' className='font-medium'> <Typography color='text.primary' className='font-medium'>
{row.original.fullName} {row.original.name}
</Typography> </Typography>
<Typography variant='body2'>{row.original.username}</Typography>
</div> </div>
</div> </div>
) )
@ -203,44 +214,31 @@ const UserListTable = ({ tableData }: { tableData?: UsersType[] }) => {
</div> </div>
) )
}), }),
columnHelper.accessor('currentPlan', { columnHelper.accessor('email', {
header: 'Plan', header: 'Email',
cell: ({ row }) => ( cell: ({ row }) => <Typography>{row.original.email}</Typography>
<Typography className='capitalize' color='text.primary'>
{row.original.currentPlan}
</Typography>
)
}), }),
columnHelper.accessor('billing', { columnHelper.accessor('is_active', {
header: 'Billing',
cell: ({ row }) => <Typography>{row.original.billing}</Typography>
}),
columnHelper.accessor('status', {
header: 'Status', header: 'Status',
cell: ({ row }) => ( cell: ({ row }) => (
<div className='flex items-center gap-3'> <div className='flex items-center gap-3'>
<Chip <Chip
variant='tonal' variant='tonal'
label={row.original.status} label={row.original.is_active ? 'Active' : 'Inactive'}
size='small' size='small'
color={userStatusObj[row.original.status]} color={row.original.is_active ? 'success' : 'error'}
className='capitalize' className='capitalize'
/> />
</div> </div>
) )
}), }),
columnHelper.accessor('action', { columnHelper.accessor('actions', {
header: 'Action', header: 'Action',
cell: ({ row }) => ( cell: ({ row }) => (
<div className='flex items-center'> <div className='flex items-center'>
<IconButton onClick={() => setData(data?.filter(product => product.id !== row.original.id))}> <IconButton onClick={() => {}}>
<i className='tabler-trash text-textSecondary' /> <i className='tabler-trash text-textSecondary' />
</IconButton> </IconButton>
<IconButton>
<Link href={getLocalizedUrl('/apps/user/view', locale as Locale)} className='flex'>
<i className='tabler-eye text-textSecondary' />
</Link>
</IconButton>
<OptionMenu <OptionMenu
iconButtonProps={{ size: 'medium' }} iconButtonProps={{ size: 'medium' }}
iconClassName='text-textSecondary' iconClassName='text-textSecondary'
@ -263,36 +261,28 @@ const UserListTable = ({ tableData }: { tableData?: UsersType[] }) => {
}) })
], ],
// eslint-disable-next-line react-hooks/exhaustive-deps // eslint-disable-next-line react-hooks/exhaustive-deps
[data, filteredData] [data]
) )
const table = useReactTable({ const table = useReactTable({
data: filteredData as UsersType[], data: users as User[],
columns, columns,
filterFns: { filterFns: {
fuzzy: fuzzyFilter fuzzy: fuzzyFilter
}, },
state: { state: {
rowSelection, rowSelection,
globalFilter
},
initialState: {
pagination: { pagination: {
pageSize: 10 pageIndex: currentPage,
pageSize
} }
}, },
enableRowSelection: true, //enable row selection for all rows 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, onRowSelectionChange: setRowSelection,
getCoreRowModel: getCoreRowModel(), getCoreRowModel: getCoreRowModel(),
onGlobalFilterChange: setGlobalFilter, // Disable client-side pagination since we're handling it server-side
getFilteredRowModel: getFilteredRowModel(), manualPagination: true,
getSortedRowModel: getSortedRowModel(), pageCount: Math.ceil(totalCount / pageSize)
getPaginationRowModel: getPaginationRowModel(),
getFacetedRowModel: getFacetedRowModel(),
getFacetedUniqueValues: getFacetedUniqueValues(),
getFacetedMinMaxValues: getFacetedMinMaxValues()
}) })
const getAvatar = (params: Pick<UsersType, 'avatar' | 'fullName'>) => { const getAvatar = (params: Pick<UsersType, 'avatar' | 'fullName'>) => {
@ -309,25 +299,25 @@ const UserListTable = ({ tableData }: { tableData?: UsersType[] }) => {
<> <>
<Card> <Card>
<CardHeader title='Filters' className='pbe-4' /> <CardHeader title='Filters' className='pbe-4' />
<TableFilters setData={setFilteredData} tableData={data} /> {/* <TableFilters setData={setFilteredData} tableData={data} /> */}
<div className='flex justify-between flex-col items-start md:flex-row md:items-center p-6 border-bs gap-4'> <div className='flex justify-between flex-col items-start md:flex-row md:items-center p-6 border-bs gap-4'>
<DebouncedInput
value={search}
onChange={value => setSearch(value as string)}
placeholder='Search User'
className='max-sm:is-full'
/>
<div className='flex flex-col sm:flex-row max-sm:is-full items-start sm:items-center gap-4'>
<CustomTextField <CustomTextField
select select
value={table.getState().pagination.pageSize} value={pageSize}
onChange={e => table.setPageSize(Number(e.target.value))} onChange={handlePageSizeChange}
className='max-sm:is-full sm:is-[70px]' className='max-sm:is-full sm:is-[70px]'
> >
<MenuItem value='10'>10</MenuItem> <MenuItem value='10'>10</MenuItem>
<MenuItem value='25'>25</MenuItem> <MenuItem value='25'>25</MenuItem>
<MenuItem value='50'>50</MenuItem> <MenuItem value='50'>50</MenuItem>
</CustomTextField> </CustomTextField>
<div className='flex flex-col sm:flex-row max-sm:is-full items-start sm:items-center gap-4'>
<DebouncedInput
value={globalFilter ?? ''}
onChange={value => setGlobalFilter(String(value))}
placeholder='Search User'
className='max-sm:is-full'
/>
<Button <Button
color='secondary' color='secondary'
variant='tonal' variant='tonal'
@ -347,6 +337,9 @@ const UserListTable = ({ tableData }: { tableData?: UsersType[] }) => {
</div> </div>
</div> </div>
<div className='overflow-x-auto'> <div className='overflow-x-auto'>
{isLoading ? (
<Loading />
) : (
<table className={tableStyles.table}> <table className={tableStyles.table}>
<thead> <thead>
{table.getHeaderGroups().map(headerGroup => ( {table.getHeaderGroups().map(headerGroup => (
@ -400,22 +393,48 @@ const UserListTable = ({ tableData }: { tableData?: UsersType[] }) => {
</tbody> </tbody>
)} )}
</table> </table>
)}
{isFetching && !isLoading && (
<Box
position='absolute'
top={0}
left={0}
right={0}
bottom={0}
display='flex'
alignItems='center'
justifyContent='center'
bgcolor='rgba(255,255,255,0.7)'
zIndex={1}
>
<CircularProgress size={24} />
</Box>
)}
</div> </div>
{/* <TablePagination
component={() => <TablePaginationComponent table={table} />} <TablePagination
count={table.getFilteredRowModel().rows.length} component={() => (
rowsPerPage={table.getState().pagination.pageSize} <TablePaginationComponent
page={table.getState().pagination.pageIndex} pageIndex={currentPage}
onPageChange={(_, page) => { pageSize={pageSize}
table.setPageIndex(page) totalCount={totalCount}
}} onPageChange={handlePageChange}
/> */} />
)}
count={totalCount}
rowsPerPage={pageSize}
page={currentPage}
onPageChange={handlePageChange}
onRowsPerPageChange={handlePageSizeChange}
rowsPerPageOptions={[10, 25, 50]}
disabled={isLoading}
/>
</Card> </Card>
<AddUserDrawer <AddUserDrawer
open={addUserOpen} open={addUserOpen}
handleClose={() => setAddUserOpen(!addUserOpen)} handleClose={() => setAddUserOpen(!addUserOpen)}
userData={data}
setData={setData}
/> />
</> </>
) )

View File

@ -6,16 +6,15 @@ import type { UsersType } from '@/types/apps/userTypes'
// Component Imports // Component Imports
import UserListTable from './UserListTable' import UserListTable from './UserListTable'
import UserListCards from './UserListCards'
const UserList = ({ userData }: { userData?: UsersType[] }) => { const UserList = () => {
return ( return (
<Grid container spacing={6}> <Grid container spacing={6}>
<Grid size={{ xs: 12 }}> {/* <Grid size={{ xs: 12 }}>
<UserListCards /> <UserListCards />
</Grid> </Grid> */}
<Grid size={{ xs: 12 }}> <Grid size={{ xs: 12 }}>
<UserListTable tableData={userData} /> <UserListTable />
</Grid> </Grid>
</Grid> </Grid>
) )

View File

@ -175,7 +175,7 @@ const EarningReportsWithTabs = ({ data }: { data: TabType[] }) => {
breakpoint: 1450, breakpoint: 1450,
options: { options: {
plotOptions: { plotOptions: {
bar: { columnWidth: '45%' } bar: { columnWidth: '35%' }
} }
} }
}, },
@ -206,7 +206,7 @@ const EarningReportsWithTabs = ({ data }: { data: TabType[] }) => {
return ( return (
<Card> <Card>
<CardHeader <CardHeader
title='Earning Reports' title='Product Reports'
subheader='Yearly Earnings Overview' subheader='Yearly Earnings Overview'
action={<OptionMenu options={['Last Week', 'Last Month', 'Last Year']} />} action={<OptionMenu options={['Last Week', 'Last Month', 'Last Year']} />}
/> />

View File

@ -0,0 +1,249 @@
'use client'
// React Imports
import type { SyntheticEvent } from 'react'
import { useState } from 'react'
// Next Imports
import dynamic from 'next/dynamic'
// MUI Imports
import TabContext from '@mui/lab/TabContext'
import TabList from '@mui/lab/TabList'
import TabPanel from '@mui/lab/TabPanel'
import Card from '@mui/material/Card'
import CardContent from '@mui/material/CardContent'
import CardHeader from '@mui/material/CardHeader'
import Tab from '@mui/material/Tab'
import Typography from '@mui/material/Typography'
import type { Theme } from '@mui/material/styles'
import { useTheme } from '@mui/material/styles'
// Third Party Imports
import type { ApexOptions } from 'apexcharts'
import classnames from 'classnames'
// Components Imports
import CustomAvatar from '@core/components/mui/Avatar'
import OptionMenu from '@core/components/option-menu'
import Loading from '../../../components/layout/shared/Loading'
import { formatShortCurrency } from '../../../utils/transform'
// Styled Component Imports
const AppReactApexCharts = dynamic(() => import('@/libs/styles/AppReactApexCharts'))
type ApexChartSeries = NonNullable<ApexOptions['series']>
type ApexChartSeriesData = Exclude<ApexChartSeries[0], number>
type TabType = {
type: string
avatarIcon: string
date: any
series: ApexChartSeries
}
const renderTabs = (tabData: TabType[], value: string) => {
return tabData.map((item, index) => (
<Tab
key={index}
value={item.type}
className='mie-4'
label={
<div
className={classnames(
'flex flex-col items-center justify-center gap-2 is-[110px] bs-[100px] border rounded-xl',
item.type === value ? 'border-solid border-[var(--mui-palette-primary-main)]' : 'border-dashed'
)}
>
<CustomAvatar variant='rounded' skin='light' size={38} {...(item.type === value && { color: 'primary' })}>
<i className={classnames('text-[22px]', { 'text-textSecondary': item.type !== value }, item.avatarIcon)} />
</CustomAvatar>
<Typography className='font-medium capitalize' color='text.primary'>
{item.type}
</Typography>
</div>
}
/>
))
}
const renderTabPanels = (tabData: TabType[], theme: Theme, options: ApexOptions, colors: string[]) => {
return tabData.map((item, index) => {
const max = Math.max(...((item.series[0] as ApexChartSeriesData).data as number[]))
const seriesIndex = ((item.series[0] as ApexChartSeriesData).data as number[]).indexOf(max)
const finalColors = colors.map((color, i) => (seriesIndex === i ? 'var(--mui-palette-primary-main)' : color))
return (
<TabPanel key={index} value={item.type} className='!p-0'>
<AppReactApexCharts type='bar' height={360} width='100%' options={{ ...options }} series={item.series} />
</TabPanel>
)
})
}
const MultipleSeries = ({ data }: { data: TabType[] }) => {
// States
const [value, setValue] = useState(data[0].type)
// Hooks
const theme = useTheme()
// Vars
const disabledText = 'var(--mui-palette-text-disabled)'
const handleChange = (event: SyntheticEvent, newValue: string) => {
setValue(newValue)
}
const colors = Array(9).fill('var(--mui-palette-primary-lightOpacity)')
const options: ApexOptions = {
chart: {
parentHeightOffset: 0,
toolbar: { show: false }
},
plotOptions: {
bar: {
horizontal: false,
columnWidth: '55%',
borderRadius: 2,
borderRadiusApplication: 'end'
}
},
legend: { show: false },
tooltip: { enabled: true },
dataLabels: {
enabled: false,
offsetY: -11
// formatter: val => formatShortCurrency(Number(val)),
// style: {
// fontWeight: 500,
// colors: ['var(--mui-palette-text-primary)'],
// fontSize: theme.typography.body1.fontSize as string
// }
},
colors: [
'var(--mui-palette-primary-main)',
'var(--mui-palette-info-main)',
'var(--mui-palette-warning-main)',
'var(--mui-palette-success-main)'
],
states: {
hover: {
filter: { type: 'none' }
},
active: {
filter: { type: 'none' }
}
},
stroke: { width: 5, colors: ['transparent'] },
grid: {
show: true,
padding: {
top: -19,
left: -4,
right: 0,
bottom: -11
}
},
xaxis: {
axisTicks: { show: false },
axisBorder: { color: 'var(--mui-palette-divider)' },
categories: data.find(item => item.type === value)?.date,
tickPlacement: 'between',
labels: {
style: {
colors: disabledText,
fontFamily: theme.typography.fontFamily,
fontSize: theme.typography.body2.fontSize as string
}
}
},
yaxis: {
labels: {
offsetX: -18,
formatter: val => `${formatShortCurrency(Number(val))}`,
style: {
colors: disabledText,
fontFamily: theme.typography.fontFamily,
fontSize: theme.typography.body2.fontSize as string
}
}
},
responsive: [
{
breakpoint: 1450,
options: {
plotOptions: {
bar: { columnWidth: '35%' }
}
}
},
{
breakpoint: 600,
options: {
dataLabels: {
style: {
fontSize: theme.typography.body2.fontSize as string
}
},
plotOptions: {
bar: { columnWidth: '58%' }
}
}
},
{
breakpoint: 500,
options: {
plotOptions: {
bar: { columnWidth: '70%' }
}
}
}
]
}
return (
<Card>
<CardHeader
title='Profit Reports'
subheader='Yearly Earnings Overview'
action={<OptionMenu options={['Last Week', 'Last Month', 'Last Year']} />}
/>
<CardContent>
<TabContext value={value}>
{data.length > 1 && (
<TabList
variant='scrollable'
scrollButtons='auto'
onChange={handleChange}
aria-label='earning report tabs'
className='!border-0 mbe-10'
sx={{
'& .MuiTabs-indicator': { display: 'none !important' },
'& .MuiTab-root': { padding: '0 !important', border: '0 !important' }
}}
>
{renderTabs(data, value)}
<Tab
disabled
value='add'
label={
<div className='flex flex-col items-center justify-center is-[110px] bs-[100px] border border-dashed rounded-xl'>
<CustomAvatar variant='rounded' size={34}>
<i className='tabler-plus text-textSecondary' />
</CustomAvatar>
</div>
}
/>
</TabList>
)}
{renderTabPanels(data, theme, options, colors)}
</TabContext>
</CardContent>
</Card>
)
}
export default MultipleSeries