82 lines
2.2 KiB
TypeScript
82 lines
2.2 KiB
TypeScript
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}`];
|
||
}
|
||
};
|