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 { Contract, ContractCreate } from "./resources/contracts"; import { notifications } from "@mantine/notifications"; import { t } from "@/config/i18n"; export function useGetShipments(filters?: URLSearchParams): UseQueryResult { const queryString = filters?.toString(); return useQuery({ queryKey: ["shipments", queryString], queryFn: () => fetch(`${Config.backend_uri}/shipments${filters ? `?${queryString}` : ""}`).then( (res) => res.json(), ), }); } export function useGetShipment( id?: number, options?: Partial>, ): UseQueryResult { return useQuery({ 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 { const queryString = filters?.toString(); return useQuery({ queryKey: ["productors", queryString], queryFn: () => fetch(`${Config.backend_uri}/productors${filters ? `?${queryString}` : ""}`).then( (res) => res.json(), ), }); } export function useGetProductor( id?: number, options?: Partial>, ) { return useQuery({ 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>, ) { return useQuery
({ queryKey: ["form"], queryFn: () => fetch(`${Config.backend_uri}/forms/${id}`).then((res) => res.json()), enabled: !!id, ...options, }); } export function useGetForms(filters?: URLSearchParams): UseQueryResult { const queryString = filters?.toString(); return useQuery({ 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>, ) { return useQuery({ queryKey: ["product"], queryFn: () => fetch(`${Config.backend_uri}/products/${id}`).then((res) => res.json()), enabled: !!id, ...options, }); } export function useGetProducts(filters?: URLSearchParams): UseQueryResult { const queryString = filters?.toString(); return useQuery({ 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>, ) { return useQuery({ queryKey: ["user"], queryFn: () => fetch(`${Config.backend_uri}/users/${id}`).then((res) => res.json()), enabled: !!id, ...options, }); } export function useGetUsers(filters?: URLSearchParams): UseQueryResult { const queryString = filters?.toString(); return useQuery({ 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 useGetContracts(filters?: URLSearchParams): UseQueryResult { const queryString = filters?.toString(); return useQuery({ queryKey: ["contracts", queryString], queryFn: () => fetch(`${Config.backend_uri}/contracts${filters ? `?${queryString}` : ""}`).then( (res) => res.json(), ), }); } export function useGetContract( id?: number, options?: Partial>, ) { return useQuery({ queryKey: ["contract"], queryFn: () => fetch(`${Config.backend_uri}/contracts/${id}`).then((res) => res.json()), enabled: !!id, ...options, }); } 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"] }); }, }); } export function useDeleteContract() { const queryClient = useQueryClient(); return useMutation({ mutationFn: (id: number) => { return fetch(`${Config.backend_uri}/contracts/${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 contract", { capfirst: true }), }); await queryClient.invalidateQueries({ queryKey: ["contracts"] }); }, onError: (error) => { notifications.show({ title: t("error", { capfirst: true }), message: error?.message || t(`error deleting contract`, { capfirst: true }), color: "red", }); }, }); }