297 lines
12 KiB
TypeScript
297 lines
12 KiB
TypeScript
// * Packages
|
|
import { route } from "ziggy-js";
|
|
import {
|
|
AlertCircleIcon,
|
|
ArchiveIcon,
|
|
ArrowLeftIcon,
|
|
CogIcon,
|
|
LockIcon,
|
|
PlusIcon,
|
|
StickyNoteIcon,
|
|
TriangleAlertIcon,
|
|
} from "lucide-react";
|
|
import { usePage, Link } from "@inertiajs/react";
|
|
import { Row } from "@tanstack/react-table";
|
|
// * Components shadcn
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { Button } from "@/components/ui/button";
|
|
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
|
|
import {
|
|
Tooltip,
|
|
TooltipContent,
|
|
TooltipTrigger,
|
|
} from "@/components/ui/tooltip";
|
|
// * Other
|
|
import AuthenticatedLayout from "@/back/Layouts/AuthenticatedLayout";
|
|
import DataTable from "@/back/Components/Table/DataTable";
|
|
import DataTableSearch from "@/back/Components/Table/DataTableSearch";
|
|
import DataTableFilterButton from "@/back/Components/Table/DataTableFilterButton";
|
|
import InputError from "@/back/Components/InputError";
|
|
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
|
import { useFormatedDate } from "@/hooks/use-formated-value";
|
|
import { Views } from "@/lib/utils";
|
|
import {
|
|
AppEnv,
|
|
AuthProp,
|
|
DynamicPageModel,
|
|
PageConfigModel,
|
|
Resource,
|
|
TranslationField,
|
|
} from "@/back/types/global";
|
|
import str from "@/hooks/use-string";
|
|
import useTrans from "@/hooks/use-translations";
|
|
import DynamicStatusBadge from "@/back/Components/DynamicPages/DynamicStatusBadge";
|
|
|
|
export default function Index({
|
|
dynamicPageModels,
|
|
search,
|
|
searchFields,
|
|
}: {
|
|
dynamicPageModels: Resource;
|
|
search: string;
|
|
searchFields: string;
|
|
}) {
|
|
const { auth, appEnv } = usePage().props as unknown as {
|
|
auth: AuthProp;
|
|
appEnv: AppEnv;
|
|
};
|
|
const { viewAny, viewTrash, create } = auth.policies.dynamicPages;
|
|
const isArchiveRoute = route().current("back.dynamic-pages.trash");
|
|
const actionModels = isArchiveRoute ? [
|
|
Views.RESTORATION,
|
|
Views.FORCEDELETION,
|
|
] : [
|
|
Views.VISUALIZATION,
|
|
Views.EDITION,
|
|
Views.DUPLICATION,
|
|
Views.ARCHIVATION,
|
|
];
|
|
const columns = [
|
|
{
|
|
accessorKey: "title",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(useTrans(`validation.attributes.${column.id}`)).ucFirst().value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DynamicPageModel> }) => {
|
|
return (row.getValue("title") as TranslationField)[appEnv.locale] ? (
|
|
(row.getValue("title") as TranslationField)[appEnv.locale]
|
|
) : (<p className="text-muted-foreground italic m-0">{useTrans('back.table_not_known')}</p>);
|
|
}
|
|
},
|
|
{
|
|
accessorKey: "parent_page",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(useTrans(`validation.custom.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DynamicPageModel> }) => {
|
|
if (row.getValue("parent_page")) {
|
|
const dynamicPageModel = row.getValue("parent_page") as DynamicPageModel;
|
|
const cellValue = dynamicPageModel.title[appEnv.locale];
|
|
let value = <span>{cellValue}</span>;
|
|
if (auth.policies.dynamicPages.viewAny) {
|
|
value = (
|
|
<Button variant="secondary" type="button" size="sm" asChild>
|
|
<Link href={route("back.dynamic-pages.show", dynamicPageModel)}>
|
|
<StickyNoteIcon />
|
|
<span>{cellValue}</span>
|
|
</Link>
|
|
</Button>
|
|
);
|
|
}
|
|
if (dynamicPageModel.deleted_at !== null) {
|
|
value = (
|
|
<div className="flex flex-row justify-center items-center">
|
|
<span>{cellValue}</span>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<TriangleAlertIcon
|
|
size={15}
|
|
className="text-destructive ms-1"
|
|
/>
|
|
</TooltipTrigger>
|
|
<TooltipContent>
|
|
{useTrans("back.group_archived")}
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</div>
|
|
);
|
|
}
|
|
return value;
|
|
}
|
|
return (
|
|
<p className="text-muted-foreground italic m-0">
|
|
{useTrans("back.table_not_known")}
|
|
</p>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "config",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(useTrans(`validation.custom.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DynamicPageModel> }) => {
|
|
if (row.getValue("config")) {
|
|
const pageConfigModel = row.getValue("config") as PageConfigModel;
|
|
const cellValue = pageConfigModel.label[appEnv.locale];
|
|
let value = <span>{cellValue}</span>;
|
|
if (auth.policies.pageConfigs.viewAny) {
|
|
value = (
|
|
<Button variant="secondary" type="button" size="sm" asChild>
|
|
<Link href={route("back.page-configs.show", row.getValue("config"))}>
|
|
<CogIcon />
|
|
<span>{cellValue}</span>
|
|
</Link>
|
|
</Button>
|
|
);
|
|
}
|
|
if (pageConfigModel.deleted_at !== null) {
|
|
value = (
|
|
<div className="flex flex-row justify-center items-center">
|
|
<span>{cellValue}</span>
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<TriangleAlertIcon
|
|
size={15}
|
|
className="text-destructive ms-1"
|
|
/>
|
|
</TooltipTrigger>
|
|
<TooltipContent>
|
|
{useTrans("back.config_archived")}
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
</div>
|
|
);
|
|
}
|
|
return value;
|
|
}
|
|
return (
|
|
<p className="text-muted-foreground italic m-0">
|
|
{useTrans("back.table_not_known")}
|
|
</p>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "status",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(useTrans(`validation.custom.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DynamicPageModel> }) => (
|
|
<div className="flex justify-center items-center">
|
|
<DynamicStatusBadge value={row.getValue("status")} />
|
|
{row.original.isLocked && (
|
|
<Tooltip>
|
|
<TooltipTrigger asChild>
|
|
<LockIcon size={15} className="ms-1" />
|
|
</TooltipTrigger>
|
|
<TooltipContent>
|
|
{useTrans("back.page_locked")}
|
|
</TooltipContent>
|
|
</Tooltip>
|
|
)}
|
|
</div>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "updated_at",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(useTrans(`validation.attributes.${column.id}`)).ucFirst().value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DynamicPageModel> }) => (
|
|
<Badge variant="secondary">
|
|
{useFormatedDate(row.getValue("updated_at"))}
|
|
</Badge>
|
|
),
|
|
},
|
|
];
|
|
|
|
const transModel = useTrans("models.classes.dynamic-page", { choice: 2 });
|
|
const transArchiveTitle = useTrans("crud.helpers.archives_model", { params: { model: transModel }})
|
|
const transTitle = str(isArchiveRoute ? transArchiveTitle : transModel).ucFirst().value();
|
|
|
|
const beforeNode = () => {
|
|
return isArchiveRoute && viewAny && (
|
|
<div className="flex justify-end items-center gap-x-1.5">
|
|
<BtnOptionnalLinkWithTooltip
|
|
link={route("back.dynamic-pages.index")}
|
|
content={<ArrowLeftIcon />}
|
|
tooltipContent={<p>{useTrans("back.crud_back_to_list")}</p>}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
const afterNode = () => {
|
|
return !isArchiveRoute && (
|
|
<div className="flex justify-end items-center gap-x-1.5">
|
|
{viewTrash && <BtnOptionnalLinkWithTooltip
|
|
link={route("back.dynamic-pages.trash")}
|
|
content={<ArchiveIcon />}
|
|
tooltipContent={<p>{useTrans("back.crud_access_trash")}</p>}
|
|
variant={"outline"}
|
|
/>}
|
|
{create && <BtnOptionnalLinkWithTooltip
|
|
link={route("back.dynamic-pages.create")}
|
|
content={<PlusIcon />}
|
|
tooltipContent={<p>{useTrans("back.crud_add_entry")}</p>}
|
|
/>}
|
|
</div>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<AuthenticatedLayout
|
|
title={transTitle}
|
|
beforeNode={beforeNode()}
|
|
afterNode={afterNode()}
|
|
>
|
|
{isArchiveRoute && (
|
|
<>
|
|
<Alert
|
|
variant="destructive"
|
|
className="text-left mb-4"
|
|
>
|
|
<AlertCircleIcon className="h-4 w-4" />
|
|
<AlertTitle>{useTrans('back.warning')}</AlertTitle>
|
|
<AlertDescription>
|
|
<p>{useTrans('back.warning_trash')}</p>
|
|
</AlertDescription>
|
|
</Alert>
|
|
<InputError all={true} />
|
|
</>
|
|
)}
|
|
<DataTableSearch
|
|
modelRoute="dynamic-pages"
|
|
searchText={search}
|
|
searchFields={searchFields}
|
|
/>
|
|
<DataTable
|
|
columns={columns}
|
|
modelRoute="dynamic-pages"
|
|
modelsPaginate={dynamicPageModels}
|
|
actions={actionModels}
|
|
/>
|
|
</AuthenticatedLayout>
|
|
);
|
|
}
|