Improves the readability of the conditional expression in the getColumns function.
366 lines
11 KiB
TypeScript
366 lines
11 KiB
TypeScript
// * Packages
|
|
import { Row } from "@tanstack/react-table";
|
|
import {
|
|
ArchiveIcon,
|
|
ArrowLeftIcon,
|
|
CheckIcon,
|
|
PlusIcon,
|
|
XIcon,
|
|
} from "lucide-react";
|
|
import { route } from "ziggy-js";
|
|
// * Components shadcn
|
|
import { Badge } from "@/components/ui/badge";
|
|
import { usePage } from "@inertiajs/react";
|
|
// * Other
|
|
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
|
import SoftdeleteWarning from "@/back/Components/SoftdeleteWarning";
|
|
import DataTable from "@/back/Components/Table/DataTable";
|
|
import DataTableFilterButton from "@/back/Components/Table/DataTableFilterButton";
|
|
import DataTableSearch from "@/back/Components/Table/DataTableSearch";
|
|
import AuthenticatedLayout from "@/back/Layouts/AuthenticatedLayout";
|
|
import Views from "@/back/lib/actions";
|
|
import { AuthProp, DefaultModel, Resource } from "@/back/types/global";
|
|
import str from "@/hooks/use-string";
|
|
import trans from "@/hooks/use-translations";
|
|
import { formatDate } from "@/lib/utils";
|
|
import { AppEnv, LaravelEnum, LaravelEnumMap } from "@/types/global";
|
|
|
|
const getColumns = (
|
|
enumProductCategory: LaravelEnumMap,
|
|
enumConsumptionTime: LaravelEnumMap,
|
|
enumInspiration: LaravelEnumMap,
|
|
enumSeason: LaravelEnumMap,
|
|
locale: string,
|
|
) => {
|
|
return [
|
|
{
|
|
accessorKey: "name",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(trans(`validation.attributes.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "product_category",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(trans(`validation.attributes.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DefaultModel & Record<string, unknown>> }) => {
|
|
const val = row.getValue("product_category") as unknown;
|
|
const rawLabel =
|
|
typeof val === "object" && val !== null
|
|
? (val as LaravelEnum).label
|
|
: val;
|
|
let mappedLabel = rawLabel;
|
|
const matchingEnum = Object.values(enumProductCategory).find(
|
|
(opt) =>
|
|
String((opt as LaravelEnum).value) === String(rawLabel) ||
|
|
(opt as LaravelEnum).name === rawLabel,
|
|
) as LaravelEnum | undefined;
|
|
if (matchingEnum) {
|
|
mappedLabel =
|
|
typeof matchingEnum.label === "object"
|
|
? String(
|
|
(matchingEnum.label as Record<string, string>)[locale] ??
|
|
matchingEnum.label,
|
|
)
|
|
: String(matchingEnum.label);
|
|
}
|
|
return (
|
|
<Badge variant="outline">
|
|
{str(String(mappedLabel)).ucFirst().value()}
|
|
</Badge>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "consomption_time",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(trans(`validation.attributes.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DefaultModel & Record<string, unknown>> }) => {
|
|
const val = row.getValue("consomption_time") as unknown;
|
|
const rawLabel =
|
|
typeof val === "object" && val !== null
|
|
? (val as LaravelEnum).label
|
|
: val;
|
|
let mappedLabel = rawLabel;
|
|
const matchingEnum = Object.values(enumConsumptionTime).find(
|
|
(opt) =>
|
|
String((opt as LaravelEnum).value) === String(rawLabel) ||
|
|
(opt as LaravelEnum).name === rawLabel,
|
|
) as LaravelEnum | undefined;
|
|
if (matchingEnum) {
|
|
mappedLabel =
|
|
typeof matchingEnum.label === "object"
|
|
? String(
|
|
(matchingEnum.label as Record<string, string>)[locale] ??
|
|
matchingEnum.label,
|
|
)
|
|
: String(matchingEnum.label);
|
|
}
|
|
return (
|
|
<Badge variant="outline">
|
|
{str(String(mappedLabel)).ucFirst().value()}
|
|
</Badge>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "inspiration",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(trans(`validation.attributes.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DefaultModel & Record<string, unknown>> }) => {
|
|
const val = row.getValue("inspiration") as unknown;
|
|
const rawLabel =
|
|
typeof val === "object" && val !== null
|
|
? (val as LaravelEnum).label
|
|
: val;
|
|
let mappedLabel = rawLabel;
|
|
const matchingEnum = Object.values(enumInspiration).find(
|
|
(opt) =>
|
|
String((opt as LaravelEnum).value) === String(rawLabel) ||
|
|
(opt as LaravelEnum).name === rawLabel,
|
|
) as LaravelEnum | undefined;
|
|
if (matchingEnum) {
|
|
mappedLabel =
|
|
typeof matchingEnum.label === "object"
|
|
? String(
|
|
(matchingEnum.label as Record<string, string>)[locale] ??
|
|
matchingEnum.label,
|
|
)
|
|
: String(matchingEnum.label);
|
|
}
|
|
return (
|
|
<Badge variant="outline">
|
|
{str(String(mappedLabel)).ucFirst().value()}
|
|
</Badge>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "season",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(trans(`validation.attributes.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DefaultModel & Record<string, unknown>> }) => {
|
|
const val = row.getValue("season") as unknown;
|
|
const rawLabel =
|
|
typeof val === "object" && val !== null
|
|
? (val as LaravelEnum).label
|
|
: val;
|
|
let mappedLabel = rawLabel;
|
|
const matchingEnum = Object.values(enumSeason).find(
|
|
(opt) =>
|
|
String((opt as LaravelEnum).value) === String(rawLabel) ||
|
|
(opt as LaravelEnum).name === rawLabel,
|
|
) as LaravelEnum | undefined;
|
|
if (matchingEnum) {
|
|
mappedLabel =
|
|
typeof matchingEnum.label === "object"
|
|
? String(
|
|
(matchingEnum.label as Record<string, string>)[locale] ??
|
|
matchingEnum.label,
|
|
)
|
|
: String(matchingEnum.label);
|
|
}
|
|
return (
|
|
<Badge variant="outline">
|
|
{str(String(mappedLabel)).ucFirst().value()}
|
|
</Badge>
|
|
);
|
|
},
|
|
},
|
|
{
|
|
accessorKey: "is_published",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(trans(`validation.attributes.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DefaultModel & Record<string, unknown>> }) => (
|
|
<Badge
|
|
variant="outline"
|
|
className={`border-0 ${
|
|
row.getValue("is_published")
|
|
? "bg-green-200 dark:bg-green-900 text-green-900 dark:text-green-100"
|
|
: "bg-amber-200 dark:bg-amber-900 text-amber-900 dark:text-amber-100"
|
|
}`}
|
|
>
|
|
{row.getValue("is_published") ? (
|
|
<CheckIcon size={15} className="mr-1" />
|
|
) : (
|
|
<XIcon size={15} className="mr-1" />
|
|
)}
|
|
{str(
|
|
trans(
|
|
row.getValue("is_published") ? "back.published" : "back.draft",
|
|
),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
</Badge>
|
|
),
|
|
},
|
|
{
|
|
accessorKey: "updated_at",
|
|
header: ({ column }: { column: { id: string } }) => (
|
|
<DataTableFilterButton
|
|
label={str(trans(`validation.attributes.${column.id}`))
|
|
.ucFirst()
|
|
.value()}
|
|
field={column.id}
|
|
/>
|
|
),
|
|
cell: ({ row }: { row: Row<DefaultModel & Record<string, unknown>> }) => (
|
|
<Badge variant="secondary">
|
|
{formatDate(row.getValue("updated_at"))}
|
|
</Badge>
|
|
),
|
|
},
|
|
];
|
|
};
|
|
|
|
export default function Index({
|
|
paginatedModels,
|
|
searchFields,
|
|
}: Readonly<{
|
|
paginatedModels: Resource;
|
|
searchFields: string;
|
|
}>) {
|
|
const {
|
|
auth,
|
|
appEnv,
|
|
enumProductCategory,
|
|
enumConsumptionTime,
|
|
enumInspiration,
|
|
enumSeason,
|
|
} = usePage().props as unknown as {
|
|
auth: AuthProp;
|
|
appEnv: AppEnv;
|
|
enumProductCategory: LaravelEnumMap;
|
|
enumConsumptionTime: LaravelEnumMap;
|
|
enumInspiration: LaravelEnumMap;
|
|
enumSeason: LaravelEnumMap;
|
|
};
|
|
const { create, viewTrash, viewAny } = auth.policies.recipes;
|
|
const isArchiveRoute = route().current("back.recipes.trash");
|
|
const actionModels = isArchiveRoute
|
|
? [Views.RESTORATION, Views.FORCEDELETION]
|
|
: [
|
|
Views.VISUALIZATION,
|
|
Views.EDITION,
|
|
Views.DUPLICATION,
|
|
Views.ARCHIVATION,
|
|
];
|
|
|
|
const columns = getColumns(
|
|
enumProductCategory ?? {},
|
|
enumConsumptionTime ?? {},
|
|
enumInspiration ?? {},
|
|
enumSeason ?? {},
|
|
appEnv.locale,
|
|
);
|
|
|
|
const transModel = trans("models.classes.recipe", { choice: Infinity });
|
|
const transArchiveTitle = trans("uwil-backend::crud.helpers.archives_model", {
|
|
params: { model: transModel },
|
|
});
|
|
const transTitle = str(isArchiveRoute ? transArchiveTitle : transModel)
|
|
.ucFirst()
|
|
.value();
|
|
|
|
const transBackToList = trans("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.recipes.index")}
|
|
content={<ArrowLeftIcon />}
|
|
tooltipContent={<p>{transBackToList}</p>}
|
|
/>
|
|
</div>
|
|
)
|
|
);
|
|
};
|
|
|
|
const transAccessToArchived = trans("uwil-backend::back.crud_access_to", {
|
|
params: { target: trans("uwil-backend::back.archived_entries") },
|
|
});
|
|
const transCrudAddEntry = trans("uwil-backend::back.crud_add_entry");
|
|
const afterNode = () => {
|
|
return (
|
|
!isArchiveRoute && (
|
|
<div className="flex justify-end items-center gap-x-1.5">
|
|
{viewTrash && (
|
|
<BtnOptionnalLinkWithTooltip
|
|
link={route("back.recipes.trash")}
|
|
content={<ArchiveIcon />}
|
|
tooltipContent={<p>{transAccessToArchived}</p>}
|
|
variant="outline"
|
|
/>
|
|
)}
|
|
{create && (
|
|
<BtnOptionnalLinkWithTooltip
|
|
link={route("back.recipes.create")}
|
|
content={<PlusIcon />}
|
|
tooltipContent={<p>{transCrudAddEntry}</p>}
|
|
/>
|
|
)}
|
|
</div>
|
|
)
|
|
);
|
|
};
|
|
|
|
return (
|
|
<AuthenticatedLayout
|
|
title={transTitle}
|
|
beforeNode={beforeNode()}
|
|
afterNode={afterNode()}
|
|
>
|
|
{isArchiveRoute && <SoftdeleteWarning />}
|
|
<DataTableSearch
|
|
modelRoute="recipes"
|
|
prefixRoute="back."
|
|
searchFields={searchFields}
|
|
isArchiveRoute={isArchiveRoute}
|
|
/>
|
|
<DataTable
|
|
columns={columns}
|
|
modelRoute="recipes"
|
|
modelsPaginate={paginatedModels}
|
|
actions={actionModels}
|
|
/>
|
|
</AuthenticatedLayout>
|
|
);
|
|
}
|