feat(clients): refresh clients page layout and toast feedback

This commit is contained in:
2026-04-28 21:53:26 +03:30
parent 2b5ee2abf1
commit 36a8c0e24c
6 changed files with 207 additions and 166 deletions

View File

@@ -1,5 +1,6 @@
import { useState } from "react"; import { useState } from "react";
import { createClient } from "../api/clients"; import { toast } from "sonner";
import { createClient } from "../api/clients";
import { useTranslation } from "../hooks/useTranslation"; import { useTranslation } from "../hooks/useTranslation";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
import { Input } from "./ui/input"; import { Input } from "./ui/input";
@@ -22,18 +23,20 @@ export default function CreateClientModal({ isOpen, onClose, onSuccess, workspac
const handleSubmit = async () => { const handleSubmit = async () => {
if (!name.trim()) return; if (!name.trim()) return;
setIsLoading(true); setIsLoading(true);
try { try {
await createClient(workspaceId, { name, notes }); await createClient(workspaceId, { name, notes });
onSuccess(); toast.success(t.clients.createSuccess);
setName(""); onSuccess();
setNotes(""); setName("");
onClose(); setNotes("");
} catch (error) { onClose();
console.error(t.clients.errors.createFailed, error); } catch (error) {
} finally { console.error(t.clients.errors.createFailed, error);
setIsLoading(false); toast.error(t.clients.errors.createFailed);
} } finally {
}; setIsLoading(false);
}
};
const footer = ( const footer = (
<> <>
@@ -72,4 +75,4 @@ export default function CreateClientModal({ isOpen, onClose, onSuccess, workspac
</div> </div>
</Modal> </Modal>
); );
} }

View File

@@ -1,5 +1,6 @@
import { useState } from "react"; import { useState } from "react";
import { type Client } from "../types/client"; import { toast } from "sonner";
import { type Client } from "../types/client";
import { deleteClient } from "../api/clients"; import { deleteClient } from "../api/clients";
import { useTranslation } from "../hooks/useTranslation"; import { useTranslation } from "../hooks/useTranslation";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
@@ -19,16 +20,18 @@ export default function DeleteClientModal({ isOpen, onClose, onSuccess, client }
const handleDelete = async () => { const handleDelete = async () => {
if (!client) return; if (!client) return;
setIsLoading(true); setIsLoading(true);
try { try {
await deleteClient(client.id); await deleteClient(client.id);
onSuccess(); toast.success(t.clients.deleteSuccess);
onClose(); onSuccess();
} catch (error) { onClose();
console.error(t.clients.errors.deleteFailed, error); } catch (error) {
} finally { console.error(t.clients.errors.deleteFailed, error);
setIsLoading(false); toast.error(t.clients.errors.deleteFailed);
} } finally {
}; setIsLoading(false);
}
};
const footer = ( const footer = (
<> <>

View File

@@ -1,5 +1,6 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { type Client } from "../types/client"; import { toast } from "sonner";
import { type Client } from "../types/client";
import { updateClient } from "../api/clients"; import { updateClient } from "../api/clients";
import { useTranslation } from "../hooks/useTranslation"; import { useTranslation } from "../hooks/useTranslation";
import { Button } from "./ui/button"; import { Button } from "./ui/button";
@@ -30,16 +31,18 @@ export default function EditClientModal({ isOpen, onClose, onSuccess, client }:
const handleSubmit = async () => { const handleSubmit = async () => {
if (!client || !name.trim()) return; if (!client || !name.trim()) return;
setIsLoading(true); setIsLoading(true);
try { try {
await updateClient(client.id, { name, notes }); await updateClient(client.id, { name, notes });
onSuccess(); toast.success(t.clients.updateSuccess);
onClose(); onSuccess();
} catch (error) { onClose();
console.error(t.clients.errors.updateFailed, error); } catch (error) {
} finally { console.error(t.clients.errors.updateFailed, error);
setIsLoading(false); toast.error(t.clients.errors.updateFailed);
} } finally {
}; setIsLoading(false);
}
};
const footer = ( const footer = (
<> <>
@@ -78,4 +81,4 @@ export default function EditClientModal({ isOpen, onClose, onSuccess, client }:
</div> </div>
</Modal> </Modal>
); );
} }

View File

@@ -244,10 +244,13 @@ export const en = {
editClient: "Edit Client", editClient: "Edit Client",
deleteConfirmTitle: "Delete Client", deleteConfirmTitle: "Delete Client",
deleteConfirmMessage: (name: string) => `Are you sure you want to delete ${name}?`, deleteConfirmMessage: (name: string) => `Are you sure you want to delete ${name}?`,
delete: "Delete", delete: "Delete",
saveChanges: "Save Changes", saveChanges: "Save Changes",
errors: { createSuccess: "Client created successfully.",
createFailed: "Failed to create client", updateSuccess: "Client updated successfully.",
deleteSuccess: "Client deleted successfully.",
errors: {
createFailed: "Failed to create client",
fetchFailed: "Failed to fetch clients", fetchFailed: "Failed to fetch clients",
updateFailed: "Failed to update client", updateFailed: "Failed to update client",
deleteFailed: "Failed to delete client", deleteFailed: "Failed to delete client",

View File

@@ -241,10 +241,13 @@ export const fa = {
editClient: "ویرایش مشتری", editClient: "ویرایش مشتری",
deleteConfirmTitle: "حذف مشتری", deleteConfirmTitle: "حذف مشتری",
deleteConfirmMessage: (name: string) => `آیا از حذف ${name} اطمینان دارید؟`, deleteConfirmMessage: (name: string) => `آیا از حذف ${name} اطمینان دارید؟`,
delete: "حذف", delete: "حذف",
saveChanges: "ذخیره تغییرات", saveChanges: "ذخیره تغییرات",
errors: { createSuccess: "مشتری با موفقیت ایجاد شد.",
createFailed: "خطا در ایجاد مشتری", updateSuccess: "مشتری با موفقیت به‌روزرسانی شد.",
deleteSuccess: "مشتری با موفقیت حذف شد.",
errors: {
createFailed: "خطا در ایجاد مشتری",
fetchFailed: "خطا در دریافت لیست مشتری‌ها", fetchFailed: "خطا در دریافت لیست مشتری‌ها",
updateFailed: "خطا در ویرایش مشتری", updateFailed: "خطا در ویرایش مشتری",
deleteFailed: "خطا در حذف مشتری", deleteFailed: "خطا در حذف مشتری",

View File

@@ -1,5 +1,6 @@
import { useEffect, useState } from "react" import { useEffect, useState } from "react"
import { Plus, Building2, Loader2, Pencil, Trash2 } from "lucide-react" import { Plus, Building2, Loader2, Pencil, Trash2 } from "lucide-react"
import { toast } from "sonner"
import { useWorkspace } from "../context/WorkspaceContext" import { useWorkspace } from "../context/WorkspaceContext"
import { useAppContext } from "../context/AppContext" import { useAppContext } from "../context/AppContext"
import { useTranslation } from "../hooks/useTranslation" import { useTranslation } from "../hooks/useTranslation"
@@ -13,11 +14,11 @@ import { type Client } from "../types/client"
import { getClients } from "../api/clients" import { getClients } from "../api/clients"
import CreateClientModal from "../components/CreateClientModal" import CreateClientModal from "../components/CreateClientModal"
import EditClientModal from "../components/EditClientModal" import EditClientModal from "../components/EditClientModal"
import DeleteClientModal from "../components/DeleteClientModal" import DeleteClientModal from "../components/DeleteClientModal"
import FilterBar from "../components/FilterBar" import FilterBar from "../components/FilterBar"
import { Button } from "../components/ui/button" import { Button } from "../components/ui/button"
import { Card } from "../components/ui/card" import { Card, CardContent, CardTitle } from "../components/ui/card"
import { Pagination } from "../components/Pagination" import { Pagination } from "../components/Pagination"
export default function Clients() { export default function Clients() {
const { activeWorkspace } = useWorkspace() const { activeWorkspace } = useWorkspace()
@@ -82,11 +83,12 @@ export default function Clients() {
setClients(items) setClients(items)
setTotalItems(count) setTotalItems(count)
} catch (error) { } catch (error) {
console.error(t.clients.errors.fetchFailed, error) console.error(t.clients.errors.fetchFailed, error)
setClients([]) toast.error(t.clients.errors.fetchFailed)
} finally { setClients([])
setIsLoading(false) } finally {
setIsLoading(false)
} }
} }
@@ -107,122 +109,146 @@ export default function Clients() {
fetchClientsList() fetchClientsList()
}, [activeWorkspace?.id, debouncedSearch, ordering, currentPage, limit]) }, [activeWorkspace?.id, debouncedSearch, ordering, currentPage, limit])
if (!activeWorkspace) { if (!activeWorkspace) {
return ( return (
<div className="p-6 text-center text-slate-500"> <div className="mx-auto max-w-7xl p-4 md:p-6">
{t.clients.selectWorkspace} <div className="rounded-3xl border border-slate-200 bg-white p-6 text-slate-600 shadow-sm dark:border-slate-800 dark:bg-slate-900 dark:text-slate-300">
</div> {t.clients.selectWorkspace}
) </div>
} </div>
)
return ( }
<div className="flex flex-col p-6 min-h-[calc(100vh-73px)] bg-slate-50 dark:bg-slate-900">
<div className="flex justify-between items-center mb-8 gap-4"> return (
<div> <div className="mx-auto max-w-7xl space-y-5 p-4 md:p-6">
<h1 className="text-2xl font-bold text-slate-900 dark:text-white">{t.clients.title}</h1> <div className="rounded-3xl border border-slate-200 bg-white p-5 shadow-sm dark:border-slate-800 dark:bg-slate-900 sm:p-6">
<p className="text-slate-500 dark:text-slate-400 text-sm mt-1"> <div className="flex items-start justify-between gap-4">
{t.clients.description(activeWorkspace.name)} <div>
</p> <h1 className="text-2xl font-bold text-slate-900 dark:text-white">{t.clients.title}</h1>
</div> <p className="mt-1 text-sm text-slate-500 dark:text-slate-400">
{t.clients.description(activeWorkspace.name)}
{canCreateClient && ( </p>
<Button </div>
onClick={() => setIsCreateModalOpen(true)}
size="icon" {canCreateClient && (
className="shadow-sm shrink-0" <Button
title={t.clients.addClient} onClick={() => setIsCreateModalOpen(true)}
> size="icon"
<Plus className="w-5 h-5" /> className="shrink-0 shadow-sm"
</Button> title={t.clients.addClient}
)} >
</div> <Plus className="h-5 w-5" />
</Button>
<FilterBar )}
searchQuery={searchQuery} </div>
setSearchQuery={setSearchQuery} </div>
ordering={ordering}
setOrdering={setOrdering} <div className="rounded-3xl border border-slate-200 bg-white p-4 shadow-sm dark:border-slate-800 dark:bg-slate-900 sm:p-5">
orderingOptions={orderingOptions} <FilterBar
searchPlaceholder={t.clients.searchPlaceholder} searchQuery={searchQuery}
/> setSearchQuery={setSearchQuery}
ordering={ordering}
<Card className="overflow-hidden dark:bg-slate-800 dark:border-slate-700 mb-6"> setOrdering={setOrdering}
<div className="p-0"> orderingOptions={orderingOptions}
{isLoading ? ( searchPlaceholder={t.clients.searchPlaceholder}
<div className="flex justify-center items-center p-12 text-slate-500"> />
<Loader2 className="w-8 h-8 animate-spin" /> </div>
</div>
) : clients.length === 0 ? ( {isLoading ? (
<div className="text-center p-12"> <div className="rounded-3xl border border-slate-200 bg-white p-12 shadow-sm dark:border-slate-800 dark:bg-slate-900">
<Building2 className="w-12 h-12 text-slate-300 dark:text-slate-700 mx-auto mb-3" /> <div className="flex items-center justify-center text-slate-500 dark:text-slate-400">
<h3 className="text-lg font-medium text-slate-900 dark:text-white">{t.clients.noClients}</h3> <Loader2 className="h-8 w-8 animate-spin" />
<p className="text-slate-500 dark:text-slate-400 mt-1"> </div>
{searchQuery ? t.clients.noClientsSearch : t.clients.noClientsAdd} </div>
</p> ) : (
</div> <div className="space-y-6">
) : ( {clients.length === 0 ? (
<ul className="divide-y divide-slate-200 dark:divide-slate-800"> <div className="rounded-3xl border-2 border-dashed border-slate-200 bg-white p-12 text-center shadow-sm dark:border-slate-800 dark:bg-slate-900">
<Building2 className="mx-auto mb-3 h-12 w-12 text-slate-300 dark:text-slate-700" />
<h3 className="text-lg font-medium text-slate-900 dark:text-white">{t.clients.noClients}</h3>
<p className="mt-1 text-slate-500 dark:text-slate-400">
{searchQuery ? t.clients.noClientsSearch : t.clients.noClientsAdd}
</p>
</div>
) : (
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 2xl:grid-cols-3">
{clients.map((client) => { {clients.map((client) => {
const canDeleteClient = canDeleteWorkspaceResource({ const canDeleteClient = canDeleteWorkspaceResource({
workspaceRole, workspaceRole,
currentUserId: user?.id, currentUserId: user?.id,
createdById: client.created_by?.id, createdById: client.created_by?.id,
}) })
return ( return (
<li key={client.id} className="p-4 hover:bg-slate-50 dark:hover:bg-slate-800/50 transition-colors flex items-center justify-between gap-4"> <Card key={client.id} className="shadow-sm dark:border-slate-700 dark:bg-slate-800">
<div className="flex-1 min-w-0"> <CardContent className="flex h-full flex-col gap-4 p-5">
<h4 className="font-medium text-slate-900 dark:text-white truncate">{client.name}</h4> <div className="flex items-start justify-between gap-3">
{client.notes && ( <div className="flex min-w-0 items-center gap-3">
<p className="text-sm text-slate-500 dark:text-slate-400 mt-1 truncate"> <div className="flex h-10 w-10 shrink-0 items-center justify-center rounded-xl bg-slate-100 text-sm font-semibold text-slate-700 dark:bg-slate-700 dark:text-slate-200">
{client.notes} {client.name.trim().charAt(0).toUpperCase() || "C"}
</p> </div>
)} <div className="min-w-0">
</div> <CardTitle className="truncate text-base text-slate-900 dark:text-white">{client.name}</CardTitle>
</div>
{(canEditClient || canDeleteClient) && ( </div>
<div className="flex items-center gap-1 shrink-0">
{canEditClient && ( {(canEditClient || canDeleteClient) && (
<Button <div className="flex shrink-0 items-center gap-1">
variant="ghost" {canEditClient && (
size="icon" <Button
onClick={() => setEditClient(client)} variant="ghost"
className="h-8 w-8 text-slate-400 hover:text-blue-600 hover:bg-blue-50 dark:hover:text-blue-400 dark:hover:bg-blue-900/20" size="icon"
> onClick={() => setEditClient(client)}
<Pencil className="w-4 h-4" /> className="h-8 w-8 text-slate-400 hover:bg-blue-50 hover:text-blue-600 dark:hover:bg-blue-900/20 dark:hover:text-blue-400"
</Button> title={t.actions?.edit || "Edit"}
)} >
{canDeleteClient && ( <Pencil className="h-4 w-4" />
<Button </Button>
variant="ghost" )}
size="icon" {canDeleteClient && (
onClick={() => setDeleteClient(client)} <Button
className="h-8 w-8 text-slate-400 hover:text-red-600 hover:bg-red-50 dark:hover:text-red-400 dark:hover:bg-red-900/20" variant="ghost"
> size="icon"
<Trash2 className="w-4 h-4" /> onClick={() => setDeleteClient(client)}
</Button> className="h-8 w-8 text-slate-400 hover:bg-red-50 hover:text-red-600 dark:hover:bg-red-900/20 dark:hover:text-red-400"
)} title={t.actions?.delete || "Delete"}
</div> >
)} <Trash2 className="h-4 w-4" />
</li> </Button>
)}
</div>
)}
</div>
<div className="space-y-3">
<p className="min-h-[3.75rem] text-sm leading-6 text-slate-600 line-clamp-3 dark:text-slate-300">
{client.notes || t.workspace?.noDescription || "No description"}
</p>
<div className="text-xs font-medium uppercase tracking-[0.14em] text-slate-400 dark:text-slate-500">
{t.clients.addedOn}: {formatDate(client.created_at)}
</div>
</div>
</CardContent>
</Card>
) )
})} })}
</ul> </div>
)} )}
</div>
</Card> {clients.length > 0 && (
<Pagination
{!isLoading && clients.length > 0 && ( currentPage={currentPage}
<Pagination totalCount={totalItems}
currentPage={currentPage} limit={limit}
totalCount={totalItems} onPageChange={setCurrentPage}
limit={limit} onLimitChange={setLimit}
onPageChange={setCurrentPage} />
onLimitChange={setLimit} )}
/> </div>
)} )}
{canCreateClient && ( {canCreateClient && (
<CreateClientModal <CreateClientModal
isOpen={isCreateModalOpen} isOpen={isCreateModalOpen}
onClose={() => setIsCreateModalOpen(false)} onClose={() => setIsCreateModalOpen(false)}
onSuccess={fetchClientsList} onSuccess={fetchClientsList}