558 lines
22 KiB
TypeScript
558 lines
22 KiB
TypeScript
// * Packages
|
|
import { useForm, usePage } from "@inertiajs/react";
|
|
import { BookTextIcon, CogIcon } from "lucide-react";
|
|
import { useEffect, useState } from "react";
|
|
// * Components shadcn
|
|
import { Separator } from "@/components/ui/separator";
|
|
import { Skeleton } from "@/components/ui/skeleton";
|
|
import { Card, CardContent } from "@/components/ui/card";
|
|
// * Other
|
|
import FieldsetWrapper from "@/back/Components/FieldsetWrapper";
|
|
import InputError from "@/back/Components/InputError";
|
|
import GoToFrontView from "@/back/Components/GoToFrontView";
|
|
import SimpleString from "@/back/Components/Inputs/SimpleString";
|
|
import EnumSelect from "@/back/Components/Inputs/EnumSelect";
|
|
import SimpleWysiwyg from "@/back/Components/Inputs/Wysiwyg/SimpleWysiwyg";
|
|
import SimpleDatePicker from "@/back/Components/Inputs/SimpleDatePicker";
|
|
import CompleteInputFile from "@/back/Components/Inputs/CompleteInputFile";
|
|
import SimpleSwitch from "@/back/Components/Inputs/SimpleSwitch";
|
|
import SimpleVimeoId from "@/back/Components/Inputs/SimpleVimeoId";
|
|
import { cn } from "@/lib/utils";
|
|
import { AppEnv, LaravelEnum } from "@/back/types/global";
|
|
import { DynamicFieldTypes } from "@/back/types/dynamic-pages";
|
|
import { NewsModel } from "@/back/types/others";
|
|
import useTrans from "@/hooks/use-translations";
|
|
import str from "@/hooks/use-string";
|
|
import useCrudSubmit from "@/hooks/use-crud-submit";
|
|
import SubmitButton from "@/back/Components/SubmitButton";
|
|
import { extractIdsFromDatabasePivotRelation } from "@/lib/mapPivotOptions";
|
|
import Slugifycator from "@/back/Components/Inputs/Slugifycator";
|
|
import { ModelSelect } from "@/back/Components/Inputs/ModelSelect";
|
|
|
|
export default function Form({
|
|
model,
|
|
modelName,
|
|
modelRoute,
|
|
action,
|
|
method,
|
|
}: {
|
|
model?: NewsModel;
|
|
modelName: string;
|
|
modelRoute: string;
|
|
action: string;
|
|
method: number;
|
|
}) {
|
|
const { locale } = usePage().props.appEnv as AppEnv;
|
|
const [isGif, setIsGif] = useState<boolean>(false);
|
|
const [dynamicKey, setDynamicKey] = useState<number | null>(null);
|
|
const enumDynamicFieldTypes = usePage().props
|
|
.enumDynamicFieldTypes as DynamicFieldTypes;
|
|
|
|
const enumNewsStatus = usePage().props.enumNewsStatus as LaravelEnum;
|
|
|
|
const defaultTagOptions = extractIdsFromDatabasePivotRelation(model?.tags);
|
|
const defaultCategoriesOptions = extractIdsFromDatabasePivotRelation(
|
|
model?.categories,
|
|
);
|
|
|
|
const isFile = (value: unknown): value is File => {
|
|
return value instanceof File;
|
|
};
|
|
|
|
/** Form section */
|
|
const { post, setData, data, reset } = useForm({
|
|
seo_title: model?.seo_title?.[locale] ?? "",
|
|
seo_description: model?.seo_description?.[locale] ?? "",
|
|
title: model?.title?.[locale] ?? "",
|
|
slug: model?.slug?.[locale] ?? "",
|
|
content: model?.content?.[locale] ?? "",
|
|
picture_alt: model?.picture_alt?.[locale] ?? "",
|
|
picture_title: model?.picture_title?.[locale] ?? "",
|
|
inverted: model?.inverted ?? false,
|
|
has_show_picture: model?.has_show_picture ?? true,
|
|
vimeo_id: model?.vimeo_id ?? "",
|
|
status: model?.status ?? enumNewsStatus.unpublished.value,
|
|
picture: model?.picture,
|
|
tags: model?.tags ? defaultTagOptions : [],
|
|
categories: model?.categories ? defaultCategoriesOptions : [],
|
|
published_at: model?.published_at ? model?.published_at : null,
|
|
unpublished_at: model?.unpublished_at ? model?.unpublished_at : null,
|
|
});
|
|
|
|
const refreshFileFromDb = () => {
|
|
if (model && model.picture) {
|
|
const newIsGifValue =
|
|
typeof model.picture === "string" &&
|
|
model.picture.toLowerCase().endsWith(".gif");
|
|
setIsGif(newIsGifValue);
|
|
setData("picture", model.picture);
|
|
setDynamicKey(Date.now());
|
|
}
|
|
};
|
|
|
|
const submitCrud = useCrudSubmit({
|
|
method,
|
|
modelRoute,
|
|
modelName,
|
|
action,
|
|
model,
|
|
post,
|
|
data,
|
|
});
|
|
|
|
useEffect(() => {
|
|
setIsGif(() => {
|
|
if (model && model?.picture && typeof model.picture === "string") {
|
|
return model.picture.toLowerCase().endsWith(".gif");
|
|
}
|
|
if (isFile(data.picture)) {
|
|
return data.picture.name.toLowerCase().endsWith(".gif");
|
|
}
|
|
return false;
|
|
});
|
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
}, []);
|
|
|
|
return (
|
|
<form onSubmit={submitCrud}>
|
|
<InputError all />
|
|
<FieldsetWrapper
|
|
title={useTrans("back.form_title_seo")}
|
|
icon={<BookTextIcon className="mr-1" />}
|
|
description={useTrans("back.form_subtitle_seo")}
|
|
className="sm:grid-cols-2 gap-4"
|
|
>
|
|
<SimpleString
|
|
name="seo_title"
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.custom.seo.title",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
required
|
|
placeholder="ex: John Doe"
|
|
value={data.seo_title ?? ""}
|
|
setData={setData}
|
|
/>
|
|
<SimpleString
|
|
name="seo_description"
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.custom.seo.description",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
placeholder="ex: John Doe"
|
|
required
|
|
value={data.seo_description ?? ""}
|
|
setData={setData}
|
|
/>
|
|
</FieldsetWrapper>
|
|
<FieldsetWrapper
|
|
title={useTrans("back.form_title_global")}
|
|
icon={<BookTextIcon className="mr-1" />}
|
|
description={useTrans("back.form_subtitle_global")}
|
|
className="space-y-3"
|
|
>
|
|
<div className="w-full grid grid-cols-1 lg:grid-cols-2 items-center sm:grid-cols-2 gap-4">
|
|
<SimpleString
|
|
name="title"
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.attributes.title",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
placeholder="ex: John Doe"
|
|
value={data.title}
|
|
setData={setData}
|
|
/>
|
|
<Slugifycator
|
|
value={data.slug}
|
|
targetValue={data.title}
|
|
setData={setData}
|
|
/>
|
|
</div>
|
|
<GoToFrontView
|
|
modelName={useTrans(`models.classes.${modelName}`, {
|
|
choice: 1,
|
|
})}
|
|
pathField={data.slug}
|
|
additionalGroupPath={str(
|
|
useTrans(`models.classes.${modelName}`, {
|
|
choice: 2,
|
|
}),
|
|
)
|
|
.slug()
|
|
.value()}
|
|
isAvailable={
|
|
model?.status === enumNewsStatus.published.value
|
|
}
|
|
/>
|
|
</FieldsetWrapper>
|
|
<FieldsetWrapper
|
|
title={useTrans("back.form_title_configuration")}
|
|
icon={<CogIcon className="mr-1" />}
|
|
description={useTrans("back.form_subtitle_configuration")}
|
|
className="sm:grid-cols-2 gap-4"
|
|
>
|
|
<SimpleDatePicker
|
|
name="published_at"
|
|
setData={setData}
|
|
date={data.published_at}
|
|
hasTime
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.custom.published_at",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
/>
|
|
<SimpleDatePicker
|
|
name="unpublished_at"
|
|
setData={setData}
|
|
date={data.unpublished_at}
|
|
hasTime
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.custom.unpublished_at",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
/>
|
|
<EnumSelect
|
|
name="status"
|
|
options={enumNewsStatus}
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans("validation.custom.status"),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
placeholder={`ex: ${enumNewsStatus.unpublished.label}`}
|
|
selected={data.status}
|
|
setData={setData}
|
|
/>
|
|
</FieldsetWrapper>
|
|
<FieldsetWrapper
|
|
title={useTrans("back.form_title_display")}
|
|
icon={<BookTextIcon className="mr-1" />}
|
|
description={useTrans("back.form_subtitle_display")}
|
|
className="sm:grid-cols-2 gap-4"
|
|
>
|
|
<div className="flex items-center gap-4">
|
|
<SimpleSwitch
|
|
label="Afficher l'image sur la page ?"
|
|
name="has_show_picture"
|
|
checked={data.has_show_picture}
|
|
setData={setData}
|
|
/>
|
|
<div
|
|
className={cn(
|
|
!data.has_show_picture
|
|
? "hidden"
|
|
: "flex items-center gap-4",
|
|
)}
|
|
>
|
|
<Separator orientation="vertical" className="h-5" />
|
|
<SimpleSwitch
|
|
label="Ou placer l'image ?"
|
|
leftHelper="Gauche"
|
|
rightHelper="Droite"
|
|
name="inverted"
|
|
checked={data.inverted}
|
|
setData={setData}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<Card className="w-full">
|
|
<CardContent className="pt-6 ">
|
|
<div className={cn("grid grid-cols-2 gap-3")}>
|
|
<Skeleton
|
|
className={cn(
|
|
"h-[125px] w-full rounded-xl",
|
|
{ hidden: !data.has_show_picture },
|
|
data.inverted ? "order-2" : "order-1",
|
|
)}
|
|
/>
|
|
<div
|
|
className={cn(
|
|
"space-y-2",
|
|
{ "col-span-2": !data.has_show_picture },
|
|
data.inverted ? "order-1" : "order-2",
|
|
)}
|
|
>
|
|
<Skeleton className="h-5 w-[80%]" />
|
|
<Skeleton className="h-5 w-[75%]" />
|
|
<Skeleton className="h-5 w-[50%]" />
|
|
<Skeleton className="h-5 w-[95%]" />
|
|
<Skeleton className="h-5 w-[79%]" />
|
|
</div>
|
|
</div>
|
|
</CardContent>
|
|
</Card>
|
|
</FieldsetWrapper>
|
|
<FieldsetWrapper
|
|
title={useTrans("back.form_title_content")}
|
|
icon={<BookTextIcon className="mr-1" />}
|
|
description={useTrans("back.form_subtitle_content")}
|
|
className="gap-4"
|
|
>
|
|
<div className="grid sm:grid-cols-2 gap-4 col-span-2 gap-4">
|
|
<div className="flex items-center justify-between gap-2 col-span-1">
|
|
<SimpleSwitch
|
|
name="inputType"
|
|
label="Type d'image"
|
|
leftHelper="Image"
|
|
rightHelper="GIF"
|
|
checked={isGif}
|
|
setData={(_, value) => {
|
|
setIsGif(value);
|
|
if (data.picture) {
|
|
setData((prev) => ({
|
|
...prev,
|
|
picture: null,
|
|
}));
|
|
}
|
|
setDynamicKey(Date.now());
|
|
}}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className="grid sm:grid-cols-2 gap-4 col-span-2 gap-4">
|
|
<CompleteInputFile
|
|
key={dynamicKey}
|
|
fileName="picture"
|
|
modelRoute="news"
|
|
originalData={model?.picture ?? undefined}
|
|
name="picture"
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.attributes.image",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
setData={setData}
|
|
onRefreshFile={refreshFileFromDb}
|
|
// @ts-expect-error Types diff between custom reset and useForm reset.
|
|
reset={reset}
|
|
value={data.picture}
|
|
required
|
|
maxFileSize="5120"
|
|
allowedFormats={isGif ? "gif" : "jpg,jpeg,png,webp"}
|
|
validationRules={
|
|
isGif
|
|
? null
|
|
: {
|
|
width: "525",
|
|
height: "295",
|
|
}
|
|
}
|
|
inputType={
|
|
isGif
|
|
? enumDynamicFieldTypes.file.value
|
|
: enumDynamicFieldTypes.image.value
|
|
}
|
|
/>
|
|
</div>
|
|
<div className="grid sm:grid-cols-2 col-span-2 gap-4">
|
|
<SimpleString
|
|
name="picture_alt"
|
|
required
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.custom.image_alt",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
placeholder="ex: John Doe"
|
|
value={data.picture_alt ?? ""}
|
|
setData={setData}
|
|
/>
|
|
<SimpleString
|
|
name="picture_title"
|
|
required
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.custom.image_title",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
placeholder="ex: John Doe"
|
|
value={data.picture_title ?? ""}
|
|
setData={setData}
|
|
/>
|
|
</div>
|
|
<SimpleWysiwyg
|
|
name="content"
|
|
model="News"
|
|
label={str(
|
|
useTrans("back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"validation.custom.content",
|
|
),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
value={data.content}
|
|
setData={setData}
|
|
/>
|
|
<div className="grid sm:grid-cols-2 gap-4 col-span-2 gap-4">
|
|
<ModelSelect
|
|
targetModel="news_tag"
|
|
label={str(
|
|
useTrans("back.form_associate_to_plural", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"models.classes.news-tag",
|
|
{ choice: 2 },
|
|
),
|
|
},
|
|
choice: 2,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
multiple
|
|
name="tags"
|
|
required={false}
|
|
variant="inverted"
|
|
placeholder={str(useTrans("back.news.tags.placeholder"))
|
|
.ucFirst()
|
|
.value()}
|
|
singleLine={false}
|
|
setData={setData}
|
|
defaultValues={defaultTagOptions}
|
|
responsive={{
|
|
mobile: {
|
|
maxCount: 3,
|
|
},
|
|
tablet: {
|
|
maxCount: 4,
|
|
},
|
|
desktop: {
|
|
maxCount: 5,
|
|
},
|
|
}}
|
|
/>
|
|
<ModelSelect
|
|
targetModel="news_category"
|
|
label={str(
|
|
useTrans("back.form_associate_to_plural", {
|
|
params: {
|
|
attribute: useTrans(
|
|
"models.classes.news-category",
|
|
{ choice: 2 },
|
|
),
|
|
},
|
|
choice: 2,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
multiple
|
|
name="categories"
|
|
required={false}
|
|
variant="inverted"
|
|
placeholder={str(
|
|
useTrans("back.news.categories.placeholder"),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
singleLine={false}
|
|
setData={setData}
|
|
defaultValues={defaultCategoriesOptions}
|
|
responsive={{
|
|
mobile: {
|
|
maxCount: 3,
|
|
},
|
|
tablet: {
|
|
maxCount: 4,
|
|
},
|
|
desktop: {
|
|
maxCount: 5,
|
|
},
|
|
}}
|
|
/>
|
|
</div>
|
|
</FieldsetWrapper>
|
|
<FieldsetWrapper
|
|
title={useTrans("back.form_title_additional_files")}
|
|
icon={<BookTextIcon className="mr-1" />}
|
|
description={useTrans("back.form_subtitle_additional_files")}
|
|
className="sm:grid-cols-2 gap-4"
|
|
>
|
|
<SimpleVimeoId
|
|
name="vimeo_id"
|
|
setData={setData}
|
|
value={data.vimeo_id}
|
|
required={false}
|
|
/>
|
|
</FieldsetWrapper>
|
|
<div className="text-center">
|
|
<SubmitButton
|
|
modelTrans={useTrans(`models.classes.${modelName}`, {
|
|
choice: 1,
|
|
})}
|
|
/>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|