|
|
@@ -24,7 +24,9 @@ import { useGlassModal } from "@/components/glass-modal";
|
|
|
import { useConfirm } from "@/components/confirm-dialog";
|
|
|
import { CustomerForm } from "@/features/customers/Components/CustomerForm";
|
|
|
import { ImportCustomersDialog } from "@/features/customers/Components/ImportCustomersDialog";
|
|
|
-import { deleteCustomer } from "@/features/customers/Actions/customerActions";
|
|
|
+import { Checkbox } from "@/components/ui/checkbox";
|
|
|
+import { deleteCustomer, deleteCustomers } from "@/features/customers/Actions/customerActions";
|
|
|
+import { toast } from "sonner";
|
|
|
import {
|
|
|
Loader2,
|
|
|
MoreVertical,
|
|
|
@@ -72,6 +74,8 @@ export function CustomersClient({
|
|
|
const [showForm, setShowForm] = useState(false);
|
|
|
const [showImport, setShowImport] = useState(false);
|
|
|
const [editCustomer, setEditCustomer] = useState<Customer | null>(null);
|
|
|
+ const [selected, setSelected] = useState<Set<string>>(new Set());
|
|
|
+ const [isDeleting, setIsDeleting] = useState(false);
|
|
|
const modal = useGlassModal();
|
|
|
const confirm = useConfirm();
|
|
|
|
|
|
@@ -103,6 +107,23 @@ export function CustomersClient({
|
|
|
[navigate, searchInput]
|
|
|
);
|
|
|
|
|
|
+ const toggleSelect = (id: string) => {
|
|
|
+ setSelected((prev) => {
|
|
|
+ const next = new Set(prev);
|
|
|
+ if (next.has(id)) next.delete(id);
|
|
|
+ else next.add(id);
|
|
|
+ return next;
|
|
|
+ });
|
|
|
+ };
|
|
|
+
|
|
|
+ const toggleSelectAll = () => {
|
|
|
+ if (selected.size === data.customers.length) {
|
|
|
+ setSelected(new Set());
|
|
|
+ } else {
|
|
|
+ setSelected(new Set(data.customers.map((c) => c.id)));
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
const handleDelete = async (id: string, name: string) => {
|
|
|
const ok = await confirm({
|
|
|
title: t("deleteTitle"),
|
|
|
@@ -119,32 +140,76 @@ export function CustomersClient({
|
|
|
}
|
|
|
};
|
|
|
|
|
|
+ const handleBatchDelete = async () => {
|
|
|
+ if (selected.size === 0) return;
|
|
|
+ const ok = await confirm({
|
|
|
+ title: t("batchDeleteTitle"),
|
|
|
+ description: t("batchDeleteDescription", { count: selected.size }),
|
|
|
+ confirmLabel: tc("buttons.delete"),
|
|
|
+ destructive: true,
|
|
|
+ });
|
|
|
+ if (!ok) return;
|
|
|
+ setIsDeleting(true);
|
|
|
+ const result = await deleteCustomers(Array.from(selected));
|
|
|
+ if (result.success) {
|
|
|
+ toast.success(t("batchDeleteSuccess", { count: result.data?.deleted ?? selected.size }));
|
|
|
+ setSelected(new Set());
|
|
|
+ router.refresh();
|
|
|
+ } else {
|
|
|
+ modal.open("error", tc("errors.error"), result.error || t("deleteError"));
|
|
|
+ }
|
|
|
+ setIsDeleting(false);
|
|
|
+ };
|
|
|
+
|
|
|
return (
|
|
|
<div className="space-y-4">
|
|
|
{/* Toolbar */}
|
|
|
<div className="flex items-center justify-between gap-3">
|
|
|
- <div className="flex flex-1 items-center gap-2">
|
|
|
- <form onSubmit={handleSearch} className="relative flex-1 sm:max-w-sm">
|
|
|
- <Search className="absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
|
|
- <Input
|
|
|
- placeholder={t("searchPlaceholder")}
|
|
|
- value={searchInput}
|
|
|
- onChange={(e) => setSearchInput(e.target.value)}
|
|
|
- className="pl-9"
|
|
|
- />
|
|
|
- </form>
|
|
|
- {isPending && <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />}
|
|
|
- </div>
|
|
|
- <div className="flex items-center gap-2">
|
|
|
- <Button size="sm" variant="outline" onClick={() => setShowImport(true)}>
|
|
|
- <Upload className="mr-1 h-3.5 w-3.5" />
|
|
|
- {t("importCustomers")}
|
|
|
- </Button>
|
|
|
- <Button size="sm" onClick={() => setShowForm(true)}>
|
|
|
- <Plus className="mr-1 h-3.5 w-3.5" />
|
|
|
- {t("addCustomer")}
|
|
|
- </Button>
|
|
|
- </div>
|
|
|
+ {selected.size > 0 ? (
|
|
|
+ <>
|
|
|
+ <div className="flex items-center gap-2 text-sm text-muted-foreground">
|
|
|
+ <span>{t("selectedCount", { count: selected.size })}</span>
|
|
|
+ <Button variant="ghost" size="sm" onClick={() => setSelected(new Set())}>
|
|
|
+ {t("clearSelection")}
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ <Button
|
|
|
+ size="sm"
|
|
|
+ variant="destructive"
|
|
|
+ onClick={handleBatchDelete}
|
|
|
+ disabled={isDeleting}
|
|
|
+ >
|
|
|
+ {isDeleting && <Loader2 className="mr-1 h-3.5 w-3.5 animate-spin" />}
|
|
|
+ <Trash2 className="mr-1 h-3.5 w-3.5" />
|
|
|
+ {t("batchDelete", { count: selected.size })}
|
|
|
+ </Button>
|
|
|
+ </>
|
|
|
+ ) : (
|
|
|
+ <>
|
|
|
+ <div className="flex flex-1 items-center gap-2">
|
|
|
+ <form onSubmit={handleSearch} className="relative flex-1 sm:max-w-sm">
|
|
|
+ <Search className="absolute left-2.5 top-1/2 h-4 w-4 -translate-y-1/2 text-muted-foreground" />
|
|
|
+ <Input
|
|
|
+ placeholder={t("searchPlaceholder")}
|
|
|
+ value={searchInput}
|
|
|
+ onChange={(e) => setSearchInput(e.target.value)}
|
|
|
+ className="pl-9"
|
|
|
+ />
|
|
|
+ </form>
|
|
|
+ {isPending && <Loader2 className="h-4 w-4 animate-spin text-muted-foreground" />}
|
|
|
+ </div>
|
|
|
+ <div className="flex items-center gap-2">
|
|
|
+ <Button size="sm" variant="outline" onClick={() => setShowImport(true)}>
|
|
|
+ <Upload className="mr-1 h-3.5 w-3.5" />
|
|
|
+ {t("importCustomers")}
|
|
|
+ </Button>
|
|
|
+ <Button size="sm" onClick={() => setShowForm(true)}>
|
|
|
+ <Plus className="mr-1 h-3.5 w-3.5" />
|
|
|
+ {t("addCustomer")}
|
|
|
+ </Button>
|
|
|
+ </div>
|
|
|
+ </>
|
|
|
+ )}
|
|
|
</div>
|
|
|
|
|
|
{/* Table - only this scrolls */}
|
|
|
@@ -152,6 +217,19 @@ export function CustomersClient({
|
|
|
<Table>
|
|
|
<TableHeader className="sticky top-0 z-10 bg-background">
|
|
|
<TableRow>
|
|
|
+ <TableHead className="w-[40px]">
|
|
|
+ <Checkbox
|
|
|
+ checked={
|
|
|
+ data.customers.length > 0 && selected.size === data.customers.length
|
|
|
+ ? true
|
|
|
+ : selected.size > 0
|
|
|
+ ? "indeterminate"
|
|
|
+ : false
|
|
|
+ }
|
|
|
+ onCheckedChange={toggleSelectAll}
|
|
|
+ onClick={(e) => e.stopPropagation()}
|
|
|
+ />
|
|
|
+ </TableHead>
|
|
|
<TableHead>{t("table.name")}</TableHead>
|
|
|
<TableHead className="hidden sm:table-cell">{t("table.company")}</TableHead>
|
|
|
<TableHead className="hidden md:table-cell">{t("table.phone")}</TableHead>
|
|
|
@@ -163,7 +241,7 @@ export function CustomersClient({
|
|
|
<TableBody>
|
|
|
{data.customers.length === 0 ? (
|
|
|
<TableRow>
|
|
|
- <TableCell colSpan={6} className="h-32 text-center text-muted-foreground">
|
|
|
+ <TableCell colSpan={7} className="h-32 text-center text-muted-foreground">
|
|
|
{search ? t("emptySearch") : t("empty")}
|
|
|
</TableCell>
|
|
|
</TableRow>
|
|
|
@@ -171,9 +249,16 @@ export function CustomersClient({
|
|
|
data.customers.map((c) => (
|
|
|
<TableRow
|
|
|
key={c.id}
|
|
|
- className="cursor-pointer"
|
|
|
+ className={`cursor-pointer ${selected.has(c.id) ? "bg-muted/50" : ""}`}
|
|
|
onClick={() => router.push(`/customers/${c.id}`)}
|
|
|
>
|
|
|
+ <TableCell className="w-[40px]">
|
|
|
+ <Checkbox
|
|
|
+ checked={selected.has(c.id)}
|
|
|
+ onCheckedChange={() => toggleSelect(c.id)}
|
|
|
+ onClick={(e) => e.stopPropagation()}
|
|
|
+ />
|
|
|
+ </TableCell>
|
|
|
<TableCell>
|
|
|
<div className="flex items-center gap-2">
|
|
|
<Users className="h-4 w-4 text-muted-foreground shrink-0" />
|