Update page.tsx

This commit is contained in:
NotII
2025-04-02 00:29:37 +01:00
parent e5eba12cbe
commit a849e39105

View File

@@ -21,8 +21,24 @@ import {
SelectTrigger, SelectTrigger,
SelectValue, SelectValue,
} from "@/components/ui/select"; } from "@/components/ui/select";
import {
Dialog,
DialogContent,
DialogHeader,
DialogTitle,
DialogDescription,
} from "@/components/ui/dialog";
import { Button } from "@/components/ui/button"; import { Button } from "@/components/ui/button";
import { ChevronLeft, ChevronRight, Loader2, Users } from "lucide-react"; import {
ChevronLeft,
ChevronRight,
Loader2,
Users,
ArrowUpDown,
MessageCircle
} from "lucide-react";
import { Badge } from "@/components/ui/badge";
import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
export default function CustomerManagementPage() { export default function CustomerManagementPage() {
const router = useRouter(); const router = useRouter();
@@ -32,12 +48,36 @@ export default function CustomerManagementPage() {
const [totalPages, setTotalPages] = useState(1); const [totalPages, setTotalPages] = useState(1);
const [itemsPerPage, setItemsPerPage] = useState(25); const [itemsPerPage, setItemsPerPage] = useState(25);
const [selectedCustomer, setSelectedCustomer] = useState<CustomerStats | null>(null); const [selectedCustomer, setSelectedCustomer] = useState<CustomerStats | null>(null);
const [sortConfig, setSortConfig] = useState<{
column: "totalOrders" | "totalSpent" | "lastOrderDate";
direction: "asc" | "desc";
}>({ column: "totalSpent", direction: "desc" });
const fetchCustomers = useCallback(async () => { const fetchCustomers = useCallback(async () => {
try { try {
setLoading(true); setLoading(true);
const response = await getCustomers(page, itemsPerPage); const response = await getCustomers(page, itemsPerPage);
setCustomers(response.customers);
// Sort customers based on current sort config
let sortedCustomers = [...response.customers];
sortedCustomers.sort((a, b) => {
if (sortConfig.column === "totalOrders") {
return sortConfig.direction === "asc"
? a.totalOrders - b.totalOrders
: b.totalOrders - a.totalOrders;
} else if (sortConfig.column === "totalSpent") {
return sortConfig.direction === "asc"
? a.totalSpent - b.totalSpent
: b.totalSpent - a.totalSpent;
} else if (sortConfig.column === "lastOrderDate") {
return sortConfig.direction === "asc"
? new Date(a.lastOrderDate).getTime() - new Date(b.lastOrderDate).getTime()
: new Date(b.lastOrderDate).getTime() - new Date(a.lastOrderDate).getTime();
}
return 0;
});
setCustomers(sortedCustomers);
setTotalPages(Math.ceil(response.total / itemsPerPage)); setTotalPages(Math.ceil(response.total / itemsPerPage));
} catch (err) { } catch (err) {
toast.error("Failed to fetch customers"); toast.error("Failed to fetch customers");
@@ -45,7 +85,7 @@ export default function CustomerManagementPage() {
} finally { } finally {
setLoading(false); setLoading(false);
} }
}, [page, itemsPerPage]); }, [page, itemsPerPage, sortConfig]);
useEffect(() => { useEffect(() => {
fetchCustomers(); fetchCustomers();
@@ -71,6 +111,23 @@ export default function CustomerManagementPage() {
setPage(1); // Reset to first page when changing items per page setPage(1); // Reset to first page when changing items per page
}; };
const handleSort = (column: "totalOrders" | "totalSpent" | "lastOrderDate") => {
setSortConfig(prev => ({
column,
direction: prev.column === column && prev.direction === "asc" ? "desc" : "asc"
}));
};
// Format date without time
const formatDate = (dateString: string) => {
const date = new Date(dateString);
return new Intl.DateTimeFormat('en-GB', {
day: '2-digit',
month: 'short',
year: 'numeric'
}).format(date);
};
return ( return (
<Layout> <Layout>
<div className="space-y-6"> <div className="space-y-6">
@@ -81,69 +138,117 @@ export default function CustomerManagementPage() {
</h1> </h1>
</div> </div>
<div className="bg-white dark:bg-gray-800 rounded-lg shadow"> <div className="bg-white dark:bg-gray-800 rounded-lg shadow overflow-hidden">
<div className="p-4 border-b border-gray-200 dark:border-gray-700 flex justify-between items-center"> <div className="p-4 border-b border-gray-200 dark:border-gray-700 flex justify-between items-center">
<div className="flex items-center gap-2"> <div className="flex items-center gap-4">
<div className="text-sm font-medium text-gray-500 dark:text-gray-400">Show:</div> <div className="flex items-center gap-2">
<Select value={itemsPerPage.toString()} onValueChange={handleItemsPerPageChange}> <div className="text-sm font-medium text-gray-500 dark:text-gray-400">Show:</div>
<SelectTrigger className="w-20"> <Select value={itemsPerPage.toString()} onValueChange={handleItemsPerPageChange}>
<SelectValue placeholder="Page Size" /> <SelectTrigger className="w-[70px]">
</SelectTrigger> <SelectValue placeholder="25" />
<SelectContent> </SelectTrigger>
{[5, 10, 25, 50, 100].map(size => ( <SelectContent>
<SelectItem key={size} value={size.toString()}>{size}</SelectItem> {[5, 10, 25, 50, 100].map(size => (
))} <SelectItem key={size} value={size.toString()}>{size}</SelectItem>
</SelectContent> ))}
</Select> </SelectContent>
</Select>
</div>
</div>
<div className="text-sm text-gray-500 dark:text-gray-400">
{loading
? "Loading..."
: `Showing ${customers.length} of ${totalPages * itemsPerPage} customers`}
</div> </div>
</div> </div>
{loading ? ( {loading ? (
<div className="p-8 flex justify-center"> <div className="p-8 flex justify-center">
<Loader2 className="h-8 w-8 animate-spin text-gray-400" /> <Loader2 className="h-8 w-8 animate-spin text-primary" />
</div> </div>
) : customers.length === 0 ? ( ) : customers.length === 0 ? (
<div className="p-8 text-center"> <div className="p-8 text-center">
<p className="text-gray-500 dark:text-gray-400">No customers found</p> <Users className="h-12 w-12 mx-auto text-muted-foreground mb-4" />
<h3 className="text-lg font-medium mb-2">No customers found</h3>
<p className="text-muted-foreground">
Once you have customers placing orders, they will appear here.
</p>
</div> </div>
) : ( ) : (
<Table> <div className="overflow-x-auto">
<TableHeader> <Table>
<TableRow> <TableHeader>
<TableHead className="w-[300px]">Customer</TableHead> <TableRow>
<TableHead>Total Orders</TableHead> <TableHead className="w-[300px]">Customer</TableHead>
<TableHead>Total Spent</TableHead> <TableHead
<TableHead className="w-[300px]">Order Status</TableHead> className="cursor-pointer"
<TableHead>Last Order</TableHead> onClick={() => handleSort("totalOrders")}
</TableRow> >
</TableHeader> <div className="flex items-center">
<TableBody> Total Orders
{customers.map((customer) => ( <ArrowUpDown className="ml-2 h-4 w-4" />
<TableRow
key={customer.userId}
className="cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700"
onClick={() => setSelectedCustomer(customer)}
>
<TableCell>
<div className="font-medium">@{customer.telegramUsername}</div>
<div className="text-sm text-gray-500">ID: {customer.telegramUserId}</div>
</TableCell>
<TableCell>{customer.totalOrders}</TableCell>
<TableCell>{formatCurrency(customer.totalSpent)}</TableCell>
<TableCell>
<div className="text-sm">
Paid: {customer.ordersByStatus.paid} |
Completed: {customer.ordersByStatus.completed} |
Shipped: {customer.ordersByStatus.shipped}
</div> </div>
</TableCell> </TableHead>
<TableCell> <TableHead
{new Date(customer.lastOrderDate).toLocaleDateString()} className="cursor-pointer"
</TableCell> onClick={() => handleSort("totalSpent")}
>
<div className="flex items-center">
Total Spent
<ArrowUpDown className="ml-2 h-4 w-4" />
</div>
</TableHead>
<TableHead>Status Breakdown</TableHead>
<TableHead
className="cursor-pointer"
onClick={() => handleSort("lastOrderDate")}
>
<div className="flex items-center">
Last Order Date
<ArrowUpDown className="ml-2 h-4 w-4" />
</div>
</TableHead>
</TableRow> </TableRow>
))} </TableHeader>
</TableBody> <TableBody>
</Table> {customers.map((customer) => (
<TableRow
key={customer.userId}
className="cursor-pointer hover:bg-gray-50 dark:hover:bg-gray-700"
onClick={() => setSelectedCustomer(customer)}
>
<TableCell>
<div className="font-medium">@{customer.telegramUsername || "Unknown"}</div>
<div className="text-sm text-gray-500">ID: {customer.telegramUserId}</div>
</TableCell>
<TableCell>
<Badge variant="outline">{customer.totalOrders}</Badge>
</TableCell>
<TableCell className="font-medium">
{formatCurrency(customer.totalSpent)}
</TableCell>
<TableCell>
<div className="flex space-x-1 text-xs">
<Badge variant="outline" className="bg-blue-50 text-blue-700 dark:bg-blue-900/20 dark:text-blue-300 dark:hover:bg-blue-900/30">
Paid: {customer.ordersByStatus.paid}
</Badge>
<Badge variant="outline" className="bg-green-50 text-green-700 dark:bg-green-900/20 dark:text-green-300 dark:hover:bg-green-900/30">
Completed: {customer.ordersByStatus.completed}
</Badge>
<Badge variant="outline" className="bg-amber-50 text-amber-700 dark:bg-amber-900/20 dark:text-amber-300 dark:hover:bg-amber-900/30">
Shipped: {customer.ordersByStatus.shipped}
</Badge>
</div>
</TableCell>
<TableCell>
{formatDate(customer.lastOrderDate)}
</TableCell>
</TableRow>
))}
</TableBody>
</Table>
</div>
)} )}
<div className="p-4 border-t border-gray-200 dark:border-gray-700 flex justify-between items-center"> <div className="p-4 border-t border-gray-200 dark:border-gray-700 flex justify-between items-center">
@@ -155,83 +260,127 @@ export default function CustomerManagementPage() {
variant="outline" variant="outline"
size="sm" size="sm"
onClick={() => handlePageChange(Math.max(1, page - 1))} onClick={() => handlePageChange(Math.max(1, page - 1))}
disabled={page === 1} disabled={page === 1 || loading}
> >
<ChevronLeft className="h-4 w-4" /> <ChevronLeft className="h-4 w-4 mr-1" />
<span className="ml-1">Previous</span> Previous
</Button> </Button>
<Button <Button
variant="outline" variant="outline"
size="sm" size="sm"
onClick={() => handlePageChange(Math.min(totalPages, page + 1))} onClick={() => handlePageChange(Math.min(totalPages, page + 1))}
disabled={page === totalPages} disabled={page === totalPages || loading}
> >
<span className="mr-1">Next</span> Next
<ChevronRight className="h-4 w-4" /> <ChevronRight className="h-4 w-4 ml-1" />
</Button> </Button>
</div> </div>
</div> </div>
</div> </div>
{/* Customer Details Modal */} {/* Customer Details Dialog */}
{selectedCustomer && ( <Dialog open={!!selectedCustomer} onOpenChange={(open) => !open && setSelectedCustomer(null)}>
<div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center p-4 z-50"> <DialogContent className="max-w-2xl">
<div className="bg-white dark:bg-gray-800 rounded-lg p-6 max-w-2xl w-full"> <DialogHeader>
<div className="flex justify-between items-center mb-4"> <DialogTitle className="text-xl font-semibold flex items-center">
<h2 className="text-xl font-bold">Customer Details</h2> <Users className="h-5 w-5 mr-2" />
<Button Customer Details
variant="ghost" </DialogTitle>
size="sm" <DialogDescription>
onClick={() => setSelectedCustomer(null)} Details and order statistics for this customer
> </DialogDescription>
</DialogHeader>
</Button>
</div> {selectedCustomer && (
<div className="space-y-4"> <div className="space-y-4">
<div className="grid grid-cols-2 gap-4"> <div className="grid grid-cols-1 md:grid-cols-2 gap-4">
<div> <Card>
<h3 className="font-semibold mb-2">Customer Information</h3> <CardHeader className="pb-2">
<div className="space-y-1"> <CardTitle className="text-md font-medium">Customer Information</CardTitle>
<p>Telegram Username: @{selectedCustomer.telegramUsername}</p> </CardHeader>
<p>Telegram User ID: {selectedCustomer.telegramUserId}</p> <CardContent className="space-y-2">
<p>Chat ID: {selectedCustomer.chatId}</p> <div className="flex justify-between text-sm">
</div> <div className="text-gray-500 dark:text-gray-400">Username:</div>
</div> <div className="font-medium">@{selectedCustomer.telegramUsername || "Unknown"}</div>
<div> </div>
<h3 className="font-semibold mb-2">Order Statistics</h3> <div className="flex justify-between text-sm">
<div className="space-y-1"> <div className="text-gray-500 dark:text-gray-400">Telegram ID:</div>
<p>Total Orders: {selectedCustomer.totalOrders}</p> <div className="font-medium">{selectedCustomer.telegramUserId}</div>
<p>Total Spent: {formatCurrency(selectedCustomer.totalSpent)}</p> </div>
<p>First Order: {new Date(selectedCustomer.firstOrderDate).toLocaleDateString()}</p> <div className="flex justify-between text-sm">
<p>Last Order: {new Date(selectedCustomer.lastOrderDate).toLocaleDateString()}</p> <div className="text-gray-500 dark:text-gray-400">Chat ID:</div>
</div> <div className="font-medium">{selectedCustomer.chatId}</div>
</div> </div>
<div className="flex justify-end pt-2">
<Button
variant="outline"
size="sm"
className="text-xs"
onClick={() => {
window.open(`https://t.me/${selectedCustomer.telegramUsername || selectedCustomer.telegramUserId}`, '_blank');
}}
>
<MessageCircle className="h-3.5 w-3.5 mr-1" />
Open Chat
</Button>
</div>
</CardContent>
</Card>
<Card>
<CardHeader className="pb-2">
<CardTitle className="text-md font-medium">Order Statistics</CardTitle>
</CardHeader>
<CardContent className="space-y-2">
<div className="flex justify-between text-sm">
<div className="text-gray-500 dark:text-gray-400">Total Orders:</div>
<div className="font-medium">{selectedCustomer.totalOrders}</div>
</div>
<div className="flex justify-between text-sm">
<div className="text-gray-500 dark:text-gray-400">Total Spent:</div>
<div className="font-medium">{formatCurrency(selectedCustomer.totalSpent)}</div>
</div>
<div className="flex justify-between text-sm">
<div className="text-gray-500 dark:text-gray-400">First Order:</div>
<div className="font-medium">{formatDate(selectedCustomer.firstOrderDate)}</div>
</div>
<div className="flex justify-between text-sm">
<div className="text-gray-500 dark:text-gray-400">Last Order:</div>
<div className="font-medium">{formatDate(selectedCustomer.lastOrderDate)}</div>
</div>
</CardContent>
</Card>
</div> </div>
<div>
<h3 className="font-semibold mb-2">Order Status Breakdown</h3> <Card>
<div className="grid grid-cols-2 sm:grid-cols-4 gap-2"> <CardHeader className="pb-2">
<div className="bg-blue-50 dark:bg-blue-900/20 p-2 rounded"> <CardTitle className="text-md font-medium">Order Status Breakdown</CardTitle>
<p className="text-sm text-blue-700 dark:text-blue-300">Paid</p> </CardHeader>
<p className="font-medium">{selectedCustomer.ordersByStatus.paid}</p> <CardContent>
<div className="grid grid-cols-2 sm:grid-cols-4 gap-3">
<div className="bg-blue-50 dark:bg-blue-900/20 p-3 rounded-md">
<p className="text-sm text-blue-700 dark:text-blue-300">Paid</p>
<p className="text-xl font-semibold">{selectedCustomer.ordersByStatus.paid}</p>
</div>
<div className="bg-purple-50 dark:bg-purple-900/20 p-3 rounded-md">
<p className="text-sm text-purple-700 dark:text-purple-300">Acknowledged</p>
<p className="text-xl font-semibold">{selectedCustomer.ordersByStatus.acknowledged}</p>
</div>
<div className="bg-amber-50 dark:bg-amber-900/20 p-3 rounded-md">
<p className="text-sm text-amber-700 dark:text-amber-300">Shipped</p>
<p className="text-xl font-semibold">{selectedCustomer.ordersByStatus.shipped}</p>
</div>
<div className="bg-green-50 dark:bg-green-900/20 p-3 rounded-md">
<p className="text-sm text-green-700 dark:text-green-300">Completed</p>
<p className="text-xl font-semibold">{selectedCustomer.ordersByStatus.completed}</p>
</div>
</div> </div>
<div className="bg-green-50 dark:bg-green-900/20 p-2 rounded"> </CardContent>
<p className="text-sm text-green-700 dark:text-green-300">Completed</p> </Card>
<p className="font-medium">{selectedCustomer.ordersByStatus.completed}</p>
</div>
<div className="bg-purple-50 dark:bg-purple-900/20 p-2 rounded">
<p className="text-sm text-purple-700 dark:text-purple-300">Acknowledged</p>
<p className="font-medium">{selectedCustomer.ordersByStatus.acknowledged}</p>
</div>
<div className="bg-amber-50 dark:bg-amber-900/20 p-2 rounded">
<p className="text-sm text-amber-700 dark:text-amber-300">Shipped</p>
<p className="font-medium">{selectedCustomer.ordersByStatus.shipped}</p>
</div>
</div>
</div>
</div> </div>
</div> )}
</div> </DialogContent>
)} </Dialog>
</div> </div>
</Layout> </Layout>
); );