2024-07-03 15:15:52 +00:00
|
|
|
import {
|
|
|
|
|
BackHistoryButton,
|
|
|
|
|
FormDatePickerField,
|
|
|
|
|
FormTextAreaField,
|
|
|
|
|
FormTextField,
|
|
|
|
|
} 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";
|
2024-07-09 16:21:12 +00:00
|
|
|
import { useWarnAboutChange } from "@/lib/hooks";
|
2024-07-12 17:29:28 +00:00
|
|
|
import { Button, Form } from "@/ui";
|
2024-07-09 16:21:12 +00:00
|
|
|
import { ICreateQuote_Request_DTO } from "@shared/contexts";
|
|
|
|
|
import { useEffect } from "react";
|
2024-07-03 15:15:52 +00:00
|
|
|
import { FieldErrors, SubmitErrorHandler, SubmitHandler, useForm } from "react-hook-form";
|
|
|
|
|
import { useNavigate } from "react-router-dom";
|
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 { setWarnWhen } = useWarnAboutChange();
|
|
|
|
|
const { useCreate } = useQuotes();
|
|
|
|
|
const { mutate } = useCreate();
|
2024-06-29 19:39:25 +00:00
|
|
|
|
|
|
|
|
const form = useForm<QuoteDataForm>({
|
|
|
|
|
defaultValues: {
|
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-06-29 19:39:25 +00:00
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
|
2024-07-09 16:21:12 +00:00
|
|
|
const { watch, handleSubmit } = form;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
const subscription = watch((values: any, { type }: { type?: any }) => {
|
|
|
|
|
if (type === "change") {
|
|
|
|
|
// Hay cambios en el formulario
|
|
|
|
|
//setWarnWhen(true);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
return () => subscription.unsubscribe();
|
|
|
|
|
}, [watch, setWarnWhen]);
|
|
|
|
|
|
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-09 16:21:12 +00:00
|
|
|
setWarnWhen(false);
|
2024-07-03 15:15:52 +00:00
|
|
|
mutate(formData, {
|
|
|
|
|
onSuccess: (data) => {
|
2024-07-11 17:43:08 +00:00
|
|
|
navigate(`/quotes/edit/${data.id}`, { relative: "path" });
|
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
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-03 15:15:52 +00:00
|
|
|
const onErrors: SubmitErrorHandler<QuoteDataForm> = async (
|
|
|
|
|
errors: FieldErrors<QuoteDataForm>
|
|
|
|
|
) => {
|
|
|
|
|
console.log(errors);
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-29 19:39:25 +00:00
|
|
|
return (
|
|
|
|
|
<Form {...form}>
|
2024-07-09 16:21:12 +00:00
|
|
|
<form onSubmit={handleSubmit(onSubmit, onErrors)}>
|
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'>
|
2024-07-12 17:29:28 +00:00
|
|
|
<Button size='sm' variant={"outline"} url='/quotes'>
|
|
|
|
|
{t("quotes.create.buttons.discard")}
|
|
|
|
|
</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>
|
|
|
|
|
);
|
|
|
|
|
};
|