2026-04-12 23:01:47 +07:00
|
|
|
import { createFileRoute, useNavigate } from '@tanstack/react-router';
|
|
|
|
|
import * as React from 'react';
|
|
|
|
|
import { Search, Pencil, Trash2, Plus } from 'lucide-react';
|
2026-04-10 14:42:24 +07:00
|
|
|
import {
|
2026-04-12 23:01:47 +07:00
|
|
|
AlertDialog,
|
|
|
|
|
AlertDialogAction,
|
|
|
|
|
AlertDialogCancel,
|
|
|
|
|
AlertDialogContent,
|
|
|
|
|
AlertDialogDescription,
|
|
|
|
|
AlertDialogFooter,
|
|
|
|
|
AlertDialogHeader,
|
|
|
|
|
AlertDialogTitle,
|
|
|
|
|
Button,
|
|
|
|
|
Card,
|
|
|
|
|
CardContent,
|
|
|
|
|
CardHeader,
|
|
|
|
|
Checkbox,
|
|
|
|
|
Input,
|
|
|
|
|
} from '@imphnen-frontend-service/ui/atoms';
|
|
|
|
|
import {
|
|
|
|
|
DataTable,
|
|
|
|
|
BackofficeWrapper,
|
|
|
|
|
} from '@imphnen-frontend-service/ui/organisms';
|
2026-04-10 14:42:24 +07:00
|
|
|
import {
|
|
|
|
|
ColumnDef,
|
|
|
|
|
getCoreRowModel,
|
|
|
|
|
getPaginationRowModel,
|
|
|
|
|
PaginationState,
|
|
|
|
|
RowSelectionState,
|
|
|
|
|
useReactTable,
|
2026-04-12 23:01:47 +07:00
|
|
|
} from '@tanstack/react-table';
|
2026-04-10 14:42:24 +07:00
|
|
|
import {
|
|
|
|
|
useTestimonialList,
|
|
|
|
|
useDeleteTestimonial,
|
|
|
|
|
TTestimonialsListItem,
|
2026-04-12 23:01:47 +07:00
|
|
|
} from '@imphnen-frontend-service/service';
|
|
|
|
|
import { toast } from 'sonner';
|
2026-04-10 14:42:24 +07:00
|
|
|
|
2026-04-11 17:04:14 +07:00
|
|
|
export const Route = createFileRoute('/_authenticated/cms-testimonials')({
|
|
|
|
|
component: CmsTestimonialsPage,
|
2026-04-12 23:01:47 +07:00
|
|
|
});
|
2026-04-11 17:04:14 +07:00
|
|
|
|
|
|
|
|
function CmsTestimonialsPage() {
|
2026-04-12 23:01:47 +07:00
|
|
|
const navigate = useNavigate();
|
|
|
|
|
const [search, setSearch] = React.useState('');
|
|
|
|
|
const [deleteId, setDeleteId] = React.useState<string | null>(null);
|
2026-04-10 14:42:24 +07:00
|
|
|
const [pagination, setPagination] = React.useState<PaginationState>({
|
|
|
|
|
pageIndex: 0,
|
2026-04-12 23:01:47 +07:00
|
|
|
pageSize: 10,
|
|
|
|
|
});
|
|
|
|
|
const [rowSelection, setRowSelection] =
|
|
|
|
|
React.useState<RowSelectionState>({});
|
2026-04-10 14:42:24 +07:00
|
|
|
|
|
|
|
|
const { data: testimonialsData, isLoading } = useTestimonialList({
|
|
|
|
|
search,
|
|
|
|
|
page: pagination.pageIndex + 1,
|
|
|
|
|
per_page: pagination.pageSize,
|
2026-04-12 23:01:47 +07:00
|
|
|
});
|
|
|
|
|
const deleteTestimonial = useDeleteTestimonial();
|
2026-04-10 14:42:24 +07:00
|
|
|
|
2026-04-12 23:01:47 +07:00
|
|
|
const testimonials: TTestimonialsListItem[] = testimonialsData?.data ?? [];
|
|
|
|
|
const totalItems = testimonialsData?.meta?.total ?? testimonials.length;
|
2026-04-10 14:42:24 +07:00
|
|
|
|
2026-04-11 22:02:57 +07:00
|
|
|
const handleDelete = async (id: string) => {
|
|
|
|
|
try {
|
2026-04-12 23:01:47 +07:00
|
|
|
await deleteTestimonial.mutateAsync(id);
|
|
|
|
|
toast.success('Data testimonial berhasil dihapus');
|
|
|
|
|
setDeleteId(null);
|
2026-04-11 22:02:57 +07:00
|
|
|
} catch (error) {
|
2026-04-12 23:01:47 +07:00
|
|
|
console.log(error);
|
|
|
|
|
toast.error('Data testimonial gagal dihapus');
|
2026-04-10 14:42:24 +07:00
|
|
|
}
|
2026-04-12 23:01:47 +07:00
|
|
|
};
|
2026-04-10 14:42:24 +07:00
|
|
|
|
|
|
|
|
const columns: ColumnDef<TTestimonialsListItem>[] = [
|
|
|
|
|
{
|
|
|
|
|
id: 'select',
|
|
|
|
|
header: ({ table }) => (
|
2026-04-12 23:01:47 +07:00
|
|
|
<Checkbox
|
|
|
|
|
checked={
|
|
|
|
|
table.getIsAllRowsSelected()
|
|
|
|
|
? true
|
|
|
|
|
: table.getIsSomeRowsSelected()
|
|
|
|
|
? 'indeterminate'
|
|
|
|
|
: false
|
|
|
|
|
}
|
|
|
|
|
onCheckedChange={(v) =>
|
|
|
|
|
table.toggleAllRowsSelected(!!v && v !== 'indeterminate')
|
|
|
|
|
}
|
2026-04-10 14:42:24 +07:00
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
cell: ({ row }) => (
|
2026-04-12 23:01:47 +07:00
|
|
|
<Checkbox
|
2026-04-10 14:42:24 +07:00
|
|
|
checked={row.getIsSelected()}
|
2026-04-12 23:01:47 +07:00
|
|
|
onCheckedChange={(v) => row.toggleSelected(!!v)}
|
2026-04-10 14:42:24 +07:00
|
|
|
/>
|
|
|
|
|
),
|
|
|
|
|
},
|
2026-04-12 23:01:47 +07:00
|
|
|
{ header: 'User', accessorKey: 'user_fullname' },
|
|
|
|
|
{ header: 'Role', accessorKey: 'role' },
|
2026-04-10 14:42:24 +07:00
|
|
|
{
|
|
|
|
|
header: 'Content',
|
|
|
|
|
accessorKey: 'content',
|
|
|
|
|
cell: ({ row }) => {
|
2026-04-12 23:01:47 +07:00
|
|
|
const content = row.original.content;
|
|
|
|
|
return content.length > 80
|
|
|
|
|
? `${content.substring(0, 80)}…`
|
|
|
|
|
: content;
|
2026-04-10 14:42:24 +07:00
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
header: 'Created At',
|
|
|
|
|
accessorKey: 'created_at',
|
|
|
|
|
cell: ({ row }) =>
|
|
|
|
|
new Date(row.original.created_at).toLocaleDateString('id-ID', {
|
|
|
|
|
day: 'numeric',
|
|
|
|
|
month: 'short',
|
|
|
|
|
year: 'numeric',
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
header: 'Action',
|
|
|
|
|
cell: ({ row }) => (
|
2026-04-12 23:01:47 +07:00
|
|
|
<div className="flex items-center gap-2">
|
2026-04-10 14:42:24 +07:00
|
|
|
<Button
|
2026-04-12 23:01:47 +07:00
|
|
|
variant="secondary"
|
2026-04-10 14:42:24 +07:00
|
|
|
size="sm"
|
|
|
|
|
onClick={(e) => {
|
2026-04-12 23:01:47 +07:00
|
|
|
e.stopPropagation();
|
|
|
|
|
navigate({
|
|
|
|
|
to: '/cms-testimonials/$id',
|
|
|
|
|
params: { id: row.original.id },
|
|
|
|
|
});
|
2026-04-10 14:42:24 +07:00
|
|
|
}}
|
|
|
|
|
>
|
2026-04-12 23:01:47 +07:00
|
|
|
<Pencil className="size-3.5" />
|
|
|
|
|
Update
|
|
|
|
|
</Button>
|
|
|
|
|
<Button
|
|
|
|
|
variant="danger"
|
|
|
|
|
size="sm"
|
|
|
|
|
onClick={(e) => {
|
|
|
|
|
e.stopPropagation();
|
|
|
|
|
setDeleteId(row.original.id);
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Trash2 className="size-3.5" />
|
|
|
|
|
Delete
|
2026-04-10 14:42:24 +07:00
|
|
|
</Button>
|
|
|
|
|
</div>
|
|
|
|
|
),
|
|
|
|
|
},
|
2026-04-12 23:01:47 +07:00
|
|
|
];
|
2026-04-10 14:42:24 +07:00
|
|
|
|
|
|
|
|
const table = useReactTable({
|
|
|
|
|
data: testimonials,
|
|
|
|
|
columns,
|
2026-04-12 23:01:47 +07:00
|
|
|
state: { pagination, rowSelection },
|
2026-04-10 14:42:24 +07:00
|
|
|
enableRowSelection: true,
|
|
|
|
|
onRowSelectionChange: setRowSelection,
|
|
|
|
|
getCoreRowModel: getCoreRowModel(),
|
|
|
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
|
|
|
onPaginationChange: setPagination,
|
|
|
|
|
pageCount: Math.ceil(totalItems / pagination.pageSize),
|
|
|
|
|
manualPagination: true,
|
2026-04-12 23:01:47 +07:00
|
|
|
});
|
2026-04-10 14:42:24 +07:00
|
|
|
|
|
|
|
|
return (
|
2026-04-12 23:01:47 +07:00
|
|
|
<BackofficeWrapper
|
|
|
|
|
title="CMS Testimonials"
|
|
|
|
|
description="Kelola testimonial pengguna"
|
|
|
|
|
>
|
|
|
|
|
<Card>
|
|
|
|
|
<CardHeader>
|
|
|
|
|
<div className="flex flex-col gap-3 sm:flex-row sm:items-center sm:justify-between">
|
|
|
|
|
<div className="relative w-full sm:max-w-sm">
|
|
|
|
|
<Search className="absolute left-3 top-1/2 size-4 -translate-y-1/2 text-muted-foreground" />
|
2026-04-10 14:42:24 +07:00
|
|
|
<Input
|
2026-04-12 23:01:47 +07:00
|
|
|
placeholder="Cari nama user…"
|
|
|
|
|
className="pl-9"
|
2026-04-10 14:42:24 +07:00
|
|
|
value={search}
|
|
|
|
|
onChange={(e) => setSearch(e.target.value)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
2026-04-12 23:01:47 +07:00
|
|
|
<Button
|
|
|
|
|
onClick={() => navigate({ to: '/cms-testimonials/create' })}
|
|
|
|
|
size="md"
|
|
|
|
|
>
|
|
|
|
|
<Plus className="size-4" />
|
|
|
|
|
Tambah Testimonial
|
|
|
|
|
</Button>
|
2026-04-10 14:42:24 +07:00
|
|
|
</div>
|
2026-04-12 23:01:47 +07:00
|
|
|
</CardHeader>
|
|
|
|
|
<CardContent>
|
2026-04-10 14:42:24 +07:00
|
|
|
{isLoading ? (
|
2026-04-12 23:01:47 +07:00
|
|
|
<div className="py-10 text-center text-sm text-muted-foreground">
|
|
|
|
|
Memuat data…
|
|
|
|
|
</div>
|
2026-04-10 14:42:24 +07:00
|
|
|
) : (
|
|
|
|
|
<DataTable
|
|
|
|
|
data={testimonials}
|
|
|
|
|
columns={columns}
|
|
|
|
|
table={table}
|
2026-04-12 23:01:47 +07:00
|
|
|
manualPagination
|
|
|
|
|
pageCount={Math.ceil(totalItems / pagination.pageSize)}
|
|
|
|
|
currentPage={pagination.pageIndex + 1}
|
|
|
|
|
onPageChange={(p) =>
|
|
|
|
|
setPagination((prev) => ({ ...prev, pageIndex: p - 1 }))
|
|
|
|
|
}
|
2026-04-10 14:42:24 +07:00
|
|
|
/>
|
|
|
|
|
)}
|
2026-04-12 23:01:47 +07:00
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
|
|
|
|
|
<AlertDialog
|
|
|
|
|
open={!!deleteId}
|
|
|
|
|
onOpenChange={(o) => !o && setDeleteId(null)}
|
|
|
|
|
>
|
|
|
|
|
<AlertDialogContent>
|
|
|
|
|
<AlertDialogHeader>
|
|
|
|
|
<AlertDialogTitle>Hapus testimonial ini?</AlertDialogTitle>
|
|
|
|
|
<AlertDialogDescription>
|
|
|
|
|
Tindakan ini tidak dapat dibatalkan. Testimonial akan dihapus
|
|
|
|
|
permanen.
|
|
|
|
|
</AlertDialogDescription>
|
|
|
|
|
</AlertDialogHeader>
|
|
|
|
|
<AlertDialogFooter>
|
|
|
|
|
<AlertDialogCancel>Batal</AlertDialogCancel>
|
|
|
|
|
<AlertDialogAction
|
|
|
|
|
onClick={() => deleteId && handleDelete(deleteId)}
|
|
|
|
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
|
|
|
|
>
|
|
|
|
|
Hapus
|
|
|
|
|
</AlertDialogAction>
|
|
|
|
|
</AlertDialogFooter>
|
|
|
|
|
</AlertDialogContent>
|
|
|
|
|
</AlertDialog>
|
|
|
|
|
</BackofficeWrapper>
|
|
|
|
|
);
|
2026-04-11 17:04:14 +07:00
|
|
|
}
|