ldc-ovoteam/resources/ts/back/Pages/Recipes/Create.tsx
Jérémy Munsch 04d038a61d
refactor(Recipes): replace 'create' and 'update' with 'viewAny' permission check
Updated permission checks in Create and Edit components to use 'viewAny' instead of 'create' and 'update'.
2026-09-21 11:50:40 +02:00

81 lines
2.3 KiB
TypeScript

import { route } from "ziggy-js";
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
import AuthenticatedLayout from "@/back/Layouts/AuthenticatedLayout";
import { Actions, Methods } from "@/back/lib/enums";
import Form from "@/back/Pages/Recipes/Form";
import { SingleResource } from "@/back/types/global";
import {
RawRecipeModel,
validateAsRawRecipeModel,
validateAsRecipeModel,
} from "@/back/types/recipes";
import usePermissions from "@/hooks/use-permissions";
import useTrans, { UseTransOptions } from "@/hooks/use-translations";
import { ArrowLeftIcon } from "lucide-react";
export default function Create({
data,
}: Readonly<{
data: SingleResource;
}>) {
const { can } = usePermissions();
const viewAny = can("viewAny", String.raw`App\Models\Recipe`);
let recipe: RawRecipeModel | undefined;
try {
const hasData =
data?.data &&
(Array.isArray(data.data)
? data.data.length > 0
: Object.keys(data.data).length > 0);
if (hasData) {
if (route().current(`back.recipes.duplicate`)) {
recipe = validateAsRawRecipeModel(data.data);
} else {
recipe = validateAsRecipeModel(data.data);
}
}
} catch (error) {
console.warn("Zod validation failed, falling back to empty model.", error);
}
const transBackToList = useTrans("uwil-backend::back.crud_back_to_list");
const title = useTrans(
route().current(`back.recipes.duplicate`)
? "uwil-backend::crud.helpers.duplication_model"
: "uwil-backend::crud.helpers.creation_model",
{
params: {
model: useTrans(`models.classes.recipe`, {
choice: 1,
}),
},
} as UseTransOptions,
);
const beforeNode = () => {
return (
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>
)
);
};
return (
<AuthenticatedLayout title={title} beforeNode={beforeNode()}>
<Form
model={recipe}
modelName="recipe"
modelRoute="recipes"
action={Actions.STORE}
method={Methods.POST}
/>
</AuthenticatedLayout>
);
}