add shipment forms and start contract from form

This commit is contained in:
2026-02-13 01:12:42 +01:00
parent fe27595931
commit ef7403f213
20 changed files with 553 additions and 312 deletions

View File

@@ -1,14 +1,11 @@
import { ActionIcon, Button, Collapse, Group, Modal, NumberInput, Select, TextInput, type ModalBaseProps } from "@mantine/core";
import { Button, Group, Modal, Select, TextInput, type ModalBaseProps } from "@mantine/core";
import { t } from "@/config/i18n";
import { DatePickerInput } from "@mantine/dates";
import { IconCancel, IconChevronDown, IconChevronUp } from "@tabler/icons-react";
import { IconCancel } from "@tabler/icons-react";
import { getProductors, getUsers } from "@/services/api";
import { useForm } from "@mantine/form";
import { useCallback, useEffect, useMemo } from "react";
import { useDisclosure } from "@mantine/hooks";
import { useEffect, useMemo } from "react";
import type { Form, FormInputs } from "@/services/resources/forms";
import type { ShipmentInputs } from "@/services/resources/shipments";
import ShipmentForm from "@/components/Shipments/Form";
export type FormModalProps = ModalBaseProps & {
currentForm?: Form;
@@ -31,7 +28,6 @@ export default function FormModal({
end: null,
productor_id: "",
referer_id: "",
shipments: [],
},
validate: {
name: (value) =>
@@ -55,7 +51,6 @@ export default function FormModal({
...currentForm,
start: currentForm.start || null,
end: currentForm.end || null,
shipments: currentForm.shipments || [],
productor_id: String(currentForm.productor.id),
referer_id: String(currentForm.referer.id)
});
@@ -70,27 +65,6 @@ export default function FormModal({
return productors?.map(prod => ({value: String(prod.id), label: `${prod.name}`}))
}, [productors])
const [openedShipents, { toggle: toggleShipments }] = useDisclosure(true);
const editShipmentElement = useCallback((
index: number,
shipment: ShipmentInputs
) => {
form.setFieldValue('shipments', (prev) => {
return prev.map((elem, id) => {
if (id === index)
return {...shipment}
return elem;
})
});
}, [form])
const deleteShipmentElement = useCallback((index: number) => {
form.setFieldValue('shipments', (prev) => {
return prev.filter((_, i) => i === index)
});
}, [form])
return (
<Modal
size="50%"
@@ -146,50 +120,6 @@ export default function FormModal({
data={productorsSelect || []}
{...form.getInputProps('productor_id')}
/>
<Group align="end">
<NumberInput
label={t("number of shipment", {capfirst: true})}
placeholder={t("number of shipment", {capfirst: true})}
radius="sm"
withAsterisk
flex="2"
value={form.getValues().shipments.length}
defaultValue={form.getValues().shipments.length}
onChange={(value: number | string) => {
const target = Number(value);
form.setFieldValue('shipments', (prev) => {
const itemsToAdd = Array.from(
{length: target - prev.length},
() => ({name: "", date: "", form_id: null, id: null})
);
return (
target > prev.length ?
[...prev, ...itemsToAdd] :
prev.slice(0, target)
);
})
}}
/>
<ActionIcon
onClick={toggleShipments}
disabled={form.getValues().shipments.length === 0}
>
{openedShipents ? <IconChevronUp/> : <IconChevronDown/>}
</ActionIcon>
</Group>
<Collapse in={openedShipents}>
{
form.getValues().shipments.map((value, index) =>
<ShipmentForm
key={index}
index={index}
setShipmentElement={editShipmentElement}
deleteShipmentElement={deleteShipmentElement}
shipment={value}
/>
)
}
</Collapse>
<Group mt="sm" justify="space-between">
<Button
variant="filled"