front-hub/src/api/cart.ts

83 lines
2.3 KiB
TypeScript
Raw Normal View History

2024-05-27 15:43:38 +00:00
import { UserAccount } from "@frontend/kitui";
import makeRequest from "@api/makeRequest";
2023-06-30 15:35:31 +00:00
2024-05-27 15:43:38 +00:00
import { parseAxiosError } from "@root/utils/parse-error";
2023-06-30 15:35:31 +00:00
2024-06-07 16:59:44 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/customer/v1.0.0`;
2023-06-30 15:35:31 +00:00
2024-05-28 13:38:01 +00:00
export const patchCart = async (
2024-05-27 15:43:38 +00:00
tariffId: string
2024-05-28 13:38:01 +00:00
): Promise<[string[], string?]> => {
2024-05-27 15:43:38 +00:00
try {
const patchCartResponse = await makeRequest<never, UserAccount>({
method: "PATCH",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/cart?id=${tariffId}`,
2024-05-27 15:43:38 +00:00
useToken: true,
});
2023-08-30 09:56:14 +00:00
2024-05-27 15:43:38 +00:00
return [patchCartResponse.cart];
} catch (nativeError) {
let [error, status] = parseAxiosError(nativeError);
if (status === 400 && error.indexOf("invalid id") !== -1)
error = "Данный тариф более недоступен";
2023-08-30 09:56:14 +00:00
2024-05-27 15:43:38 +00:00
return [[], `Не удалось добавить товар в корзину. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2023-06-30 15:35:31 +00:00
2024-05-28 13:38:01 +00:00
export const deleteCart = async (
2024-05-27 15:43:38 +00:00
tariffId: string
2024-05-28 13:38:01 +00:00
): Promise<[string[], string?]> => {
2024-05-27 15:43:38 +00:00
try {
const deleteCartResponse = await makeRequest<never, UserAccount>({
method: "DELETE",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/cart?id=${tariffId}`,
2024-05-27 15:43:38 +00:00
useToken: true,
});
2023-08-30 09:56:14 +00:00
2024-05-27 15:43:38 +00:00
return [deleteCartResponse.cart];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2023-08-30 09:56:14 +00:00
2024-05-27 15:43:38 +00:00
return [[], `Не удалось удалить товар из корзины. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2023-07-07 13:53:08 +00:00
2024-05-28 13:38:01 +00:00
export const payCart = async (): Promise<[UserAccount | null, string?]> => {
2024-05-27 15:43:38 +00:00
try {
const payCartResponse = await makeRequest<never, UserAccount>({
method: "POST",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/cart/pay`,
2024-05-27 15:43:38 +00:00
useToken: true,
});
2023-08-30 09:56:14 +00:00
2024-05-27 15:43:38 +00:00
return [payCartResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2023-08-30 09:56:14 +00:00
2024-05-27 15:43:38 +00:00
return [null, `Не удалось оплатить товар из корзины. ${error}`];
}
2024-05-28 13:38:01 +00:00
};
2023-07-07 13:53:08 +00:00
2024-05-28 13:38:01 +00:00
export const patchCurrency = async (
2024-05-27 15:43:38 +00:00
currency: string
2024-05-28 13:38:01 +00:00
): Promise<[UserAccount | null, string?]> => {
2024-05-27 15:43:38 +00:00
try {
const patchCurrencyResponse = await makeRequest<
2023-08-30 09:56:14 +00:00
{ currency: string },
UserAccount
>({
2024-05-27 15:43:38 +00:00
method: "PATCH",
2024-05-28 13:38:01 +00:00
url: `${API_URL}/wallet`,
2024-05-27 15:43:38 +00:00
useToken: true,
2024-05-28 13:38:01 +00:00
body: { currency },
2024-05-27 15:43:38 +00:00
});
2023-08-30 09:56:14 +00:00
2024-05-27 15:43:38 +00:00
return [patchCurrencyResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2023-08-30 09:56:14 +00:00
2024-05-27 15:43:38 +00:00
return [null, `Не удалось изменить валюту. ${error}`];
}
2024-05-28 13:38:01 +00:00
};