Presupuestador_web/client/src/app/quotes/create.tsx

142 lines
4.1 KiB
TypeScript
Raw Normal View History

2024-07-03 15:15:52 +00:00
import {
BackHistoryButton,
FormDatePickerField,
FormTextAreaField,
FormTextField,
LoadingOverlay,
2024-07-03 15:15:52 +00:00
} from "@/components";
2024-07-01 17:12:15 +00:00
import { t } from "i18next";
2024-06-29 19:39:25 +00:00
import { SubmitButton } from "@/components";
import { useUnsavedChangesNotifier } from "@/lib/hooks";
2024-07-12 17:29:28 +00:00
import { Button, Form } from "@/ui";
2024-08-13 12:03:55 +00:00
import { joiResolver } from "@hookform/resolvers/joi";
2024-07-09 16:21:12 +00:00
import { ICreateQuote_Request_DTO } from "@shared/contexts";
2024-08-13 12:03:55 +00:00
import Joi from "joi";
import { useMemo } from "react";
import { SubmitHandler, useForm } from "react-hook-form";
2024-07-03 15:15:52 +00:00
import { useNavigate } from "react-router-dom";
import { toast } from "react-toastify";
2024-06-29 19:39:25 +00:00
import { useQuotes } from "./hooks";
2024-07-09 16:21:12 +00:00
interface QuoteDataForm extends ICreateQuote_Request_DTO {}
2024-06-29 19:39:25 +00:00
2024-07-01 17:12:15 +00:00
export const QuoteCreate = () => {
2024-07-03 15:15:52 +00:00
const navigate = useNavigate();
2024-07-09 16:21:12 +00:00
const { useCreate } = useQuotes();
2024-08-13 12:03:55 +00:00
const { mutate, isPending } = useCreate();
2024-06-29 19:39:25 +00:00
2024-08-13 12:03:55 +00:00
const defaultValues = useMemo(
() => ({
2024-07-03 15:15:52 +00:00
date: new Date(Date.now()).toUTCString(),
2024-06-29 19:39:25 +00:00
customer_information: "",
2024-07-03 15:15:52 +00:00
reference: "",
2024-08-13 12:03:55 +00:00
}),
[]
);
const form = useForm<QuoteDataForm>({
mode: "onBlur",
defaultValues,
resolver: joiResolver(
Joi.object({
reference: Joi.string().required(),
date: Joi.string().required(),
customer_information: Joi.string().required(),
}),
{
//messages: SpanishJoiMessages,
}
),
2024-06-29 19:39:25 +00:00
});
const { formState, reset, getValues, handleSubmit } = form;
const { isSubmitting, isDirty } = formState;
2024-07-09 16:21:12 +00:00
useUnsavedChangesNotifier({
isDirty,
});
2024-07-09 16:21:12 +00:00
2024-07-01 17:12:15 +00:00
const onSubmit: SubmitHandler<QuoteDataForm> = async (formData) => {
2024-06-29 19:39:25 +00:00
try {
2024-07-03 15:15:52 +00:00
mutate(formData, {
onError: (error) => {
console.debug(error);
toast.error(error.message);
},
2024-07-03 15:15:52 +00:00
onSuccess: (data) => {
reset(getValues());
2024-08-12 11:26:10 +00:00
toast.success("Cotización guardada");
2024-07-17 18:10:07 +00:00
navigate(`/quotes/edit/${data.id}`, { relative: "path", replace: true });
2024-07-03 15:15:52 +00:00
},
});
2024-06-29 19:39:25 +00:00
} finally {
2024-07-01 17:12:15 +00:00
//setLoading(false);
2024-06-29 19:39:25 +00:00
}
};
if (isSubmitting) {
return <LoadingOverlay title='Guardando ajustes' />;
}
2024-08-13 12:03:55 +00:00
/*if (isError) {
return <ErrorOverlay errorMessage={mutateError?.message} />;
2024-08-13 12:03:55 +00:00
}*/
if (isPending) {
return <LoadingOverlay />;
}
2024-07-03 15:15:52 +00:00
2024-06-29 19:39:25 +00:00
return (
<Form {...form}>
<form onSubmit={handleSubmit(onSubmit)}>
2024-06-29 19:39:25 +00:00
<div className='mx-auto grid max-w-[90rem] flex-1 auto-rows-max gap-6'>
<div className='flex items-center gap-4'>
2024-07-11 17:43:08 +00:00
<BackHistoryButton />
2024-06-29 19:39:25 +00:00
<h1 className='flex-1 text-xl font-semibold tracking-tight shrink-0 whitespace-nowrap sm:grow-0'>
{t("quotes.create.title")}
</h1>
2024-07-01 17:12:15 +00:00
</div>
2024-07-09 16:21:12 +00:00
<div className='grid w-6/12 gap-6 mx-auto'>
2024-07-01 17:12:15 +00:00
<FormTextField
2024-07-03 15:15:52 +00:00
className='row-span-2'
name='reference'
required
2024-07-11 16:40:46 +00:00
label={t("quotes.form_fields.reference.label")}
description={t("quotes.form_fields.reference.desc")}
placeholder={t("quotes.form_fields.reference.placeholder")}
2024-07-01 17:12:15 +00:00
/>
2024-07-03 15:15:52 +00:00
2024-07-01 17:12:15 +00:00
<FormDatePickerField
required
2024-07-11 16:40:46 +00:00
label={t("quotes.form_fields.date.label")}
description={t("quotes.form_fields.date.desc")}
placeholder={t("quotes.form_fields.date.placeholder")}
2024-07-03 15:15:52 +00:00
name='date'
2024-07-01 17:12:15 +00:00
/>
2024-07-03 15:15:52 +00:00
<FormTextAreaField
rows={4}
className='row-span-2'
name='customer_information'
required
2024-07-11 16:40:46 +00:00
label={t("quotes.form_fields.customer_information.label")}
description={t("quotes.form_fields.customer_information.desc")}
placeholder={t("quotes.form_fields.customer_information.placeholder")}
2024-07-03 15:15:52 +00:00
/>
2024-07-01 17:12:15 +00:00
2024-07-09 16:21:12 +00:00
<div className='flex items-center justify-around gap-2'>
<Button size='sm' variant={"outline"} onClick={() => navigate("/quotes")}>
{t("common.discard")}
2024-07-12 17:29:28 +00:00
</Button>
2024-07-01 17:12:15 +00:00
2024-07-09 16:21:12 +00:00
<SubmitButton size='sm' label={t("common.continue")}></SubmitButton>
</div>
2024-06-29 19:39:25 +00:00
</div>
</div>
</form>
</Form>
);
};