Files
hats-app/app/(dashboard)/task/component/client/Client.tsx
T
Ichsan Kurniawan faa22e93e0 Align task pages with access permissions
Use the shared page-permission helper across task routes and gate client creation controls on CREATE_CLIENT. Remove an unused profile permission import.
2026-11-17 21:46:07 +07:00

129 lines
4.6 KiB
TypeScript

"use client";
import { useState } from 'react';
import { useRouter, useSearchParams } from 'next/navigation';
import { Users } from 'lucide-react';
import { Button } from '@/components/ui/button';
import { IClient } from '../../types';
import dynamic from 'next/dynamic';
const ClientGrid = dynamic(() => import('./ClientGrid'), {
ssr: false,
loading: () => (
<div className="grid gap-6 md:grid-cols-2 lg:grid-cols-3">
{[1, 2, 3, 4, 5, 6].map((i) => (
<div key={i} className="h-80 w-full animate-pulse rounded-2xl border border-border/80 bg-card p-6" />
))}
</div>
),
});
const ClientList = dynamic(() => import('./ClientList'), {
ssr: false,
loading: () => (
<div className="flex flex-col gap-6">
{[1, 2, 3, 4].map((i) => (
<div key={i} className="h-64 w-full animate-pulse rounded-2xl border border-border/80 bg-card p-6" />
))}
</div>
),
});
const FormClient = dynamic(() => import('./FormClient'), { ssr: false });
const ConfirmDeleteWithInput = dynamic(
() => import('@/components/modal/ConfirmDeleteWithInput').then((mod) => mod.ConfirmDeleteWithInput),
{ ssr: false }
);
interface ClientProps {
clients: IClient[];
initialLayout?: 'grid' | 'list';
actions: string[];
}
export default function Client({ clients, actions, initialLayout = 'grid' }: ClientProps) {
const router = useRouter();
const searchParams = useSearchParams();
const layout = searchParams?.get('layout') === 'list' ? 'list' : initialLayout;
const [action, setAction] = useState<'EDIT' | 'DELETE' | undefined>(undefined);
const [selectedData, setSelectedData] = useState<IClient>();
const handleClose = () => {
setAction(undefined);
setSelectedData(undefined);
};
const handleEdit = (client: IClient) => {
setSelectedData(client);
setAction('EDIT');
};
const handleDelete = (client: IClient) => {
setSelectedData(client);
setAction('DELETE');
};
return (
<>
{clients.length === 0 ? (
<div className="py-12 text-center border-2 border-dashed rounded-xl">
<Users className="w-12 h-12 mx-auto text-muted-foreground mb-4 opacity-20" />
<div className="text-center">
<h3 className="text-xl font-semibold">No Client Found</h3>
<p className="text-muted-foreground max-w-sm mx-auto mt-2">
Try to change the filter or create one to get started.
</p>
{actions?.includes('CREATE_CLIENT') &&
<Button
variant="outline"
className="mt-4"
onClick={() => {
setAction('EDIT');
setSelectedData(undefined);
}}
>
Create New Client
</Button>
}
</div>
</div>
) : null}
{clients.length > 0 && layout === 'list' ?
<ClientList clients={clients} onEdit={handleEdit} onDelete={handleDelete} actions={actions} />
: null}
{clients.length > 0 && layout === 'grid' ?
<ClientGrid clients={clients} onEdit={handleEdit} onDelete={handleDelete} actions={actions} />
: null}
{action === 'EDIT' && (
<FormClient
data={selectedData}
onCancel={handleClose}
onSuccess={() => {
router.refresh();
handleClose();
}}
/>
)}
{action === 'DELETE' && selectedData ?
<ConfirmDeleteWithInput
endpoint={`/client/${selectedData.id}`}
onCancel={handleClose}
onSuccess={() => {
router.refresh();
handleClose();
}}
title={selectedData.name}
description={`This action cannot be undone. This will permanently delete and remove client ${selectedData.name} from our servers.`}
titleButton="I understand the consequences, delete this client"
/>
: null}
</>
);
}