add i18n, products, productors and forms as tables
This commit is contained in:
@@ -1,85 +1,16 @@
|
||||
import { useMutation, useQuery, useQueryClient,type UseQueryResult } from "@tanstack/react-query";
|
||||
import { Config } from "../config/config";
|
||||
import { Config } from "@/config/config";
|
||||
import type { Form, FormCreate, FormEditPayload } from "@/services/resources/forms";
|
||||
import type { Shipment, ShipmentCreate, ShipmentEditPayload } from "@/services/resources/shipments";
|
||||
import type { Productor, ProductorCreate, ProductorEditPayload } from "@/services/resources/productors";
|
||||
import type { User } from "@/services/resources/users";
|
||||
import type { Product, ProductCreate, ProductEditPayload } from "./resources/products";
|
||||
|
||||
export type Productor = {
|
||||
id: number;
|
||||
name: string;
|
||||
address: string;
|
||||
payment: string;
|
||||
}
|
||||
|
||||
export type Shipment = {
|
||||
name: string;
|
||||
date: string;
|
||||
id?: number;
|
||||
form_id: 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;
|
||||
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 = {
|
||||
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'],
|
||||
export function getUsers() {
|
||||
return useQuery<User[]>({
|
||||
queryKey: ['users'],
|
||||
queryFn: () => (
|
||||
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}` : ""}`)
|
||||
fetch(`${Config.backend_uri}/users`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
@@ -95,32 +26,11 @@ export function getShipments() {
|
||||
});
|
||||
}
|
||||
|
||||
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: Shipment) => {
|
||||
mutationFn: (newShipment: ShipmentCreate) => {
|
||||
return fetch(`${Config.backend_uri}/shipments`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -136,11 +46,6 @@ export function createShipment() {
|
||||
})
|
||||
}
|
||||
|
||||
export type ShipmentEditPayload = {
|
||||
id: number;
|
||||
shipment: Shipment;
|
||||
}
|
||||
|
||||
export function editShipment() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
@@ -160,11 +65,32 @@ export function editShipment() {
|
||||
})
|
||||
}
|
||||
|
||||
export function getProductors(filters?: URLSearchParams) {
|
||||
const queryString = filters?.toString()
|
||||
return useQuery<Productor[]>({
|
||||
queryKey: ['productors', filters],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/productors${filters ? `?${queryString}` : ""}`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function getProductor(id: number) {
|
||||
return useQuery<Productor>({
|
||||
queryKey: ['productor'],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/productors/${id}`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function createProductor() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (newProductor: Productor) => {
|
||||
mutationFn: (newProductor: ProductorCreate) => {
|
||||
return fetch(`${Config.backend_uri}/productors`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
@@ -179,6 +105,63 @@ export function createProductor() {
|
||||
})
|
||||
}
|
||||
|
||||
export function editProductor() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({productor, id}: ProductorEditPayload) => {
|
||||
return fetch(`${Config.backend_uri}/productors/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(productor),
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['productors'] })
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export function deleteProductor() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => {
|
||||
return fetch(`${Config.backend_uri}/productors/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['productors'] })
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function getForm(id: number): UseQueryResult<Form, Error> {
|
||||
return useQuery<Form>({
|
||||
queryKey: ['form'],
|
||||
queryFn: () => (
|
||||
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 createForm() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
@@ -215,11 +198,6 @@ export function deleteForm() {
|
||||
});
|
||||
}
|
||||
|
||||
export type FormEditPayload = {
|
||||
id: number;
|
||||
form: FormEdit;
|
||||
}
|
||||
|
||||
export function editForm() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
@@ -238,3 +216,79 @@ export function editForm() {
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function getProduct(id: number): UseQueryResult<Product, Error> {
|
||||
return useQuery<Product>({
|
||||
queryKey: ['product'],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/products/${id}`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function getProducts(filters?: URLSearchParams): UseQueryResult<Product[], Error> {
|
||||
const queryString = filters?.toString()
|
||||
return useQuery<Product[]>({
|
||||
queryKey: ['products', queryString],
|
||||
queryFn: () => (
|
||||
fetch(`${Config.backend_uri}/products${filters ? `?${queryString}` : ""}`)
|
||||
.then((res) => res.json())
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function createProduct() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: (newProduct: ProductCreate) => {
|
||||
return fetch(`${Config.backend_uri}/products`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(newProduct),
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['products'] })
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function deleteProduct() {
|
||||
const queryClient = useQueryClient()
|
||||
return useMutation({
|
||||
mutationFn: (id: number) => {
|
||||
return fetch(`${Config.backend_uri}/products/${id}`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['products'] })
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function editProduct() {
|
||||
const queryClient = useQueryClient()
|
||||
|
||||
return useMutation({
|
||||
mutationFn: ({id, product}: ProductEditPayload) => {
|
||||
return fetch(`${Config.backend_uri}/products/${id}`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json"
|
||||
},
|
||||
body: JSON.stringify(product),
|
||||
}).then((res) => res.json());
|
||||
},
|
||||
onSuccess: async () => {
|
||||
await queryClient.invalidateQueries({ queryKey: ['products'] })
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
47
frontend/src/services/resources/forms.ts
Normal file
47
frontend/src/services/resources/forms.ts
Normal file
@@ -0,0 +1,47 @@
|
||||
import type { Productor } from "@/services/resources/productors";
|
||||
import type { Shipment, ShipmentInputs } from "@/services/resources/shipments";
|
||||
import type { User } from "@/services/resources/users";
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
export type FormEdit = {
|
||||
name?: string | null;
|
||||
season?: string | null;
|
||||
start?: string | null;
|
||||
end?: string | null;
|
||||
productor_id?: number | null;
|
||||
referer_id?: number | null;
|
||||
}
|
||||
|
||||
export type FormEditPayload = {
|
||||
id: number;
|
||||
form: FormEdit;
|
||||
}
|
||||
|
||||
export type FormInputs = {
|
||||
name: string;
|
||||
season: string;
|
||||
start: string | null;
|
||||
end: string | null;
|
||||
productor_id: string;
|
||||
referer_id: string;
|
||||
shipments: ShipmentInputs[];
|
||||
}
|
||||
33
frontend/src/services/resources/productors.ts
Normal file
33
frontend/src/services/resources/productors.ts
Normal file
@@ -0,0 +1,33 @@
|
||||
export type Productor = {
|
||||
id: number;
|
||||
name: string;
|
||||
address: string;
|
||||
payment: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export type ProductorCreate = {
|
||||
name: string;
|
||||
address: string;
|
||||
payment: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export type ProductorEdit = {
|
||||
name: string | null;
|
||||
address: string | null;
|
||||
payment: string | null;
|
||||
type: string | null;
|
||||
}
|
||||
|
||||
export type ProductorInputs = {
|
||||
name: string;
|
||||
address: string;
|
||||
payment: string;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export type ProductorEditPayload = {
|
||||
productor: ProductorEdit;
|
||||
id: number;
|
||||
}
|
||||
49
frontend/src/services/resources/products.ts
Normal file
49
frontend/src/services/resources/products.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import type { Productor } from "@/services/resources/productors";
|
||||
import type { Shipment } from "@/services/resources/shipments";
|
||||
|
||||
export type Product = {
|
||||
id: number;
|
||||
productor: Productor;
|
||||
name: string;
|
||||
unit: number;
|
||||
price: number;
|
||||
priceKg: number | null;
|
||||
weight: number;
|
||||
type: number;
|
||||
shipments: Shipment[];
|
||||
}
|
||||
|
||||
export type ProductCreate = {
|
||||
productor_id: Productor;
|
||||
name: string;
|
||||
unit: number;
|
||||
price: number;
|
||||
priceKg: number | null;
|
||||
weight: number;
|
||||
type: number;
|
||||
}
|
||||
|
||||
export type ProductEdit = {
|
||||
productor_id: Productor | null;
|
||||
name: string | null;
|
||||
unit: number | null;
|
||||
price: number | null;
|
||||
priceKg: number | null;
|
||||
weight: number | null;
|
||||
type: number | null;
|
||||
}
|
||||
|
||||
export type ProductInputs = {
|
||||
productor_id: number | null;
|
||||
name: string;
|
||||
unit: number | null;
|
||||
price: number | null;
|
||||
priceKg: number | null;
|
||||
weight: number | null;
|
||||
type: string;
|
||||
}
|
||||
|
||||
export type ProductEditPayload = {
|
||||
product: ProductEdit;
|
||||
id: number;
|
||||
}
|
||||
30
frontend/src/services/resources/shipments.ts
Normal file
30
frontend/src/services/resources/shipments.ts
Normal file
@@ -0,0 +1,30 @@
|
||||
export type Shipment = {
|
||||
name: string;
|
||||
date: string;
|
||||
id: number;
|
||||
form_id: number;
|
||||
}
|
||||
|
||||
export type ShipmentCreate = {
|
||||
name: string;
|
||||
date: string;
|
||||
form_id: number;
|
||||
}
|
||||
|
||||
export type ShipmentEdit = {
|
||||
name: string | null;
|
||||
date: string | null;
|
||||
form_id: number | null;
|
||||
}
|
||||
|
||||
export type ShipmentEditPayload = {
|
||||
id: number;
|
||||
shipment: ShipmentEdit;
|
||||
}
|
||||
|
||||
export type ShipmentInputs = {
|
||||
name: string | null;
|
||||
date: string | null;
|
||||
id: number | null;
|
||||
form_id: number | null;
|
||||
}
|
||||
8
frontend/src/services/resources/users.ts
Normal file
8
frontend/src/services/resources/users.ts
Normal file
@@ -0,0 +1,8 @@
|
||||
import type { Product } from "@/services/resources/products";
|
||||
|
||||
export type User = {
|
||||
id: number;
|
||||
name: string;
|
||||
email: string;
|
||||
products: Product[];
|
||||
}
|
||||
Reference in New Issue
Block a user