[WIP] front api exchanges
This commit is contained in:
@@ -1,10 +1,168 @@
|
||||
import { useQuery } from "@tanstack/react-query";
|
||||
import { useMutation, useQuery, useQueryClient,type UseQueryResult } from "@tanstack/react-query";
|
||||
import { Config } from "../config/config";
|
||||
|
||||
export function getForm(id: number) {
|
||||
return useQuery({
|
||||
export type Productor = {
|
||||
id: number;
|
||||
name: string;
|
||||
address: string;
|
||||
payment: string;
|
||||
}
|
||||
|
||||
export type Shipment = {
|
||||
name: string;
|
||||
date: string;
|
||||
id: number;
|
||||
}
|
||||
|
||||
export type ShipmentCreate = {
|
||||
name: string;
|
||||
date: string;
|
||||
product_ids?: number[];
|
||||
}
|
||||
|
||||
export type Form = {
|
||||
id: number;
|
||||
name: string;
|
||||
season: string;
|
||||
start: string;
|
||||
end: string;
|
||||
productor: Productor;
|
||||
referer: User;
|
||||
shipments: Shipment[];
|
||||
}
|
||||
|
||||
export type FormCreate = {
|
||||
name: string;
|
||||
season: string;
|
||||
start: string;
|
||||
end: string;
|
||||
productor_id: number;
|
||||
referer_id: number;
|
||||
shipments: Shipment[];
|
||||
}
|
||||
|
||||
export type Product = {
|
||||
id: number;
|
||||
productor: Productor;
|
||||
name: string;
|
||||
unit: number;
|
||||
price: number;
|
||||
priceKg: number | null;
|
||||
weight: number;
|
||||
type: number;
|
||||
}
|
||||
|
||||
export type User = {
|
||||
id: number;
|
||||
name: string;
|
||||
email: string;
|
||||
products: Product[];
|
||||
}
|
||||
|
||||
export function getForm(id: number): UseQueryResult<Form, Error> {
|
||||
return useQuery<Form>({
|
||||
queryKey: ['form'],
|
||||
queryFn: () => (
|
||||
fetch(`http://localhost:8000/forms/${id}`).then((res) => res.json())
|
||||
fetch(`${Config.backend_uri}/forms/${id}`).then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function getForms(filters?: URLSearchParams): UseQueryResult<Form[], Error> {
|
||||
const queryString = filters?.toString()
|
||||
return useQuery<Form[]>({
|
||||
queryKey: ['forms', queryString],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/forms${filters ? `?${queryString}` : ""}`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function getShipments() {
|
||||
return useQuery<Shipment[]>({
|
||||
queryKey: ['shipments'],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/shipments`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function getProductors() {
|
||||
return useQuery<Productor[]>({
|
||||
queryKey: ['productors'],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/productors`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
export function getUsers() {
|
||||
return useQuery<User[]>({
|
||||
queryKey: ['users'],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/users`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function createShipment() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (newShipment: ShipmentCreate) => {
|
||||
return fetch(`${Config.backend_uri}/shipments`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(newShipment),
|
||||
});
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['shipments'] })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function createProductor() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (newProductor: Productor) => {
|
||||
return fetch(`${Config.backend_uri}/productors`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(newProductor),
|
||||
});
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['productors'] })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function createForm() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (newForm: FormCreate) => {
|
||||
return fetch(`${Config.backend_uri}/forms`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(newForm),
|
||||
});
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['forms'] })
|
||||
}
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user