add shipments create and form create
This commit is contained in:
@@ -11,13 +11,8 @@ export type Productor = {
|
||||
export type Shipment = {
|
||||
name: string;
|
||||
date: string;
|
||||
id: number;
|
||||
}
|
||||
|
||||
export type ShipmentCreate = {
|
||||
name: string;
|
||||
date: string;
|
||||
product_ids?: number[];
|
||||
id?: number;
|
||||
form_id: number;
|
||||
}
|
||||
|
||||
export type Form = {
|
||||
@@ -38,7 +33,17 @@ export type FormCreate = {
|
||||
end: string;
|
||||
productor_id: number;
|
||||
referer_id: number;
|
||||
shipments: Shipment[];
|
||||
shipment_ids: Shipment[];
|
||||
}
|
||||
|
||||
export type FormEdit = {
|
||||
name?: string | null;
|
||||
season?: string | null;
|
||||
start?: string | null;
|
||||
end?: string | null;
|
||||
productor_id?: number | null;
|
||||
referer_id?: number | null;
|
||||
shipment_ids: Shipment[];
|
||||
}
|
||||
|
||||
export type Product = {
|
||||
@@ -63,7 +68,8 @@ export function getForm(id: number): UseQueryResult<Form, Error> {
|
||||
return useQuery<Form>({
|
||||
queryKey: ['form'],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/forms/${id}`).then((res) => res.json())
|
||||
fetch(`${Config.backend_uri}/forms/${id}`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
@@ -114,14 +120,39 @@ export function createShipment() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (newShipment: ShipmentCreate) => {
|
||||
mutationFn: (newShipment: Shipment) => {
|
||||
return fetch(`${Config.backend_uri}/shipments`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(newShipment),
|
||||
});
|
||||
// TODO change product ids hardcode here
|
||||
body: JSON.stringify({...newShipment, product_ids: []}),
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['shipments'] })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export type ShipmentEditPayload = {
|
||||
id: number;
|
||||
shipment: Shipment;
|
||||
}
|
||||
|
||||
export function editShipment() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({id, shipment}: ShipmentEditPayload) => {
|
||||
return fetch(`${Config.backend_uri}/shipments/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify({...shipment}),
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['shipments'] })
|
||||
@@ -140,7 +171,7 @@ export function createProductor() {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(newProductor),
|
||||
});
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['productors'] })
|
||||
@@ -159,10 +190,51 @@ export function createForm() {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(newForm),
|
||||
});
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['forms'] })
|
||||
}
|
||||
})
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteForm() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => {
|
||||
return fetch(`${Config.backend_uri}/forms/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['forms'] })
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export type FormEditPayload = {
|
||||
id: number;
|
||||
form: FormEdit;
|
||||
}
|
||||
|
||||
export function editForm() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({id, form}: FormEditPayload) => {
|
||||
return fetch(`${Config.backend_uri}/forms/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(form),
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['forms'] })
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user