108 lines
2.6 KiB
TypeScript
108 lines
2.6 KiB
TypeScript
import { makeRequest } from "@frontend/kitui";
|
||
import { parseAxiosError } from "@utils/parse-error";
|
||
|
||
const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz`;
|
||
|
||
// Types
|
||
export interface AuditoryItem {
|
||
id: number;
|
||
quiz_id: number;
|
||
sex: number; // 0 - женский, 1 - мужской, 2 - оба
|
||
age: string;
|
||
deleted: boolean;
|
||
created_at: number;
|
||
}
|
||
|
||
export interface AuditoryResponse {
|
||
ID: number;
|
||
quiz_id: number;
|
||
sex: number;
|
||
age: string;
|
||
deleted: boolean;
|
||
created_at: number;
|
||
}
|
||
|
||
// Request Types
|
||
export interface AuditoryGetRequest {
|
||
quizId: number;
|
||
}
|
||
|
||
export interface AuditoryDeleteRequest {
|
||
id: number;
|
||
}
|
||
|
||
export interface AuditoryAddRequest {
|
||
sex: number;
|
||
age: string;
|
||
}
|
||
|
||
// Parameters
|
||
export interface AuditoryGetParams {
|
||
quizId: number;
|
||
}
|
||
|
||
export interface AuditoryDeleteParams {
|
||
quizId: number;
|
||
auditoryId: number;
|
||
}
|
||
|
||
export interface AuditoryAddParams {
|
||
quizId: number;
|
||
body: AuditoryAddRequest;
|
||
}
|
||
|
||
// API calls
|
||
export const auditoryGet = async ({ quizId }: AuditoryGetParams): Promise<[AuditoryItem[] | null, string?]> => {
|
||
if (!quizId) {
|
||
return [null, "Quiz ID is required"];
|
||
}
|
||
|
||
try {
|
||
const response = await makeRequest<AuditoryGetRequest, AuditoryItem[]>({
|
||
url: `${API_URL}/quiz/${quizId}/auditory`,
|
||
method: "GET",
|
||
});
|
||
|
||
return [response];
|
||
} catch (nativeError) {
|
||
const [error] = parseAxiosError(nativeError);
|
||
return [null, `Не удалось получить аудиторию. ${error}`];
|
||
}
|
||
};
|
||
|
||
export const auditoryDelete = async ({ quizId, auditoryId }: AuditoryDeleteParams): Promise<[AuditoryResponse | null, string?]> => {
|
||
if (!quizId || !auditoryId) {
|
||
return [null, "Quiz ID and Auditory ID are required"];
|
||
}
|
||
|
||
try {
|
||
const response = await makeRequest<AuditoryDeleteRequest, AuditoryResponse>({
|
||
url: `${API_URL}/quiz/${quizId}/auditory/${auditoryId}`,
|
||
method: "DELETE",
|
||
});
|
||
|
||
return [response];
|
||
} catch (nativeError) {
|
||
const [error] = parseAxiosError(nativeError);
|
||
return [null, `Не удалось удалить аудиторию. ${error}`];
|
||
}
|
||
};
|
||
|
||
export const auditoryAdd = async ({ quizId, body }: AuditoryAddParams): Promise<[AuditoryResponse | null, string?]> => {
|
||
if (!quizId) {
|
||
return [null, "Quiz ID is required"];
|
||
}
|
||
|
||
try {
|
||
const response = await makeRequest<AuditoryAddRequest, AuditoryResponse>({
|
||
url: `${API_URL}/quiz/${quizId}/auditory`,
|
||
body,
|
||
method: "POST",
|
||
});
|
||
|
||
return [response];
|
||
} catch (nativeError) {
|
||
const [error] = parseAxiosError(nativeError);
|
||
return [null, `Не удалось добавить аудиторию. ${error}`];
|
||
}
|
||
}; |