215 lines
6.3 KiB
TypeScript
215 lines
6.3 KiB
TypeScript
// * Packages
|
|
import { route } from "ziggy-js";
|
|
import { usePage } from "@inertiajs/react";
|
|
import { Row } from "@tanstack/react-table";
|
|
import { ArchiveIcon, ArrowLeftIcon, CheckIcon, PlusIcon, XIcon } from "lucide-react";
|
|
// * Components shadcn
|
|
import { Badge } from "@/components/ui/badge";
|
|
// * 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 SoftdeleteWarning from "@/back/Components/SoftdeleteWarning";
|
|
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
|
import RenderTranslationCell from "@/back/Components/Table/RenderTranslationCell";
|
|
import { GetFormatedDate, GetTranslation } from "@/lib/utils";
|
|
import { Views } from "@/back/lib/actions";
|
|
import { AuthProp, DefaultModel, Resource } from "@/back/types/global";
|
|
import str from "@/hooks/use-string";
|
|
import useTrans from "@/hooks/use-translations";
|
|
import { InvoicePackageModel } from "@/back/types/project";
|
|
|
|
const getColumns = () => {
|
|
return [
|
|
{
|
|
accessorKey: "name",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(
|
|
GetTranslation(`validation.custom.${column.id}`),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<InvoicePackageModel> }) => (
|
|
<RenderTranslationCell value={row.getValue("name")} />
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "amount",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(
|
|
GetTranslation(`validation.custom.${column.id}`),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<InvoicePackageModel> }) => row.getValue("amount"),
|
|
},
|
|
{
|
|
accessorKey: "published",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(
|
|
GetTranslation(`uwil-backend::validation.custom.${column.id}`),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<InvoicePackageModel> }) => (
|
|
<Badge
|
|
variant="outline"
|
|
className={`border-0 ${
|
|
row.getValue("published")
|
|
? "bg-green-200 dark:bg-green-900"
|
|
: "bg-red-200 dark:bg-red-900"
|
|
}`}
|
|
>
|
|
{row.getValue("published") ? (
|
|
<CheckIcon size={15} className="mr-1" />
|
|
) : (
|
|
<XIcon size={15} className="mr-1" />
|
|
)}
|
|
{str(
|
|
GetTranslation(
|
|
row.getValue("published")
|
|
? "uwil-backend::back.yes"
|
|
: "uwil-backend::back.no",
|
|
),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
</Badge>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "updated_at",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(
|
|
GetTranslation(`uwil-backend::validation.attributes.${column.id}`),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DefaultModel> }) => (
|
|
<Badge variant="secondary">
|
|
{GetFormatedDate(row.getValue("updated_at"))}
|
|
</Badge>
|
|
),
|
|
},
|
|
];
|
|
};
|
|
|
|
export default function Index({
|
|
models,
|
|
searchFields,
|
|
modelName,
|
|
}: {
|
|
models: Resource;
|
|
searchFields: string;
|
|
modelName: string;
|
|
}) {
|
|
const { auth } = usePage().props as unknown as {
|
|
auth: AuthProp;
|
|
};
|
|
const { viewAny, viewTrash, create } = auth.policies.invoicePackages;
|
|
const isArchiveRoute = route().current(`back.${modelName}.trash`);
|
|
const actionModels = isArchiveRoute
|
|
? [Views.RESTORATION, Views.FORCEDELETION]
|
|
: [
|
|
Views.VISUALIZATION,
|
|
Views.EDITION,
|
|
Views.DUPLICATION,
|
|
Views.ARCHIVATION,
|
|
];
|
|
const columns = getColumns();
|
|
|
|
// Translations.
|
|
const transModel = useTrans(`models.classes.${modelName}`, { choice: 2 });
|
|
const transArchiveTitle = useTrans(
|
|
"uwil-backend::crud.helpers.archives_model",
|
|
{
|
|
params: { model: transModel },
|
|
},
|
|
);
|
|
const transTitle = str(isArchiveRoute ? transArchiveTitle : transModel)
|
|
.ucFirst()
|
|
.value();
|
|
const transCrudBackList = useTrans("uwil-backend::back.crud_back_to_list");
|
|
|
|
const beforeNode = () => {
|
|
return (
|
|
isArchiveRoute &&
|
|
viewAny && (
|
|
<div className="flex justify-end items-center gap-x-1.5">
|
|
<BtnOptionnalLinkWithTooltip
|
|
link={route(`back.${modelName}.index`)}
|
|
content={<ArrowLeftIcon />}
|
|
tooltipContent={<p>{transCrudBackList}</p>}
|
|
/>
|
|
</div>
|
|
)
|
|
);
|
|
};
|
|
const transCrudAddEntry = useTrans("uwil-backend::back.crud_add_entry");
|
|
const transAccessTo = useTrans("uwil-backend::back.crud_access_to", {
|
|
params: {
|
|
target: useTrans("uwil-backend::back.archived_entries"),
|
|
},
|
|
});
|
|
const afterNode = () => {
|
|
return (
|
|
!isArchiveRoute && (
|
|
<div className="flex justify-end items-center gap-x-1.5">
|
|
{viewTrash && (
|
|
<BtnOptionnalLinkWithTooltip
|
|
link={route(`back.${modelName}.trash`)}
|
|
content={<ArchiveIcon />}
|
|
tooltipContent={<p>{transAccessTo}</p>}
|
|
variant="outline"
|
|
/>
|
|
)}
|
|
{create && (
|
|
<BtnOptionnalLinkWithTooltip
|
|
link={route(`back.${modelName}.create`)}
|
|
content={<PlusIcon />}
|
|
tooltipContent={<p>{transCrudAddEntry}</p>}
|
|
/>
|
|
)}
|
|
</div>
|
|
)
|
|
);
|
|
};
|
|
|
|
return (
|
|
<AuthenticatedLayout
|
|
title={transTitle}
|
|
beforeNode={beforeNode()}
|
|
afterNode={afterNode()}
|
|
>
|
|
{isArchiveRoute && <SoftdeleteWarning />}
|
|
<DataTableSearch
|
|
modelRoute={modelName}
|
|
searchFields={searchFields}
|
|
isArchiveRoute={isArchiveRoute}
|
|
/>
|
|
<DataTable
|
|
columns={columns}
|
|
modelRoute={modelName}
|
|
modelsPaginate={models}
|
|
actions={actionModels}
|
|
/>
|
|
</AuthenticatedLayout>
|
|
);
|
|
}
|