|
|
@ -15,22 +15,26 @@ import HeightIcon from "@mui/icons-material/Height"; |
|
|
|
import RemoveRedEyeIcon from "@mui/icons-material/RemoveRedEye"; |
|
|
|
import { IconButton, Tooltip } from "@mui/material"; |
|
|
|
import { grey } from "@mui/material/colors"; |
|
|
|
import { IProducts, Product } from "@/interface/products.interface"; |
|
|
|
|
|
|
|
const TanStackTable = ({ data }: any) => { |
|
|
|
const [tableData, setTableData] = useState([]); |
|
|
|
interface TanStackTableProps { |
|
|
|
data: IProducts; |
|
|
|
} |
|
|
|
const TanStackTable = ({ data }: TanStackTableProps) => { |
|
|
|
const [tableData, setTableData] = useState<Product[] | []>([]); |
|
|
|
const [open, setOpen] = useState(false); |
|
|
|
const [reviewModalData, setReviewModalData] = useState([]); |
|
|
|
const [sortOrder, setSortOrder] = useState<"asc" | "desc" | null>(null); |
|
|
|
const handleOpen = () => setOpen(true); |
|
|
|
const handleClose = () => setOpen(false); |
|
|
|
|
|
|
|
const handleSort = (key: string) => { |
|
|
|
const handleSort = (key: keyof Product) => { |
|
|
|
let sortedData; |
|
|
|
if (sortOrder === "asc") { |
|
|
|
sortedData = [...tableData].sort((a, b) => (a[key] > b[key] ? -1 : 1)); |
|
|
|
sortedData = [...tableData.sort((a, b) => (a[key] && b[key] ? (a[key] > b[key] ? -1 : 1) : 0))]; |
|
|
|
setSortOrder("desc"); |
|
|
|
} else { |
|
|
|
sortedData = [...tableData].sort((a, b) => (a[key] > b[key] ? 1 : -1)); |
|
|
|
sortedData = [...tableData].sort((a, b) => (a[key] && b[key] ? (a[key] > b[key] ? 1 : -1) : 0)); |
|
|
|
setSortOrder("asc"); |
|
|
|
} |
|
|
|
setTableData(sortedData); |
|
|
@ -56,16 +60,16 @@ const TanStackTable = ({ data }: any) => { |
|
|
|
{ |
|
|
|
header: "Title", |
|
|
|
accessorKey: "title", |
|
|
|
|
|
|
|
}, |
|
|
|
{ |
|
|
|
header: "Description", |
|
|
|
accessorKey: "description", |
|
|
|
cell: ({ row }: any) => ( |
|
|
|
<> |
|
|
|
<Tooltip title={row.original.description}> |
|
|
|
<p className={styles.clamp_2}>{row.original.description}</p> |
|
|
|
</Tooltip> |
|
|
|
</> |
|
|
|
|
|
|
|
<Tooltip title={row.original.description}> |
|
|
|
<p className={styles.clamp_2}>{row.original.description}</p> |
|
|
|
</Tooltip> |
|
|
|
), |
|
|
|
}, |
|
|
|
{ |
|
|
@ -128,7 +132,7 @@ const TanStackTable = ({ data }: any) => { |
|
|
|
}); |
|
|
|
|
|
|
|
useEffect(() => { |
|
|
|
setTableData(data?.products); |
|
|
|
setTableData(data.products); |
|
|
|
}, [data]); |
|
|
|
|
|
|
|
return ( |
|
|
@ -136,9 +140,9 @@ const TanStackTable = ({ data }: any) => { |
|
|
|
<div className={styles.responsive_table}> |
|
|
|
<table className={styles.table}> |
|
|
|
<thead> |
|
|
|
{table.getHeaderGroups().map((headerGroup: any) => ( |
|
|
|
{table.getHeaderGroups().map((headerGroup) => ( |
|
|
|
<tr key={headerGroup?.id}> |
|
|
|
{headerGroup.headers.map((header: any) => ( |
|
|
|
{headerGroup.headers.map((header) => ( |
|
|
|
<th key={header?.id} className={global.body2}> |
|
|
|
{flexRender( |
|
|
|
header.column.columnDef.header, |
|
|
@ -150,9 +154,9 @@ const TanStackTable = ({ data }: any) => { |
|
|
|
))} |
|
|
|
</thead> |
|
|
|
<tbody> |
|
|
|
{table.getRowModel().rows.map((row: any) => ( |
|
|
|
{table.getRowModel().rows.map((row) => ( |
|
|
|
<tr key={row.id}> |
|
|
|
{row.getVisibleCells().map((cell: any) => ( |
|
|
|
{row.getVisibleCells().map((cell) => ( |
|
|
|
<td key={cell.id}> |
|
|
|
{flexRender(cell.column.columnDef.cell, cell.getContext())} |
|
|
|
</td> |
|
|
|