316 lines
14 KiB
TypeScript
316 lines
14 KiB
TypeScript
import { useState } from 'react'
|
|
import { useParams, useNavigate } from 'react-router-dom'
|
|
import { useTranslation } from 'react-i18next'
|
|
import { Pencil, Trash2, XCircle, CheckCircle2 } from 'lucide-react'
|
|
import { Button } from '@/components/ui/button'
|
|
import { Skeleton } from '@/components/ui/skeleton'
|
|
import { ConfirmDialog } from '@/components/confirmDialog'
|
|
import { BentoGrid, BentoCard, InfoRow, AuditLog, CollapsibleSection, EmptyState } from '@/components/viewPageComponents'
|
|
import { DataTable, type ColumnDef } from '@/components/dataTable'
|
|
import { DataTableToolbar } from '@/components/dataTableToolbar'
|
|
import { useClient, useDeleteClient } from '@/features/clients/hooks/useClients'
|
|
import type { ClientRole, ClientBranch, ClientAuditLog } from '@/features/clients/api/clientsApi'
|
|
|
|
// ─── ActiveIndicator ──────────────────────────────────────────────────────────
|
|
|
|
function ActiveIndicator({ active }: { active: boolean }) {
|
|
return active ? (
|
|
<CheckCircle2 size={18} className="text-emerald-500" />
|
|
) : (
|
|
<XCircle size={18} className="text-destructive" />
|
|
);
|
|
}
|
|
|
|
// ─── Page ─────────────────────────────────────────────────────────────────────
|
|
|
|
export default function ClientDetailPage() {
|
|
const { id } = useParams<{ id: string }>()
|
|
const clientId = Number(id)
|
|
const { t } = useTranslation()
|
|
const navigate = useNavigate()
|
|
const deleteMutation = useDeleteClient()
|
|
|
|
const { data: client, isLoading } = useClient(clientId)
|
|
|
|
const [deleteOpen, setDeleteOpen] = useState(false)
|
|
const [roleSearch, setRoleSearch] = useState('')
|
|
const [permSearch, setPermSearch] = useState('')
|
|
const [branchSearch, setBranchSearch] = useState('')
|
|
const [loanSearch, setLoanSearch] = useState('')
|
|
const [cardSearch, setCardSearch] = useState('')
|
|
|
|
// ── Role columns ──────────────────────────────────────────────────────────
|
|
|
|
const roleColumns: ColumnDef<ClientRole>[] = [
|
|
{
|
|
accessorKey: 'id',
|
|
header: t('common.id', 'ID'),
|
|
cell: ({ row }) => <span className="text-primary font-semibold text-sm">{row.original.id}</span>,
|
|
size: 80,
|
|
},
|
|
{
|
|
accessorKey: 'code',
|
|
header: t('roles.fields.code', 'KOD'),
|
|
cell: ({ row }) => <span className="text-sm">{row.original.code}</span>,
|
|
},
|
|
{
|
|
accessorKey: 'name',
|
|
header: t('roles.fields.name', 'ADY'),
|
|
cell: ({ row }) => <span className="text-sm">{row.original.name}</span>,
|
|
},
|
|
{
|
|
accessorKey: 'guardName',
|
|
header: t('roles.fields.guardName', 'GUARD NAME'),
|
|
cell: ({ row }) => <span className="text-sm">{row.original.guardName}</span>,
|
|
},
|
|
{
|
|
id: 'actions',
|
|
header: '',
|
|
cell: () => (
|
|
<div className="flex items-center gap-1 justify-end">
|
|
<Button size="icon" variant="ghost" className="h-7 w-7 text-muted-foreground hover:text-destructive">
|
|
<Trash2 size={13} />
|
|
</Button>
|
|
</div>
|
|
),
|
|
size: 60,
|
|
},
|
|
]
|
|
|
|
// ── Branch columns ────────────────────────────────────────────────────────
|
|
|
|
const branchColumns: ColumnDef<ClientBranch>[] = [
|
|
{
|
|
accessorKey: 'id',
|
|
header: t('common.id', 'ID'),
|
|
cell: ({ row }) => <span className="text-primary font-semibold text-sm">{row.original.id}</span>,
|
|
size: 60,
|
|
},
|
|
{
|
|
accessorKey: 'name',
|
|
header: t('branches.fields.name', 'ADY'),
|
|
cell: ({ row }) => <span className="text-sm">{row.original.name}</span>,
|
|
},
|
|
{
|
|
accessorKey: 'region',
|
|
header: t('branches.fields.region', 'WELAÝAT'),
|
|
cell: ({ row }) => <span className="text-sm">{row.original.region}</span>,
|
|
},
|
|
{
|
|
accessorKey: 'district',
|
|
header: t('branches.fields.district', 'ETRAP'),
|
|
cell: ({ row }) => <span className="text-sm">{row.original.district}</span>,
|
|
},
|
|
{
|
|
accessorKey: 'uniqueCode',
|
|
header: t('branches.fields.uniqueCode', 'UNIKAL BELGI'),
|
|
cell: ({ row }) => <span className="text-sm">{row.original.uniqueCode}</span>,
|
|
},
|
|
{
|
|
id: 'actions',
|
|
header: '',
|
|
cell: () => (
|
|
<div className="flex items-center gap-1 justify-end">
|
|
<Button size="icon" variant="ghost" className="h-7 w-7 text-muted-foreground hover:text-destructive">
|
|
<Trash2 size={13} />
|
|
</Button>
|
|
</div>
|
|
),
|
|
size: 60,
|
|
},
|
|
]
|
|
|
|
// ── Loading ───────────────────────────────────────────────────────────────
|
|
|
|
if (isLoading) {
|
|
return (
|
|
<div className="flex flex-col gap-4">
|
|
<Skeleton className="h-7 w-64" />
|
|
<div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
|
|
{Array.from({ length: 4 }).map((_, i) => (
|
|
<div key={i} className="bg-card border border-border rounded-xl p-4 flex flex-col gap-3">
|
|
<Skeleton className="h-3 w-24 mb-1" />
|
|
{Array.from({ length: 3 }).map((_, j) => (
|
|
<Skeleton key={j} className="h-4 w-full" />
|
|
))}
|
|
</div>
|
|
))}
|
|
</div>
|
|
</div>
|
|
)
|
|
}
|
|
|
|
if (!client) return null
|
|
|
|
const auditLogs = (client.auditLogs ?? []).map((log: ClientAuditLog) => ({
|
|
id: String(log.id),
|
|
action: log.actionName,
|
|
by: log.performedBy,
|
|
target: log.target,
|
|
status: log.status,
|
|
date: log.createdAt,
|
|
}))
|
|
|
|
const filteredRoles = (client.roles ?? []).filter((r) =>
|
|
r.name.toLowerCase().includes(roleSearch.toLowerCase()) ||
|
|
r.code.toLowerCase().includes(roleSearch.toLowerCase()),
|
|
)
|
|
const filteredBranches = (client.branches ?? []).filter((b) =>
|
|
b.name.toLowerCase().includes(branchSearch.toLowerCase()),
|
|
)
|
|
|
|
return (
|
|
<div className=" flex flex-col gap-6">
|
|
|
|
{/* ── Page header ──────────────────────────────────────────────────── */}
|
|
<div className="flex items-center justify-between">
|
|
<h1 className="text-2xl font-semibold text-foreground">
|
|
{t('clients.detailTitle', 'Müşderi giňişleýin')}: {client.name}
|
|
</h1>
|
|
<div className="flex items-center gap-2">
|
|
<Button
|
|
size="icon" variant="ghost"
|
|
className="h-8 w-8 text-muted-foreground hover:text-destructive"
|
|
onClick={() => setDeleteOpen(true)}
|
|
>
|
|
<Trash2 size={15} />
|
|
</Button>
|
|
<Button
|
|
size="icon" variant="ghost"
|
|
className="h-8 w-8 text-muted-foreground hover:text-foreground"
|
|
onClick={() => navigate(`/clients/${clientId}/edit`)}
|
|
>
|
|
<Pencil size={15} />
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
|
|
{/* ── Row 1: Esasy maglumatlar + Kontakt ──────────────────────────── */}
|
|
<BentoGrid cols={2}>
|
|
<BentoCard title={t('clients.sections.basic', 'Esasy maglumatlar')}>
|
|
<InfoRow label={t('common.id', 'ID')} value={client.id} />
|
|
<InfoRow
|
|
label={t('clients.fields.username', 'Ulanyjy ady')}
|
|
value={client.username}
|
|
/>
|
|
<InfoRow
|
|
label={t('clients.fields.name', 'Ady')}
|
|
value={client.name}
|
|
/>
|
|
<InfoRow label={t('clients.fields.isActive', 'Işjeň')}>
|
|
<ActiveIndicator active={client.isActive} />
|
|
</InfoRow>
|
|
</BentoCard>
|
|
|
|
<BentoCard title={t('clients.sections.contact', 'Kontakt')}>
|
|
<InfoRow
|
|
label={t('clients.fields.phone', 'Telefon')}
|
|
value={client.phone ?? '—'}
|
|
/>
|
|
<InfoRow
|
|
label={t('clients.fields.email', 'E-poçta')}
|
|
value={client.email ?? '—'}
|
|
/>
|
|
</BentoCard>
|
|
</BentoGrid>
|
|
|
|
{/* ── Rollar ───────────────────────────────────────────────────────── */}
|
|
<CollapsibleSection title={t('clients.sections.roles', 'Rollar')}>
|
|
<DataTableToolbar
|
|
searchValue={roleSearch}
|
|
onSearchChange={setRoleSearch}
|
|
searchPlaceholder={t('common.search', 'Gözlemek')}
|
|
columns={[]} columnVisibility={{}} onColumnVisibilityChange={() => {}}
|
|
columnOrder={[]} onColumnOrderChange={() => {}}
|
|
actionLabel={t('clients.addRole', 'Rol birikdiriň')}
|
|
onAction={() => {}}
|
|
/>
|
|
{filteredRoles.length > 0 ? (
|
|
<DataTable columns={roleColumns} data={filteredRoles} getRowId={(r) => String(r.id)} enableRowSelection />
|
|
) : (
|
|
<EmptyState label={t('clients.noRoles', 'Berlen kriterýalara Rol gabat gelmedi.')} />
|
|
)}
|
|
</CollapsibleSection>
|
|
|
|
{/* ── Rugsatlar ────────────────────────────────────────────────────── */}
|
|
<CollapsibleSection title={t('clients.sections.permissions', 'Rugsatlar')}>
|
|
<DataTableToolbar
|
|
searchValue={permSearch}
|
|
onSearchChange={setPermSearch}
|
|
searchPlaceholder={t('common.search', 'Gözlemek')}
|
|
columns={[]} columnVisibility={{}} onColumnVisibilityChange={() => {}}
|
|
columnOrder={[]} onColumnOrderChange={() => {}}
|
|
actionLabel={t('clients.addPermission', 'Rugsat birikdiriň')}
|
|
onAction={() => {}}
|
|
/>
|
|
{(client.permissions ?? []).length === 0 && (
|
|
<EmptyState label={t('clients.noPermissions', 'Berlen kriterýalara Rugsat gabat gelmedi.')} />
|
|
)}
|
|
</CollapsibleSection>
|
|
|
|
{/* ── Şahamçalar ───────────────────────────────────────────────────── */}
|
|
<CollapsibleSection title={t('clients.sections.branches', 'Şahamçalar')}>
|
|
<DataTableToolbar
|
|
searchValue={branchSearch}
|
|
onSearchChange={setBranchSearch}
|
|
searchPlaceholder={t('common.search', 'Gözlemek')}
|
|
columns={[]} columnVisibility={{}} onColumnVisibilityChange={() => {}}
|
|
columnOrder={[]} onColumnOrderChange={() => {}}
|
|
actionLabel={t('clients.addBranch', 'Şahamça birikdiriň')}
|
|
onAction={() => {}}
|
|
/>
|
|
{filteredBranches.length > 0 ? (
|
|
<DataTable columns={branchColumns} data={filteredBranches} getRowId={(r) => String(r.id)} enableRowSelection />
|
|
) : (
|
|
<EmptyState label={t('clients.noBranches', 'Berlen kriterýalara Şahamça gabat gelmedi.')} />
|
|
)}
|
|
</CollapsibleSection>
|
|
|
|
{/* ── Karz sargyt ──────────────────────────────────────────────────── */}
|
|
<CollapsibleSection title={t('clients.sections.loanOrders', 'Karz sargyt')}>
|
|
<DataTableToolbar
|
|
searchValue={loanSearch}
|
|
onSearchChange={setLoanSearch}
|
|
searchPlaceholder={t('common.search', 'Gözlemek')}
|
|
columns={[]} columnVisibility={{}} onColumnVisibilityChange={() => {}}
|
|
columnOrder={[]} onColumnOrderChange={() => {}}
|
|
actionLabel={t('clients.addLoanOrder', 'Karz sargyt dörediň')}
|
|
onAction={() => {}}
|
|
/>
|
|
{(client.loanOrders ?? []).length === 0 && (
|
|
<EmptyState label={t('clients.noLoanOrders', 'Berlen kriterýalara Karz sargyt gabat gelmedi.')} />
|
|
)}
|
|
</CollapsibleSection>
|
|
|
|
{/* ── Kart sargyt ──────────────────────────────────────────────────── */}
|
|
<CollapsibleSection title={t('clients.sections.cardOrders', 'Kart sargyt')}>
|
|
<DataTableToolbar
|
|
searchValue={cardSearch}
|
|
onSearchChange={setCardSearch}
|
|
searchPlaceholder={t('common.search', 'Gözlemek')}
|
|
columns={[]} columnVisibility={{}} onColumnVisibilityChange={() => {}}
|
|
columnOrder={[]} onColumnOrderChange={() => {}}
|
|
actionLabel={t('clients.addCardOrder', 'Kart sargyt dörediň')}
|
|
onAction={() => {}}
|
|
/>
|
|
{(client.cardOrders ?? []).length === 0 && (
|
|
<EmptyState label={t('clients.noCardOrders', 'Berlen kriterýalara Kart sargyt gabat gelmedi.')} />
|
|
)}
|
|
</CollapsibleSection>
|
|
|
|
{/* ── Audit log ────────────────────────────────────────────────────── */}
|
|
<AuditLog logs={auditLogs} />
|
|
|
|
<ConfirmDialog
|
|
open={deleteOpen}
|
|
onOpenChange={setDeleteOpen}
|
|
title={t('clients.deleteTitle', 'Müşderini pozmak')}
|
|
description={t('clients.deleteConfirm', '{{name}} müşderisini pozmak isleýärsiňizmi?', { name: client.name })}
|
|
confirmLabel={t('common.delete', 'Poz')}
|
|
onConfirm={() => deleteMutation.mutate(clientId, { onSuccess: () => navigate('/clients') })}
|
|
isLoading={deleteMutation.isPending}
|
|
/>
|
|
|
|
</div>
|
|
)
|
|
} |