137 lines
3.8 KiB
TypeScript
137 lines
3.8 KiB
TypeScript
// * Packages
|
|
import { useForm, usePage } from "@inertiajs/react";
|
|
import { BookTextIcon, ExternalLinkIcon } from "lucide-react";
|
|
// * Components shadcn
|
|
//
|
|
// * Other
|
|
import FieldsetWrapper from "@/back/Components/FieldsetWrapper";
|
|
import useTrans from "@/hooks/use-translations";
|
|
import InputError from "@/back/Components/InputError";
|
|
import { GitlabProjectModel } from "@/back/types/project";
|
|
import SubmitButton from "@/back/Components/SubmitButton";
|
|
import useCrudSubmit from "@/back/hooks/use-crud-submit";
|
|
import { AppEnv } from "@/types/global";
|
|
import SimpleString from "@/back/Components/Inputs/SimpleString";
|
|
import str from "@/hooks/use-string";
|
|
import { GetTranslation, isAValidUrl } from "@/lib/utils";
|
|
import BtnOptionnalLinkWithTooltip from "@/back/Components/BtnOptionnalLinkWithTooltip";
|
|
import SimpleNumber from "@/back/Components/Inputs/SimpleNumber";
|
|
|
|
export default function Form({
|
|
model,
|
|
modelName,
|
|
modelRoute,
|
|
action,
|
|
method,
|
|
}: {
|
|
model?: GitlabProjectModel;
|
|
modelName: string;
|
|
modelRoute: string;
|
|
action: string;
|
|
method: number;
|
|
}) {
|
|
const { appEnv } = usePage().props as unknown as {
|
|
appEnv: AppEnv;
|
|
};
|
|
|
|
/** Form section */
|
|
const { post, setData, data } = useForm({
|
|
name: model?.name ? model?.name[appEnv.locale] : undefined,
|
|
url: model?.url ?? undefined,
|
|
gitlab_id: model?.gitlab_id ?? undefined,
|
|
});
|
|
|
|
const submit = useCrudSubmit({
|
|
method,
|
|
modelRoute,
|
|
modelName,
|
|
action,
|
|
model,
|
|
post,
|
|
data,
|
|
modelNameTrans: useTrans(`models.classes.${modelName}`, {
|
|
choice: 1,
|
|
}),
|
|
});
|
|
|
|
return (
|
|
<form id="config-form" onSubmit={submit}>
|
|
<InputError all />
|
|
<FieldsetWrapper
|
|
title={useTrans("uwil-backend::back.form_title_global")}
|
|
icon={<BookTextIcon className="mr-1" />}
|
|
description={useTrans("uwil-backend::back.form_subtitle_global")}
|
|
className="sm:grid-cols-2 gap-4"
|
|
>
|
|
<SimpleString
|
|
name="name"
|
|
label={str(
|
|
useTrans("uwil-backend::back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans("validation.custom.name"),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
placeholder="ex: John Doe"
|
|
value={data.name}
|
|
setData={setData}
|
|
/>
|
|
<SimpleNumber
|
|
name="gitlab_id"
|
|
label={str(
|
|
useTrans("uwil-backend::back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans("validation.custom.gitlab_id"),
|
|
},
|
|
choice: 1,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
placeholder="ex: 100"
|
|
value={data.gitlab_id}
|
|
setData={setData}
|
|
/>
|
|
<div className="flex justify-center items-end gap-1.5">
|
|
<SimpleString
|
|
name="url"
|
|
label={str(
|
|
useTrans("uwil-backend::back.form_associate_to", {
|
|
params: {
|
|
attribute: useTrans("validation.custom.url"),
|
|
},
|
|
choice: 2,
|
|
}),
|
|
)
|
|
.ucFirst()
|
|
.value()}
|
|
type="url"
|
|
placeholder="ex: https://example.com/"
|
|
value={data.url}
|
|
setData={setData}
|
|
/>
|
|
{isAValidUrl(data.url) && (
|
|
<BtnOptionnalLinkWithTooltip
|
|
content={<ExternalLinkIcon />}
|
|
tooltipContent={GetTranslation("back.access_url")}
|
|
link={data.url}
|
|
externalLink
|
|
type="button"
|
|
size="icon"
|
|
/>
|
|
)}
|
|
</div>
|
|
</FieldsetWrapper>
|
|
<div className="text-center">
|
|
<SubmitButton
|
|
modelTrans={useTrans(`models.classes.${modelName}`, {
|
|
choice: 1,
|
|
})}
|
|
/>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|