infoalloggi-monorepo/apps/infoalloggi/src/components/tables/banners-table.tsx

240 lines
5.3 KiB
TypeScript
Raw Normal View History

2025-08-04 17:45:44 +02:00
import type { ColumnDef } from "@tanstack/react-table";
import { Cog } from "lucide-react";
import { DataTable } from "~/components/custom_ui/data-table";
2025-08-28 18:27:07 +02:00
import { DataTableColumnHeader } from "~/components/custom_ui/dataTable-header";
2025-08-04 17:45:44 +02:00
import type { SearchFiltro } from "~/components/custom_ui/dataTable-toolbar";
import { Button } from "~/components/ui/button";
import type { Banners } from "~/schemas/public/Banners";
export const BannersTable = (props: {
2025-08-28 18:27:07 +02:00
data: Banners[];
setOpenEdit: (status: boolean) => void;
setEditData: (data: Banners | null) => void;
2025-08-04 17:45:44 +02:00
}) => {
2025-08-28 18:27:07 +02:00
const { data, setOpenEdit, setEditData } = props;
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const tabledata = data.map((row) => {
return {
2025-08-29 16:18:32 +02:00
actions: "Azioni",
color: row.color,
2025-08-28 18:27:07 +02:00
cta_href: row.cta_href,
cta_icon: row.cta_icon,
cta_text: row.cta_text,
2025-08-29 16:18:32 +02:00
has_cta: row.has_cta,
hide_duration: row.hide_duration,
idbanner: row.idbanner,
2025-08-28 18:27:07 +02:00
is_active: row.is_active,
2025-08-29 16:18:32 +02:00
is_unskippable: row.is_unskippable,
2025-08-28 18:27:07 +02:00
show_private: row.show_private,
2025-08-29 16:18:32 +02:00
show_public: row.show_public,
testo: row.testo,
titolo: row.titolo,
2025-08-28 18:27:07 +02:00
};
});
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const columns_titles = {
2025-08-29 16:18:32 +02:00
actions: "Azioni",
color: "Colore",
2025-08-28 18:27:07 +02:00
cta_href: "Indirizzo",
cta_icon: "Icona",
cta_text: "Testo",
2025-08-29 16:18:32 +02:00
has_cta: "Ha link",
hide_duration: "Durata",
idbanner: "Codice",
2025-08-28 18:27:07 +02:00
is_active: "Attivo",
2025-08-29 16:18:32 +02:00
is_unskippable: "Non saltabile",
2025-08-28 18:27:07 +02:00
show_private: "A. Riservata",
2025-08-29 16:18:32 +02:00
show_public: "Pubblico",
testo: "Testo",
titolo: "Titolo",
2025-08-28 18:27:07 +02:00
};
type Column = (typeof tabledata)[number];
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const columns: ColumnDef<Column>[] = [
{
2025-08-29 16:18:32 +02:00
accessorKey: "idbanner",
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader
column={column}
title={columns_titles.idbanner}
/>
),
},
{
2025-08-29 16:18:32 +02:00
accessorKey: "titolo",
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader column={column} title={columns_titles.titolo} />
),
},
{
2025-08-29 16:18:32 +02:00
accessorKey: "testo",
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader column={column} title={columns_titles.testo} />
),
},
{
2025-08-29 16:18:32 +02:00
accessorKey: "is_unskippable",
cell: ({ row }) => {
const data = row.original;
return data.is_unskippable ? "Vero" : "Falso";
},
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader
column={column}
title={columns_titles.is_unskippable}
/>
),
},
{
accessorKey: "has_cta",
cell: ({ row }) => {
const data = row.original;
return data.has_cta ? "Vero" : "Falso";
},
2025-08-29 16:18:32 +02:00
header: ({ column }) => (
<DataTableColumnHeader column={column} title={columns_titles.has_cta} />
),
2025-08-28 18:27:07 +02:00
},
{
2025-08-29 16:18:32 +02:00
accessorKey: "hide_duration",
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader
column={column}
title={columns_titles.hide_duration}
/>
),
},
{
2025-08-29 16:18:32 +02:00
accessorKey: "cta_href",
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader
column={column}
title={columns_titles.cta_href}
/>
),
},
{
2025-08-29 16:18:32 +02:00
accessorKey: "cta_icon",
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader
column={column}
title={columns_titles.cta_icon}
/>
),
},
{
2025-08-29 16:18:32 +02:00
accessorKey: "cta_text",
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader
column={column}
title={columns_titles.cta_text}
/>
),
},
{
2025-08-29 16:18:32 +02:00
accessorKey: "color",
2025-08-28 18:27:07 +02:00
header: ({ column }) => (
<DataTableColumnHeader column={column} title={columns_titles.color} />
),
},
{
accessorKey: "is_active",
cell: ({ row }) => {
const data = row.original;
return data.is_active ? "Vero" : "Falso";
},
header: ({ column }) => (
<DataTableColumnHeader
column={column}
2025-08-29 16:18:32 +02:00
title={columns_titles.is_active}
2025-08-28 18:27:07 +02:00
/>
),
2025-08-29 16:18:32 +02:00
},
{
2025-08-28 18:27:07 +02:00
accessorKey: "show_public",
cell: ({ row }) => {
const data = row.original;
return data.show_public ? "Vero" : "Falso";
},
header: ({ column }) => (
<DataTableColumnHeader
column={column}
2025-08-29 16:18:32 +02:00
title={columns_titles.show_public}
2025-08-28 18:27:07 +02:00
/>
),
2025-08-29 16:18:32 +02:00
},
{
2025-08-28 18:27:07 +02:00
accessorKey: "show_private",
cell: ({ row }) => {
const data = row.original;
return data.show_private ? "Vero" : "Falso";
},
2025-08-29 16:18:32 +02:00
header: ({ column }) => (
<DataTableColumnHeader
column={column}
title={columns_titles.show_private}
/>
),
2025-08-28 18:27:07 +02:00
},
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
{
2025-08-29 16:18:32 +02:00
accessorKey: "actions",
2025-08-28 18:27:07 +02:00
cell: ({ row }) => {
const data = row.original;
return (
<Button
aria-label="Edit Banner"
onClick={() => {
setEditData(data);
setOpenEdit(true);
}}
2025-08-29 16:18:32 +02:00
variant="outline"
2025-08-28 18:27:07 +02:00
>
<div className="flex items-center gap-2">
<Cog /> Modifica
</div>
</Button>
);
},
enableHiding: false,
enableSorting: false,
2025-08-29 16:18:32 +02:00
header: ({ column }) => (
<DataTableColumnHeader column={column} title={columns_titles.actions} />
),
2025-08-28 18:27:07 +02:00
},
];
2025-08-04 17:45:44 +02:00
2025-08-28 18:27:07 +02:00
const searchFiltro: SearchFiltro = {
columnName: "idbanner",
placeholder: "Filtra codice...",
};
return (
<div className="mx-auto w-full px-2 py-10">
<DataTable
columns={columns}
columns_titles={columns_titles}
2025-08-29 16:18:32 +02:00
data={tabledata}
2025-08-28 18:27:07 +02:00
defaultColumnVisibility={{
2025-08-29 16:18:32 +02:00
actions: true,
color: false,
2025-08-28 18:27:07 +02:00
cta_href: false,
cta_icon: false,
cta_text: false,
2025-08-29 16:18:32 +02:00
has_cta: true,
hide_duration: false,
idbanner: true,
2025-08-28 18:27:07 +02:00
is_active: true,
2025-08-29 16:18:32 +02:00
is_unskippable: true,
2025-08-28 18:27:07 +02:00
show_private: true,
2025-08-29 16:18:32 +02:00
show_public: true,
testo: true,
titolo: true,
2025-08-28 18:27:07 +02:00
}}
2025-08-29 16:18:32 +02:00
defaultSort={[{ desc: true, id: "idbanner" }]}
pinnedFiltri={undefined}
searchColumn={searchFiltro}
2025-08-28 18:27:07 +02:00
/>
</div>
);
2025-08-04 17:45:44 +02:00
};