add contract storage fix various bugs and translations
This commit is contained in:
@@ -15,7 +15,7 @@ import type {
|
||||
} 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 type { Contract, ContractCreate } from "./resources/contracts";
|
||||
import { notifications } from "@mantine/notifications";
|
||||
import { t } from "@/config/i18n";
|
||||
|
||||
@@ -563,6 +563,29 @@ export function useEditUser() {
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetContracts(filters?: URLSearchParams): UseQueryResult<Contract[], Error> {
|
||||
const queryString = filters?.toString();
|
||||
return useQuery<Contract[]>({
|
||||
queryKey: ["contracts", queryString],
|
||||
queryFn: () =>
|
||||
fetch(`${Config.backend_uri}/contracts${filters ? `?${queryString}` : ""}`).then(
|
||||
(res) => res.json(),
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
export function useGetContract(
|
||||
id?: number,
|
||||
options?: Partial<DefinedInitialDataOptions<Contract, Error, Contract, readonly unknown[]>>,
|
||||
) {
|
||||
return useQuery<Contract>({
|
||||
queryKey: ["contract"],
|
||||
queryFn: () => fetch(`${Config.backend_uri}/contracts/${id}`).then((res) => res.json()),
|
||||
enabled: !!id,
|
||||
...options,
|
||||
});
|
||||
}
|
||||
|
||||
export function useCreateContract() {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
@@ -587,3 +610,31 @@ export function useCreateContract() {
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
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",
|
||||
});
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,4 +1,72 @@
|
||||
import type { Cheque } from "@/components/PaymentMethods/Cheque";
|
||||
import type { Form } from "./forms";
|
||||
import type { Product } from "./products";
|
||||
import type { Shipment } from "./shipments";
|
||||
|
||||
export type Contract = {
|
||||
id: number;
|
||||
form_id: number;
|
||||
products: ContractProduct;
|
||||
form: Form;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
payment_method: string;
|
||||
cheque_quantity: number;
|
||||
};
|
||||
|
||||
export type ContractCreate = {
|
||||
form_id: number;
|
||||
contract: Record<string, string | number | null>;
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
payment_method: string;
|
||||
cheque_quantity: number;
|
||||
products: ContractProductCreate[];
|
||||
cheques: Cheque[];
|
||||
};
|
||||
|
||||
export type ContractInputs = {
|
||||
firstname: string;
|
||||
lastname: string;
|
||||
email: string;
|
||||
phone: string;
|
||||
products: Record<string, string | number>;
|
||||
payment_method: string;
|
||||
cheques: Cheque[];
|
||||
cheque_quantity: number;
|
||||
};
|
||||
|
||||
export type ContractProduct = {
|
||||
id: number;
|
||||
product_id: number;
|
||||
shipment_id: number;
|
||||
quantity: number;
|
||||
contract: Contract;
|
||||
product: Product;
|
||||
shipment?: Shipment | null;
|
||||
};
|
||||
|
||||
export type ContractProductCreate = {
|
||||
product_id: number;
|
||||
shipment_id: number | null;
|
||||
quantity: number;
|
||||
};
|
||||
|
||||
export function tranformProducts(
|
||||
products: Record<string, string | number>,
|
||||
): ContractProductCreate[] {
|
||||
return Object.entries(products).map(([key, value]) => {
|
||||
const quantity = value;
|
||||
const parts = key.split("-");
|
||||
const shipment_id = parts[0] === "planned" ? Number(parts[1]) : null;
|
||||
const product_id = parts[0] === "planned" ? Number(parts[2]) : Number(parts[1]);
|
||||
return {
|
||||
quantity: Number(quantity),
|
||||
shipment_id: shipment_id,
|
||||
product_id: product_id,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user