- Updated translation utility to improve consistency and reduce redundancy. - Removed deprecated useTrans hook and related code. - Introduced a new Zustand store for managing translations and locale settings. - Cleaned up unused hooks and files related to localization.
81 lines
2.3 KiB
TypeScript
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 trans, { 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 = trans("uwil-backend::back.crud_back_to_list");
|
|
const title = trans(
|
|
route().current(`back.recipes.duplicate`)
|
|
? "uwil-backend::crud.helpers.duplication_model"
|
|
: "uwil-backend::crud.helpers.creation_model",
|
|
{
|
|
params: {
|
|
model: trans(`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>
|
|
);
|
|
}
|