258 lines
7.8 KiB
TypeScript
258 lines
7.8 KiB
TypeScript
"use client";
|
|
|
|
import * as React from "react";
|
|
import {
|
|
ColumnDef,
|
|
ColumnFiltersState,
|
|
PaginationState,
|
|
SortingState,
|
|
VisibilityState,
|
|
flexRender,
|
|
getCoreRowModel,
|
|
getFilteredRowModel,
|
|
getPaginationRowModel,
|
|
getSortedRowModel,
|
|
useReactTable,
|
|
} from "@tanstack/react-table";
|
|
import { Button } from "@/components/ui/button";
|
|
|
|
import {
|
|
Table,
|
|
TableBody,
|
|
TableCell,
|
|
TableHead,
|
|
TableHeader,
|
|
TableRow,
|
|
} from "@/components/ui/table";
|
|
import { Avatar, AvatarFallback, AvatarImage } from "@/components/ui/avatar";
|
|
import {
|
|
ChevronLeft,
|
|
ChevronRight,
|
|
Eye,
|
|
MoreVertical,
|
|
Search,
|
|
SquarePen,
|
|
Trash2,
|
|
TrendingDown,
|
|
TrendingUp,
|
|
} from "lucide-react";
|
|
import { cn } from "@/lib/utils";
|
|
import {
|
|
DropdownMenu,
|
|
DropdownMenuContent,
|
|
DropdownMenuItem,
|
|
DropdownMenuTrigger,
|
|
} from "@/components/ui/dropdown-menu";
|
|
import { Input } from "@/components/ui/input";
|
|
import { InputGroup, InputGroupText } from "@/components/ui/input-group";
|
|
import { paginationBlog } from "@/service/blog/blog";
|
|
import { ticketingPagination } from "@/service/ticketing/ticketing";
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { useRouter, useSearchParams } from "next/navigation";
|
|
import TablePagination from "@/components/table/table-pagination";
|
|
import columns from "./columns";
|
|
import { listTask } from "@/service/task";
|
|
|
|
const TaskTable = () => {
|
|
const router = useRouter();
|
|
const searchParams = useSearchParams();
|
|
|
|
const [dataTable, setDataTable] = React.useState<any[]>([]);
|
|
const [totalData, setTotalData] = React.useState<number>(1);
|
|
const [sorting, setSorting] = React.useState<SortingState>([]);
|
|
const [columnFilters, setColumnFilters] = React.useState<ColumnFiltersState>(
|
|
[]
|
|
);
|
|
const [columnVisibility, setColumnVisibility] =
|
|
React.useState<VisibilityState>({});
|
|
const [rowSelection, setRowSelection] = React.useState({});
|
|
const [pagination, setPagination] = React.useState<PaginationState>({
|
|
pageIndex: 0,
|
|
pageSize: 10,
|
|
});
|
|
const [page, setPage] = React.useState(1);
|
|
const [totalPage, setTotalPage] = React.useState(1);
|
|
const [limit, setLimit] = React.useState(10);
|
|
const [isSpecificAttention, setIsSpecificAttention] = React.useState(true);
|
|
const [search, setSearch] = React.useState<string>("");
|
|
|
|
const table = useReactTable({
|
|
data: dataTable,
|
|
columns,
|
|
onSortingChange: setSorting,
|
|
onColumnFiltersChange: setColumnFilters,
|
|
getCoreRowModel: getCoreRowModel(),
|
|
getPaginationRowModel: getPaginationRowModel(),
|
|
getSortedRowModel: getSortedRowModel(),
|
|
getFilteredRowModel: getFilteredRowModel(),
|
|
onColumnVisibilityChange: setColumnVisibility,
|
|
onRowSelectionChange: setRowSelection,
|
|
onPaginationChange: setPagination,
|
|
state: {
|
|
sorting,
|
|
columnFilters,
|
|
columnVisibility,
|
|
rowSelection,
|
|
pagination,
|
|
},
|
|
});
|
|
|
|
React.useEffect(() => {
|
|
const pageFromUrl = searchParams?.get("page");
|
|
if (pageFromUrl) {
|
|
setPage(Number(pageFromUrl));
|
|
}
|
|
}, [searchParams]);
|
|
|
|
React.useEffect(() => {
|
|
fetchData();
|
|
}, [page, limit, isSpecificAttention, search]);
|
|
|
|
async function fetchData() {
|
|
try {
|
|
const res = await listTask(
|
|
search,
|
|
page - 1,
|
|
limit,
|
|
isSpecificAttention ? "atensi-khusus" : "tugas-harian"
|
|
);
|
|
const data = res.data?.data;
|
|
const contentData = data?.content;
|
|
contentData.forEach((item: any, index: number) => {
|
|
item.no = (page - 1) * limit + index + 1;
|
|
});
|
|
|
|
console.log("contentData : ", contentData);
|
|
|
|
setDataTable(contentData);
|
|
setTotalData(data?.totalElements);
|
|
setTotalPage(data?.totalPages);
|
|
} catch (error) {
|
|
console.error("Error fetching tasks:", error);
|
|
}
|
|
}
|
|
|
|
const handleSearch = (e: React.ChangeEvent<HTMLInputElement>) => {
|
|
setSearch(e.target.value); // Perbarui state search
|
|
table.getColumn("judul")?.setFilterValue(e.target.value); // Set filter tabel
|
|
};
|
|
|
|
return (
|
|
<div className="w-full overflow-x-auto">
|
|
<div className="mx-5 mb-3">
|
|
<div className="">
|
|
<div className="row">
|
|
<div className="flex justify-between mb-6">
|
|
<label className="inline-flex text-md cursor-pointer">
|
|
<input
|
|
type="checkbox"
|
|
onChange={() => setIsSpecificAttention(!isSpecificAttention)}
|
|
hidden
|
|
/>
|
|
<span
|
|
className={` ${
|
|
isSpecificAttention
|
|
? "bg-default-900 text-white"
|
|
: "dark:text-default-700 border-2"
|
|
}
|
|
px-[18px] py-1 transition duration-100 rounded`}
|
|
>
|
|
Atensi Khusus
|
|
</span>
|
|
<span
|
|
className={`
|
|
${
|
|
!isSpecificAttention
|
|
? "bg-default-900 text-white"
|
|
: " dark:text-default-700 border-2"
|
|
}
|
|
px-[18px] py-1 transition duration-100 rounded
|
|
`}
|
|
>
|
|
Tugas Harian
|
|
</span>
|
|
</label>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div className="flex justify-between items-center px-5">
|
|
<div>
|
|
<InputGroup merged>
|
|
<InputGroupText className="bg-transparent dark:border-secondary dark:group-focus-within:border-secondary">
|
|
<Search className=" h-4 w-4 dark:text-white" />
|
|
</InputGroupText>
|
|
<Input
|
|
type="text"
|
|
placeholder="Search Judul..."
|
|
className="bg-transparent dark:border-secondary dark:placeholder-white/80 dark:focus:border-secondary dark:text-white"
|
|
value={search}
|
|
onChange={handleSearch}
|
|
/>
|
|
</InputGroup>
|
|
</div>
|
|
<div className="flex-none">
|
|
<Input
|
|
placeholder="Filter Status..."
|
|
value={
|
|
(table.getColumn("status")?.getFilterValue() as string) ?? ""
|
|
}
|
|
onChange={(event: React.ChangeEvent<HTMLInputElement>) =>
|
|
table.getColumn("status")?.setFilterValue(event.target.value)
|
|
}
|
|
className="max-w-sm "
|
|
/>
|
|
</div>
|
|
</div>
|
|
<Table className="overflow-hidden mt-3">
|
|
<TableHeader>
|
|
{table.getHeaderGroups().map((headerGroup) => (
|
|
<TableRow key={headerGroup.id} className="bg-default-200">
|
|
{headerGroup.headers.map((header) => (
|
|
<TableHead key={header.id}>
|
|
{header.isPlaceholder
|
|
? null
|
|
: flexRender(
|
|
header.column.columnDef.header,
|
|
header.getContext()
|
|
)}
|
|
</TableHead>
|
|
))}
|
|
</TableRow>
|
|
))}
|
|
</TableHeader>
|
|
<TableBody>
|
|
{table.getRowModel().rows?.length ? (
|
|
table.getRowModel().rows.map((row) => (
|
|
<TableRow
|
|
key={row.id}
|
|
data-state={row.getIsSelected() && "selected"}
|
|
className="h-[75px]"
|
|
>
|
|
{row.getVisibleCells().map((cell) => (
|
|
<TableCell key={cell.id}>
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())}
|
|
</TableCell>
|
|
))}
|
|
</TableRow>
|
|
))
|
|
) : (
|
|
<TableRow>
|
|
<TableCell colSpan={columns.length} className="h-24 text-center">
|
|
No results.
|
|
</TableCell>
|
|
</TableRow>
|
|
)}
|
|
</TableBody>
|
|
</Table>
|
|
<TablePagination
|
|
table={table}
|
|
totalData={totalData}
|
|
totalPage={totalPage}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default TaskTable;
|