580 lines
20 KiB
TypeScript
580 lines
20 KiB
TypeScript
import { useMutation, useQuery, useQueryClient,type DefinedInitialDataOptions,type UseQueryResult } from "@tanstack/react-query";
|
|
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, UserCreate, UserEditPayload } from "@/services/resources/users";
|
|
import type { Product, ProductCreate, ProductEditPayload } from "./resources/products";
|
|
import type { ContractCreate } from "./resources/contracts";
|
|
import { notifications } from "@mantine/notifications";
|
|
import { t } from "@/config/i18n";
|
|
|
|
export function useGetShipments(filters?: URLSearchParams): UseQueryResult<Shipment[], Error> {
|
|
const queryString = filters?.toString()
|
|
return useQuery<Shipment[]>({
|
|
queryKey: ['shipments', queryString],
|
|
queryFn: () => (
|
|
fetch(`${Config.backend_uri}/shipments${filters ? `?${queryString}` : ""}`)
|
|
.then((res) => res.json())
|
|
),
|
|
});
|
|
}
|
|
|
|
export function useGetShipment(id?: number, options?: Partial<DefinedInitialDataOptions<Shipment, Error, Shipment, readonly unknown[]>>): UseQueryResult<Shipment, Error> {
|
|
return useQuery<Shipment>({
|
|
queryKey: ['shipment'],
|
|
queryFn: () => (
|
|
fetch(`${Config.backend_uri}/shipments/${id}`)
|
|
.then((res) => res.json())
|
|
),
|
|
enabled: !!id,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
export function useCreateShipment() {
|
|
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),
|
|
}).then((res) => res.json());
|
|
},
|
|
onSuccess: async () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully created shipment", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['shipments'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing shipment`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
})
|
|
}
|
|
|
|
export function useEditShipment() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: ({shipment, id}: 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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully edited shipment", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['shipments'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing shipment`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
})
|
|
}
|
|
|
|
export function useDeleteShipment() {
|
|
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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully deleted shipment", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['shipments'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error deleting shipment`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useGetProductors(filters?: URLSearchParams): UseQueryResult<Productor[], Error> {
|
|
const queryString = filters?.toString()
|
|
return useQuery<Productor[]>({
|
|
queryKey: ['productors', queryString],
|
|
queryFn: () => (
|
|
fetch(`${Config.backend_uri}/productors${filters ? `?${queryString}` : ""}`)
|
|
.then((res) => res.json())
|
|
),
|
|
});
|
|
}
|
|
|
|
export function useGetProductor(id?: number, options?: Partial<DefinedInitialDataOptions<Productor, Error, Productor, readonly unknown[]>>) {
|
|
return useQuery<Productor>({
|
|
queryKey: ['productor'],
|
|
queryFn: () => (
|
|
fetch(`${Config.backend_uri}/productors/${id}`)
|
|
.then((res) => res.json())
|
|
),
|
|
enabled: !!id,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
export function useCreateProductor() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (newProductor: ProductorCreate) => {
|
|
return fetch(`${Config.backend_uri}/productors`, {
|
|
method: 'POST',
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify(newProductor),
|
|
}).then((res) => res.json());
|
|
},
|
|
onSuccess: async () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully created productor", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['productors'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing productor`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
})
|
|
}
|
|
|
|
export function useEditProductor() {
|
|
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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully edited productor", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['productors'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing productor`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
})
|
|
}
|
|
|
|
export function useDeleteProductor() {
|
|
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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully deleted productor", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['productors'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error deleting productor`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useGetForm(id?: number, options?: Partial<DefinedInitialDataOptions<Form, Error, Form, readonly unknown[]>>) {
|
|
return useQuery<Form>({
|
|
queryKey: ['form'],
|
|
queryFn: () => (
|
|
fetch(`${Config.backend_uri}/forms/${id}`)
|
|
.then((res) => res.json())
|
|
),
|
|
enabled: !!id,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
export function useGetForms(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 useCreateForm() {
|
|
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),
|
|
}).then((res) => res.json());
|
|
},
|
|
onSuccess: async () => {
|
|
await queryClient.invalidateQueries({ queryKey: ['forms'] })
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useDeleteForm() {
|
|
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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully deleted form", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['forms'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error deleting form`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useEditForm() {
|
|
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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully edited form", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['forms'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing form`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useGetProduct(id?: number, options?: Partial<DefinedInitialDataOptions<Product, Error, Product, readonly unknown[]>>) {
|
|
return useQuery<Product>({
|
|
queryKey: ['product'],
|
|
queryFn: () => (
|
|
fetch(`${Config.backend_uri}/products/${id}`)
|
|
.then((res) => res.json())
|
|
),
|
|
enabled: !!id,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
export function useGetProducts(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 useCreateProduct() {
|
|
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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully created product", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['products'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing product`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useDeleteProduct() {
|
|
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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully deleted product", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['products'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error deleting product`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useEditProduct() {
|
|
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 () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully edited product", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['products'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing product`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useGetUser(id?: number, options?: Partial<DefinedInitialDataOptions<User, Error, User, readonly unknown[]>>) {
|
|
return useQuery<User>({
|
|
queryKey: ['user'],
|
|
queryFn: () => (
|
|
fetch(`${Config.backend_uri}/users/${id}`)
|
|
.then((res) => res.json())
|
|
),
|
|
enabled: !!id,
|
|
...options,
|
|
});
|
|
}
|
|
|
|
export function useGetUsers(filters?: URLSearchParams): UseQueryResult<User[], Error> {
|
|
const queryString = filters?.toString()
|
|
return useQuery<User[]>({
|
|
queryKey: ['users', queryString],
|
|
queryFn: () => (
|
|
fetch(`${Config.backend_uri}/users${filters ? `?${queryString}` : ""}`)
|
|
.then((res) => res.json())
|
|
),
|
|
});
|
|
}
|
|
|
|
export function useCreateUser() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (newUser: UserCreate) => {
|
|
return fetch(`${Config.backend_uri}/users`, {
|
|
method: 'POST',
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify(newUser),
|
|
}).then((res) => res.json());
|
|
},
|
|
onSuccess: async () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully created user", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['users'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing user`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useDeleteUser() {
|
|
const queryClient = useQueryClient()
|
|
return useMutation({
|
|
mutationFn: (id: number) => {
|
|
return fetch(`${Config.backend_uri}/users/${id}`, {
|
|
method: 'DELETE',
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
}).then((res) => res.json());
|
|
},
|
|
onSuccess: async () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully deleted user", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['users'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error deleting user`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
export function useEditUser() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: ({id, user}: UserEditPayload) => {
|
|
return fetch(`${Config.backend_uri}/users/${id}`, {
|
|
method: 'PUT',
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify(user),
|
|
}).then((res) => res.json());
|
|
},
|
|
onSuccess: async () => {
|
|
notifications.show({
|
|
title: t("success", {capfirst: true}),
|
|
message: t("successfully edited user", {capfirst: true}),
|
|
});
|
|
await queryClient.invalidateQueries({ queryKey: ['users'] })
|
|
},
|
|
onError: (error) => {
|
|
notifications.show({
|
|
title: t("error", {capfirst: true}),
|
|
message: error?.message || t(`error editing user`, {capfirst: true}),
|
|
color: "red"
|
|
});
|
|
}
|
|
});
|
|
}
|
|
|
|
|
|
export function useCreateContract() {
|
|
const queryClient = useQueryClient()
|
|
|
|
return useMutation({
|
|
mutationFn: (newContract: ContractCreate) => {
|
|
return fetch(`${Config.backend_uri}/contracts`, {
|
|
method: 'POST',
|
|
headers: {
|
|
"Content-Type": "application/json"
|
|
},
|
|
body: JSON.stringify(newContract),
|
|
}).then(async (res) => await res.blob());
|
|
},
|
|
onSuccess: async (pdfBlob) => {
|
|
const url = URL.createObjectURL(pdfBlob);
|
|
const link = document.createElement("a");
|
|
link.href = url;
|
|
link.download = `contract.pdf`;
|
|
link.click();
|
|
URL.revokeObjectURL(url);
|
|
await queryClient.invalidateQueries({ queryKey: ["contracts"] });
|
|
}
|
|
});
|
|
} |