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

127 lines
3.7 KiB
TypeScript
Raw Normal View History

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 { ChevronLeft } from "lucide-react";
import { SubmitButton } from "@/components";
2024-07-01 17:12:15 +00:00
import { Button, Form } from "@/ui";
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";
type QuoteDataForm = {
id: string;
status: string;
date: string;
reference: string;
customer_information: string;
lang_code: string;
currency_code: string;
payment_method: string;
notes: string;
validity: string;
items: any[];
};
2024-07-01 17:12:15 +00:00
/*type QuoteCreateProps = {
2024-06-29 19:39:25 +00:00
isOverModal?: boolean;
2024-07-01 17:12:15 +00:00
};*/
2024-06-29 19:39:25 +00:00
2024-07-01 17:12:15 +00:00
export const QuoteCreate = () => {
//const [loading, setLoading] = useState(false);
2024-06-29 19:39:25 +00:00
2024-07-01 17:12:15 +00:00
//const { data: userIdentity } = useGetIdentity();
//console.log(userIdentity);
2024-06-29 19:39:25 +00:00
2024-07-03 15:15:52 +00:00
const navigate = useNavigate();
2024-07-01 17:12:15 +00:00
const { useMutation } = useQuotes();
2024-07-03 15:15:52 +00:00
const { mutate } = useMutation();
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-01 17:12:15 +00:00
const onSubmit: SubmitHandler<QuoteDataForm> = async (formData) => {
alert(JSON.stringify(formData));
2024-06-29 19:39:25 +00:00
try {
2024-07-01 17:12:15 +00:00
//setLoading(true);
2024-07-03 15:15:52 +00:00
mutate(formData, {
onSuccess: (data) => {
navigate(`/quotes/edit/${data.id}`, { relative: "path", replace: true });
},
});
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-03 15:15:52 +00:00
<form onSubmit={form.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'>
<Button variant='outline' size='icon' className='h-7 w-7'>
<ChevronLeft className='w-4 h-4' />
<span className='sr-only'>{t("quotes.common.back")}</span>
</Button>
<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>
<div className='grid max-w-lg gap-6'>
<FormTextField
2024-07-03 15:15:52 +00:00
className='row-span-2'
name='reference'
required
2024-07-01 17:12:15 +00:00
label={t("quotes.create.form_fields.reference.label")}
description={t("quotes.create.form_fields.reference.desc")}
placeholder={t("quotes.create.form_fields.reference.placeholder")}
/>
2024-07-03 15:15:52 +00:00
2024-07-01 17:12:15 +00:00
<FormDatePickerField
required
label={t("quotes.create.form_fields.date.label")}
description={t("quotes.create.form_fields.date.desc")}
placeholder={t("quotes.create.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
label={t("quotes.create.form_fields.customer_information.label")}
description={t("quotes.create.form_fields.customer_information.desc")}
placeholder={t("quotes.create.form_fields.customer_information.placeholder")}
/>
2024-06-29 19:39:25 +00:00
</div>
2024-07-01 17:12:15 +00:00
2024-07-03 15:15:52 +00:00
<div className='flex items-center justify-start gap-2'>
<BackHistoryButton size='sm' label={t("quotes.create.buttons.discard")} url='/quotes' />
2024-07-01 17:12:15 +00:00
<SubmitButton size='sm' label={t("common.continue")}></SubmitButton>
2024-06-29 19:39:25 +00:00
</div>
</div>
</form>
</Form>
);
};