modify users list page
This commit is contained in:
@@ -1,29 +1,36 @@
|
|||||||
import Table from '@/components/Table'
|
import Table from '@/components/Table'
|
||||||
import Filters, { type FilterValues, type FieldType } from '@/components/Filters'
|
import Filters, { type FieldType } from '@/components/Filters'
|
||||||
import { type FC, useState, useMemo, useCallback } from 'react'
|
import { type FC, useMemo } from 'react'
|
||||||
import { useGetUsers } from './hooks/useUsersData'
|
import { useGetUsers } from './hooks/useUsersData'
|
||||||
import type { Customer, CustomerListRow } from '@/pages/customers/types/Types'
|
import { useCustomerFilters } from './hooks/useCustomerFilters'
|
||||||
|
import type { CustomerListRow, UserRestaurant } from '@/pages/customers/types/Types'
|
||||||
import { formatOptionalDate, formatFaNumber } from '@/helpers/func'
|
import { formatOptionalDate, formatFaNumber } from '@/helpers/func'
|
||||||
|
|
||||||
const mapCustomerToRow = (customer: Customer): CustomerListRow => {
|
const mapUserRestaurantToRow = (userRestaurant: UserRestaurant): CustomerListRow => {
|
||||||
const fullName = [customer.firstName, customer.lastName].filter(Boolean).join(' ').trim()
|
const { user, orderCount, totalOrderAmount } = userRestaurant
|
||||||
|
const fullName = [user.firstName, user.lastName].filter(Boolean).join(' ').trim()
|
||||||
return {
|
return {
|
||||||
id: customer.id,
|
id: user.id,
|
||||||
fullName: fullName || 'بدون نام',
|
fullName: fullName || 'بدون نام',
|
||||||
phone: customer.phone || '-',
|
phone: user.phone || '-',
|
||||||
birthDate: formatOptionalDate(customer.birthDate),
|
birthDate: formatOptionalDate(user.birthDate),
|
||||||
marriageDate: formatOptionalDate(customer.marriageDate),
|
marriageDate: formatOptionalDate(user.marriageDate),
|
||||||
wallet: formatFaNumber(customer.wallet),
|
orderCount: formatFaNumber(orderCount),
|
||||||
points: formatFaNumber(customer.points),
|
totalOrderAmount: formatFaNumber(totalOrderAmount),
|
||||||
status: customer.isActive ? 'فعال' : 'غیرفعال',
|
status: user.isActive !== false ? 'فعال' : 'غیرفعال',
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const CustomersList: FC = () => {
|
const CustomersList: FC = () => {
|
||||||
const { data: usersData, isLoading } = useGetUsers()
|
const {
|
||||||
const [filters, setFilters] = useState<FilterValues>({ search: '', status: 'all' })
|
filters,
|
||||||
const [currentPage, setCurrentPage] = useState(1)
|
currentPage,
|
||||||
const limit = 10
|
apiParams,
|
||||||
|
handleFiltersChange,
|
||||||
|
handlePageChange,
|
||||||
|
} = useCustomerFilters()
|
||||||
|
|
||||||
|
const { data: usersData, isLoading } = useGetUsers(apiParams)
|
||||||
|
|
||||||
const filterFields = useMemo<FieldType[]>(() => [
|
const filterFields = useMemo<FieldType[]>(() => [
|
||||||
{
|
{
|
||||||
@@ -31,49 +38,15 @@ const CustomersList: FC = () => {
|
|||||||
name: 'search',
|
name: 'search',
|
||||||
placeholder: 'نام یا شماره تماس',
|
placeholder: 'نام یا شماره تماس',
|
||||||
},
|
},
|
||||||
{
|
|
||||||
type: 'select',
|
|
||||||
name: 'status',
|
|
||||||
placeholder: 'انتخاب وضعیت',
|
|
||||||
defaultValue: 'all',
|
|
||||||
options: [
|
|
||||||
{ label: 'همه وضعیتها', value: 'all' },
|
|
||||||
{ label: 'فعال', value: 'active' },
|
|
||||||
{ label: 'غیرفعال', value: 'inactive' },
|
|
||||||
],
|
|
||||||
},
|
|
||||||
], [])
|
], [])
|
||||||
|
|
||||||
const customers = useMemo(() => usersData?.data ?? [], [usersData])
|
const userRestaurants = usersData?.data ?? []
|
||||||
|
const tableData = useMemo<CustomerListRow[]>(
|
||||||
|
() => userRestaurants.map(mapUserRestaurantToRow),
|
||||||
|
[userRestaurants],
|
||||||
|
)
|
||||||
|
|
||||||
const filteredCustomers = useMemo(() => {
|
const totalPages = usersData?.meta?.totalPages || 1
|
||||||
const searchValue = (filters.search as string || '').toLowerCase().trim()
|
|
||||||
const statusFilter = filters.status || 'all'
|
|
||||||
|
|
||||||
return customers.filter((customer) => {
|
|
||||||
const fullName = [customer.firstName, customer.lastName].filter(Boolean).join(' ').toLowerCase().trim()
|
|
||||||
const matchesSearch =
|
|
||||||
searchValue === '' ||
|
|
||||||
fullName.includes(searchValue) ||
|
|
||||||
(customer.phone || '').includes(searchValue)
|
|
||||||
|
|
||||||
const matchesStatus =
|
|
||||||
statusFilter === 'all' ||
|
|
||||||
(statusFilter === 'active' && customer.isActive) ||
|
|
||||||
(statusFilter === 'inactive' && !customer.isActive)
|
|
||||||
|
|
||||||
return matchesSearch && matchesStatus
|
|
||||||
})
|
|
||||||
}, [customers, filters])
|
|
||||||
|
|
||||||
const totalPages = Math.max(1, Math.ceil(filteredCustomers.length / limit) || 1)
|
|
||||||
|
|
||||||
const paginatedCustomers = useMemo(() => {
|
|
||||||
const startIndex = (currentPage - 1) * limit
|
|
||||||
return filteredCustomers.slice(startIndex, startIndex + limit)
|
|
||||||
}, [filteredCustomers, currentPage, limit])
|
|
||||||
|
|
||||||
const tableData = useMemo<CustomerListRow[]>(() => paginatedCustomers.map(mapCustomerToRow), [paginatedCustomers])
|
|
||||||
|
|
||||||
const columns = useMemo(() => [
|
const columns = useMemo(() => [
|
||||||
{
|
{
|
||||||
@@ -93,12 +66,12 @@ const CustomersList: FC = () => {
|
|||||||
title: 'تاریخ ازدواج',
|
title: 'تاریخ ازدواج',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'wallet',
|
key: 'orderCount',
|
||||||
title: 'کیف پول (تومان)',
|
title: 'تعداد سفارش',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'points',
|
key: 'totalOrderAmount',
|
||||||
title: 'امتیاز',
|
title: 'مجموع سفارش (تومان)',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
key: 'status',
|
key: 'status',
|
||||||
@@ -117,27 +90,10 @@ const CustomersList: FC = () => {
|
|||||||
},
|
},
|
||||||
], [])
|
], [])
|
||||||
|
|
||||||
const handlePageChange = (page: number) => {
|
|
||||||
setCurrentPage(page)
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleFiltersChange = useCallback((newFilters: FilterValues) => {
|
|
||||||
setFilters(newFilters)
|
|
||||||
setCurrentPage(1)
|
|
||||||
}, [])
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className='mt-5'>
|
<div className='mt-5'>
|
||||||
<div className='flex justify-between items-center'>
|
<div className='flex justify-between items-center'>
|
||||||
<h1 className='text-lg font-light'>لیست مشتریان</h1>
|
<h1 className='text-lg font-light'>لیست مشتریان</h1>
|
||||||
{/* <Link to={Pages.customers.add}>
|
|
||||||
<Button className='w-fit px-6'>
|
|
||||||
<div className='flex gap-2 items-center'>
|
|
||||||
<Add color='#fff' size={20} />
|
|
||||||
<span>افزودن مشتری</span>
|
|
||||||
</div>
|
|
||||||
</Button>
|
|
||||||
</Link> */}
|
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div className='mt-8'>
|
<div className='mt-8'>
|
||||||
|
|||||||
@@ -0,0 +1,43 @@
|
|||||||
|
import { useState, useMemo } from "react";
|
||||||
|
import type { FilterValues } from "@/components/Filters";
|
||||||
|
import type { GetUsersParams } from "../types/Types";
|
||||||
|
|
||||||
|
const DEFAULT_PAGE = 1;
|
||||||
|
const DEFAULT_LIMIT = 10;
|
||||||
|
|
||||||
|
export const useCustomerFilters = () => {
|
||||||
|
const [filters, setFilters] = useState<FilterValues>({});
|
||||||
|
const [currentPage, setCurrentPage] = useState(DEFAULT_PAGE);
|
||||||
|
const [limit] = useState(DEFAULT_LIMIT);
|
||||||
|
|
||||||
|
const apiParams = useMemo<GetUsersParams>(() => {
|
||||||
|
const params: GetUsersParams = {
|
||||||
|
page: currentPage,
|
||||||
|
limit,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (filters.search) {
|
||||||
|
params.search = filters.search as string;
|
||||||
|
}
|
||||||
|
|
||||||
|
return params;
|
||||||
|
}, [filters, currentPage, limit]);
|
||||||
|
|
||||||
|
const handleFiltersChange = (newFilters: FilterValues) => {
|
||||||
|
setFilters(newFilters);
|
||||||
|
setCurrentPage(DEFAULT_PAGE);
|
||||||
|
};
|
||||||
|
|
||||||
|
const handlePageChange = (page: number) => {
|
||||||
|
setCurrentPage(page);
|
||||||
|
};
|
||||||
|
|
||||||
|
return {
|
||||||
|
filters,
|
||||||
|
currentPage,
|
||||||
|
apiParams,
|
||||||
|
handleFiltersChange,
|
||||||
|
handlePageChange,
|
||||||
|
limit,
|
||||||
|
};
|
||||||
|
};
|
||||||
@@ -1,9 +1,10 @@
|
|||||||
import * as api from "../service/UsersService";
|
import * as api from "../service/UsersService";
|
||||||
import { useQuery } from "@tanstack/react-query";
|
import { useQuery } from "@tanstack/react-query";
|
||||||
|
import type { GetUsersParams } from "../types/Types";
|
||||||
|
|
||||||
export const useGetUsers = () => {
|
export const useGetUsers = (params?: GetUsersParams) => {
|
||||||
return useQuery({
|
return useQuery({
|
||||||
queryKey: ["users"],
|
queryKey: ["users", params],
|
||||||
queryFn: api.getUsers,
|
queryFn: () => api.getUsers(params),
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,7 +1,12 @@
|
|||||||
import axios from "@/config/axios";
|
import axios from "@/config/axios";
|
||||||
import type { GetCustomersResponse } from "@/pages/customers/types/Types";
|
import type {
|
||||||
|
GetCustomersResponse,
|
||||||
|
GetUsersParams,
|
||||||
|
} from "@/pages/customers/types/Types";
|
||||||
|
|
||||||
export const getUsers = async () => {
|
export const getUsers = async (params?: GetUsersParams) => {
|
||||||
const { data } = await axios.get<GetCustomersResponse>(`/admin/users`);
|
const { data } = await axios.get<GetCustomersResponse>(`/admin/users`, {
|
||||||
|
params,
|
||||||
|
});
|
||||||
return data;
|
return data;
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,32 +1,57 @@
|
|||||||
import type { IResponse } from "@/types/response.types";
|
import type { IResponse } from "@/types/response.types";
|
||||||
import type { RowDataType } from "@/components/types/TableTypes";
|
import type { RowDataType } from "@/components/types/TableTypes";
|
||||||
|
|
||||||
export interface Customer {
|
export type User = {
|
||||||
id: string;
|
id: string;
|
||||||
createdAt: string;
|
createdAt: string;
|
||||||
updatedAt: string;
|
updatedAt: string;
|
||||||
deletedAt: string | null;
|
deletedAt: string | null;
|
||||||
restaurant: string;
|
|
||||||
firstName: string;
|
firstName: string;
|
||||||
lastName: string;
|
lastName?: string;
|
||||||
phone: string;
|
phone: string;
|
||||||
birthDate: string;
|
birthDate?: string;
|
||||||
marriageDate: string;
|
marriageDate?: string;
|
||||||
referrer: string | null;
|
referrer?: string | null;
|
||||||
isActive: boolean;
|
isActive?: boolean;
|
||||||
gender: boolean;
|
gender?: boolean;
|
||||||
wallet: number;
|
};
|
||||||
points: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type GetCustomersResponse = IResponse<Customer[]>;
|
export type UserRestaurant = {
|
||||||
|
id: string;
|
||||||
|
createdAt: string;
|
||||||
|
updatedAt: string;
|
||||||
|
deletedAt: string | null;
|
||||||
|
user: User;
|
||||||
|
restaurant: string;
|
||||||
|
orderCount: number;
|
||||||
|
totalOrderAmount: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PaginationMeta = {
|
||||||
|
total: number;
|
||||||
|
page: number;
|
||||||
|
limit: number;
|
||||||
|
totalPages: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GetUsersParams = {
|
||||||
|
page?: number;
|
||||||
|
limit?: number;
|
||||||
|
search?: string;
|
||||||
|
orderBy?: string;
|
||||||
|
order?: "asc" | "desc";
|
||||||
|
};
|
||||||
|
|
||||||
|
export type GetCustomersResponse = IResponse<UserRestaurant[]> & {
|
||||||
|
meta: PaginationMeta;
|
||||||
|
};
|
||||||
|
|
||||||
export type CustomerListRow = RowDataType & {
|
export type CustomerListRow = RowDataType & {
|
||||||
fullName: string;
|
fullName: string;
|
||||||
phone: string;
|
phone: string;
|
||||||
birthDate: string;
|
birthDate: string;
|
||||||
marriageDate: string;
|
marriageDate: string;
|
||||||
wallet: string;
|
orderCount: string;
|
||||||
points: string;
|
totalOrderAmount: string;
|
||||||
status: string;
|
status: string;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user