import { makeRequest } from "@api/makeRequest"; import { parseAxiosError } from "@utils/parse-error"; export type DevicesResponse = { Device: Record; OS: Record; Browser: Record; }; export type GeneralResponse = { Open: Record; Result: Record; AvTime: Record; Conversion: Record; }; export type QuestionsResponse = { Funnel: number[]; FunnelData: number[]; Results: Record; Questions: Record>; }; export type GraphicsResponse = unknown; type TRequest = { to: number; from: number; }; const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz/statistic`; export const getDevices = async ( quizId: string, to: number, from: number, ): Promise<[DevicesResponse | null, string?]> => { try { const devicesResponse = await makeRequest({ method: "POST", url: `${API_URL}/${quizId}/devices`, withCredentials: true, body: { to, from }, }); return [devicesResponse]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось получить статистику о девайсах. ${error}`]; } }; export const getGeneral = async ( quizId: string, to: number, from: number, ): Promise<[GeneralResponse | null, string?]> => { try { const generalResponse = await makeRequest({ method: "POST", url: `${API_URL}/${quizId}/general`, withCredentials: true, body: { to, from }, }); return [generalResponse]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось получить ключевые метрики. ${error}`]; } }; export const getQuestions = async ( quizId: string, to: number, from: number, ): Promise<[QuestionsResponse | null, string?]> => { try { const questionsResponse = await makeRequest({ method: "POST", url: `${API_URL}/${quizId}/questions`, withCredentials: true, body: { to, from }, }); return [questionsResponse]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось получить статистику по результатам. ${error}`]; } }; export const getGraphics = async ( quizId: string, to: number, from: number, ): Promise<[GraphicsResponse | null, string?]> => { try { const questionsResponse = await makeRequest({ method: "get", url: `${API_URL}/${quizId}/pipelines?from=${from}&to=${to}`, withCredentials: true, }); console.log(questionsResponse) return [questionsResponse]; } catch (nativeError) { const [error] = parseAxiosError(nativeError); return [null, `Не удалось получить статистику по результатам. ${error}`]; } };