add shipment forms and start contract from form
This commit is contained in:
@@ -6,16 +6,29 @@ import type { Productor, ProductorCreate, ProductorEditPayload } from "@/service
|
||||
import type { User, UserCreate, UserEditPayload } from "@/services/resources/users";
|
||||
import type { Product, ProductCreate, ProductEditPayload } from "./resources/products";
|
||||
|
||||
export function getShipments() {
|
||||
export function getShipments(filters?: URLSearchParams): UseQueryResult<Shipment[], Error> {
|
||||
const queryString = filters?.toString()
|
||||
return useQuery<Shipment[]>({
|
||||
queryKey: ['shipments'],
|
||||
queryKey: ['shipments', queryString],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/shipments`)
|
||||
fetch(`${Config.backend_uri}/shipments${filters ? `?${queryString}` : ""}`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function getShipment(id?: number, options?: any): UseQueryResult<Shipment, Error> {
|
||||
return useQuery<Shipment>({
|
||||
queryKey: ['shipment'],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/shipments/${id}`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
enabled: !!id,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
export function createShipment() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
@@ -26,8 +39,7 @@ export function createShipment() {
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
// TODO change product ids hardcode here
|
||||
body: JSON.stringify({...newShipment, product_ids: []}),
|
||||
body: JSON.stringify(newShipment),
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
@@ -40,13 +52,13 @@ export function editShipment() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({id, shipment}: ShipmentEditPayload) => {
|
||||
mutationFn: ({shipment, id}: ShipmentEditPayload) => {
|
||||
return fetch(`${Config.backend_uri}/shipments/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({...shipment}),
|
||||
body: JSON.stringify(shipment),
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
@@ -55,6 +67,23 @@ export function editShipment() {
|
||||
})
|
||||
}
|
||||
|
||||
export function deleteShipment() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => {
|
||||
return fetch(`${Config.backend_uri}/shipments/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['shipments'] })
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function getProductors(filters?: URLSearchParams): UseQueryResult<Productor[], Error> {
|
||||
const queryString = filters?.toString()
|
||||
return useQuery<Productor[]>({
|
||||
|
||||
@@ -43,5 +43,4 @@ export type FormInputs = {
|
||||
end: string | null;
|
||||
productor_id: string;
|
||||
referer_id: string;
|
||||
shipments: ShipmentInputs[];
|
||||
}
|
||||
@@ -1,9 +1,12 @@
|
||||
import type { Product } from "./products";
|
||||
|
||||
export type Productor = {
|
||||
id: number;
|
||||
name: string;
|
||||
address: string;
|
||||
payment: string;
|
||||
type: string;
|
||||
products: Product[]
|
||||
}
|
||||
|
||||
export type ProductorCreate = {
|
||||
|
||||
@@ -19,42 +19,42 @@ export type Product = {
|
||||
id: number;
|
||||
productor: Productor;
|
||||
name: string;
|
||||
unit: number;
|
||||
unit: string;
|
||||
price: number;
|
||||
price_kg: number | null;
|
||||
weight: number;
|
||||
type: number;
|
||||
type: string;
|
||||
shipments: Shipment[];
|
||||
}
|
||||
|
||||
export type ProductCreate = {
|
||||
productor_id: number;
|
||||
name: string;
|
||||
unit: number;
|
||||
unit: string;
|
||||
price: number;
|
||||
price_kg: number | null;
|
||||
weight: number | null;
|
||||
type: number;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export type ProductEdit = {
|
||||
productor_id: number | null;
|
||||
name: string | null;
|
||||
unit: number | null;
|
||||
unit: string | null;
|
||||
price: number | null;
|
||||
price_kg: number | null;
|
||||
weight: number | null;
|
||||
type: number | null;
|
||||
type: string | null;
|
||||
}
|
||||
|
||||
export type ProductInputs = {
|
||||
productor_id: number | null;
|
||||
productor_id: string | null;
|
||||
name: string;
|
||||
unit: number | null;
|
||||
unit: string | null;
|
||||
price: number | null;
|
||||
price_kg: number | null;
|
||||
weight: number | null;
|
||||
type: number | null;
|
||||
type: string | null;
|
||||
}
|
||||
|
||||
export type ProductEditPayload = {
|
||||
@@ -64,7 +64,7 @@ export type ProductEditPayload = {
|
||||
|
||||
export function productToProductInputs(product: Product): ProductInputs {
|
||||
return {
|
||||
productor_id: product.productor.id,
|
||||
productor_id: String(product.productor.id),
|
||||
name: product.name,
|
||||
unit: product.unit,
|
||||
price: product.price,
|
||||
@@ -76,7 +76,7 @@ export function productToProductInputs(product: Product): ProductInputs {
|
||||
|
||||
export function productCreateFromProductInputs(productInput: ProductInputs): ProductCreate {
|
||||
return {
|
||||
productor_id: productInput.productor_id!,
|
||||
productor_id: Number(productInput.productor_id)!,
|
||||
name: productInput.name,
|
||||
unit: productInput.unit!,
|
||||
price: productInput.price!,
|
||||
|
||||
@@ -1,20 +1,27 @@
|
||||
import type { Form } from "./forms";
|
||||
import type { Product } from "./products";
|
||||
|
||||
export type Shipment = {
|
||||
name: string;
|
||||
date: string;
|
||||
id: number;
|
||||
form: Form;
|
||||
form_id: number;
|
||||
products: Product[];
|
||||
}
|
||||
|
||||
export type ShipmentCreate = {
|
||||
name: string;
|
||||
date: string;
|
||||
form_id: number;
|
||||
product_ids: number[];
|
||||
}
|
||||
|
||||
export type ShipmentEdit = {
|
||||
name: string | null;
|
||||
date: string | null;
|
||||
form_id: number | null;
|
||||
product_ids: number[];
|
||||
}
|
||||
|
||||
export type ShipmentEditPayload = {
|
||||
@@ -25,6 +32,23 @@ export type ShipmentEditPayload = {
|
||||
export type ShipmentInputs = {
|
||||
name: string | null;
|
||||
date: string | null;
|
||||
id: number | null;
|
||||
form_id: number | null;
|
||||
form_id: string | null;
|
||||
product_ids: string[];
|
||||
}
|
||||
|
||||
export function shipmentToShipmentInputs(shipment: Shipment): ShipmentInputs {
|
||||
return {
|
||||
...shipment,
|
||||
form_id: String(shipment.form_id),
|
||||
product_ids: shipment.products.map((el) => (String(el.id)))
|
||||
};
|
||||
}
|
||||
|
||||
export function shipmentCreateFromShipmentInputs(shipmentInput: ShipmentInputs): ShipmentCreate {
|
||||
return {
|
||||
name: shipmentInput.name!,
|
||||
date: shipmentInput.date!,
|
||||
form_id: Number(shipmentInput.form_id),
|
||||
product_ids: shipmentInput.product_ids.map(el => (Number(el))),
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user