frontPanel/src/api/promocode.ts

31 lines
867 B
TypeScript
Raw Normal View History

2024-05-13 13:24:41 +00:00
import { makeRequest } from "@api/makeRequest";
2024-04-09 10:41:32 +00:00
import { parseAxiosError } from "@utils/parse-error";
2024-03-22 19:01:48 +00:00
2024-05-13 13:24:41 +00:00
type ActivatePromocodeRequest = { codeword: string } | { fastLink: string };
type ActivatePromocodeResponse = { greetings: string };
2024-03-22 19:01:48 +00:00
2024-05-15 11:44:10 +00:00
const API_URL = `${process.env.REACT_APP_DOMAIN}/codeword/promocode`;
2024-05-13 13:24:41 +00:00
2024-05-15 11:44:10 +00:00
export const activatePromocode = async (
promocode: string,
): Promise<[string | null, string?]> => {
2024-03-22 19:01:48 +00:00
try {
const response = await makeRequest<
2024-05-13 13:24:41 +00:00
ActivatePromocodeRequest,
ActivatePromocodeResponse
2024-03-22 19:01:48 +00:00
>({
method: "POST",
2024-05-15 11:44:10 +00:00
url: `${API_URL}/activate`,
2024-04-09 10:41:32 +00:00
body: { codeword: promocode },
2024-05-13 13:24:41 +00:00
contentType: true,
2024-03-22 19:01:48 +00:00
});
2024-05-15 11:44:10 +00:00
return [response.greetings];
2024-04-09 10:41:32 +00:00
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
2024-05-15 11:44:10 +00:00
return [null, `Ошибка при активации промокода. ${error}`];
2024-03-22 19:01:48 +00:00
}
2024-05-13 13:24:41 +00:00
};