front-hub/src/api/cart.ts

85 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-05-27 15:43:38 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/customer`;
2023-06-30 15:35:31 +00:00
2023-08-30 09:56:14 +00:00
export async function patchCart(
2024-05-27 15:43:38 +00:00
tariffId: string
2023-08-30 09:56:14 +00:00
): Promise<[string[], string?]> {
2024-05-27 15:43:38 +00:00
try {
const patchCartResponse = await makeRequest<never, UserAccount>({
url: `${API_URL}/cart?id=${tariffId}`,
method: "PATCH",
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}`];
}
2023-06-30 15:35:31 +00:00
}
2023-08-30 09:56:14 +00:00
export async function deleteCart(
2024-05-27 15:43:38 +00:00
tariffId: string
2023-08-30 09:56:14 +00:00
): Promise<[string[], string?]> {
2024-05-27 15:43:38 +00:00
try {
const deleteCartResponse = await makeRequest<never, UserAccount>({
url: `${API_URL}/cart?id=${tariffId}`,
method: "DELETE",
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}`];
}
2023-07-07 13:53:08 +00:00
}
2023-08-30 09:56:14 +00:00
export async function payCart(): Promise<[UserAccount | null, string?]> {
2024-05-27 15:43:38 +00:00
try {
const payCartResponse = await makeRequest<never, UserAccount>({
url: `${API_URL}/cart/pay`,
method: "POST",
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}`];
}
2023-07-07 13:53:08 +00:00
}
2023-08-30 09:56:14 +00:00
export async function patchCurrency(
2024-05-27 15:43:38 +00:00
currency: string
2023-08-30 09:56:14 +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
url: `${API_URL}/wallet`,
method: "PATCH",
useToken: true,
body: {
currency,
},
});
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}`];
}
}