frontPanel/src/api/statistic.ts
2024-03-21 17:24:56 +03:00

82 lines
2.2 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { makeRequest } from "@frontend/kitui";
import { parseAxiosError } from "@utils/parse-error";
const apiUrl = process.env.REACT_APP_DOMAIN + "/squiz/statistic";
export type DevicesResponse = {
device: Record<string, number>;
os: Record<string, number>;
browser: Record<string, number>;
};
export type GeneralResponse = {
open: Record<string, number>;
result: Record<string, number>;
avtime: Record<string, number>;
conversation: Record<string, number>;
};
export type QuestionsResponse = {
funnel: number[];
results: Record<string, number>;
questions: Record<string, Record<string, number>>;
};
export const getDevices = async (
quizId: string,
): Promise<[DevicesResponse | null, string?]> => {
try {
const devicesResponse = await makeRequest<unknown, DevicesResponse>({
method: "POST",
url: `${apiUrl}/${quizId}/devices`,
useToken: false,
withCredentials: true,
});
return [devicesResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось получить статистику о девайсах. ${error}`];
}
};
export const getGeneral = async (
quizId: string,
): Promise<[GeneralResponse | null, string?]> => {
try {
const generalResponse = await makeRequest<unknown, GeneralResponse>({
method: "POST",
url: `${apiUrl}/${quizId}/general`,
useToken: false,
withCredentials: true,
});
return [generalResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось получить ключевые метрики. ${error}`];
}
};
export const getQuestions = async (
quizId: string,
): Promise<[QuestionsResponse | null, string?]> => {
try {
const questionsResponse = await makeRequest<unknown, QuestionsResponse>({
method: "POST",
url: `${apiUrl}/${quizId}/questions`,
useToken: false,
withCredentials: true,
});
return [questionsResponse];
} catch (nativeError) {
const [error] = parseAxiosError(nativeError);
return [null, `Не удалось получить статистику по результатам. ${error}`];
}
};