2024-05-13 13:24:41 +00:00
|
|
|
|
import { makeRequest } from "@api/makeRequest";
|
2023-11-14 13:09:17 +00:00
|
|
|
|
import { defaultQuizConfig } from "@model/quizSettings";
|
2023-11-02 16:45:28 +00:00
|
|
|
|
import { CopyQuizRequest, CopyQuizResponse } from "model/quiz/copy";
|
|
|
|
|
import { CreateQuizRequest } from "model/quiz/create";
|
|
|
|
|
import { DeleteQuizRequest, DeleteQuizResponse } from "model/quiz/delete";
|
|
|
|
|
import { EditQuizRequest, EditQuizResponse } from "model/quiz/edit";
|
|
|
|
|
import { GetQuizRequest, GetQuizResponse } from "model/quiz/get";
|
|
|
|
|
import { GetQuizListRequest, GetQuizListResponse } from "model/quiz/getList";
|
2023-11-13 18:04:51 +00:00
|
|
|
|
import { RawQuiz } from "model/quiz/quiz";
|
2024-05-13 13:24:41 +00:00
|
|
|
|
import { parseAxiosError } from "@utils/parse-error";
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
2024-05-13 13:24:41 +00:00
|
|
|
|
type AddedQuizImagesResponse = {
|
|
|
|
|
[key: string]: string;
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-15 11:44:10 +00:00
|
|
|
|
const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz`;
|
|
|
|
|
const IMAGES_URL = `${process.env.REACT_APP_DOMAIN}/squizstorer`;
|
2024-05-13 13:24:41 +00:00
|
|
|
|
|
|
|
|
|
export const createQuiz = async (
|
|
|
|
|
body?: Partial<CreateQuizRequest>,
|
|
|
|
|
): Promise<[RawQuiz | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const createdQuiz = await makeRequest<CreateQuizRequest, RawQuiz>({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: `${API_URL}/quiz/create`,
|
|
|
|
|
body: { ...defaultCreateQuizBody, ...body },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [createdQuiz];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-05-15 12:37:42 +00:00
|
|
|
|
return [null, `Не удалось создать квиз. ${error}`];
|
2024-05-13 13:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getQuizList = async (
|
|
|
|
|
body?: Partial<CreateQuizRequest>,
|
|
|
|
|
): Promise<[RawQuiz[] | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const { items } = await makeRequest<
|
|
|
|
|
GetQuizListRequest,
|
|
|
|
|
GetQuizListResponse
|
|
|
|
|
>({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: `${API_URL}/quiz/getList`,
|
|
|
|
|
body: { ...defaultGetQuizListBody, ...body },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [items];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-05-15 12:37:42 +00:00
|
|
|
|
return [null, `Не удалось получить список квизов. ${error}`];
|
2024-05-13 13:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getQuiz = async (
|
|
|
|
|
body?: Partial<GetQuizRequest>,
|
|
|
|
|
): Promise<[GetQuizResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const quiz = await makeRequest<GetQuizRequest, GetQuizResponse>({
|
|
|
|
|
method: "GET",
|
|
|
|
|
url: `${API_URL}/quiz/get`,
|
|
|
|
|
body: { ...defaultGetQuizBody, ...body },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [quiz];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-05-15 12:37:42 +00:00
|
|
|
|
return [null, `Не удалось получить квиз. ${error}`];
|
2024-05-13 13:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const editQuiz = async (
|
|
|
|
|
body: EditQuizRequest,
|
|
|
|
|
signal?: AbortSignal,
|
|
|
|
|
): Promise<[EditQuizResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const editedQuiz = await makeRequest<EditQuizRequest, EditQuizResponse>({
|
|
|
|
|
method: "PATCH",
|
|
|
|
|
url: `${API_URL}/quiz/edit`,
|
|
|
|
|
body,
|
|
|
|
|
signal,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [editedQuiz];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-05-15 12:37:42 +00:00
|
|
|
|
return [null, `Не удалось изменить квиз. ${error}`];
|
2024-05-13 13:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const copyQuiz = async (
|
|
|
|
|
id: number,
|
|
|
|
|
): Promise<[EditQuizResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const copiedQuiz = await makeRequest<CopyQuizRequest, CopyQuizResponse>({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: `${API_URL}/quiz/copy`,
|
|
|
|
|
body: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [copiedQuiz];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-05-15 12:37:42 +00:00
|
|
|
|
return [null, `Не удалось скопировать квиз. ${error}`];
|
2024-05-13 13:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const deleteQuiz = async (
|
|
|
|
|
id: number,
|
|
|
|
|
): Promise<[DeleteQuizResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const deletedQuiz = await makeRequest<
|
|
|
|
|
DeleteQuizRequest,
|
|
|
|
|
DeleteQuizResponse
|
|
|
|
|
>({
|
|
|
|
|
method: "DELETE",
|
|
|
|
|
url: `${API_URL}/quiz/delete`,
|
|
|
|
|
body: { id },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [deletedQuiz];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-05-15 12:37:42 +00:00
|
|
|
|
return [null, `Не удалось удалить квиз. ${error}`];
|
2024-05-13 13:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const addQuizImages = async (
|
|
|
|
|
quizId: number,
|
|
|
|
|
image: Blob,
|
|
|
|
|
): Promise<[AddedQuizImagesResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
|
|
formData.append("quiz", quizId.toString());
|
|
|
|
|
formData.append("image", image);
|
|
|
|
|
|
|
|
|
|
const addedQuizImages = await makeRequest<
|
|
|
|
|
FormData,
|
|
|
|
|
AddedQuizImagesResponse
|
|
|
|
|
>({
|
|
|
|
|
url: `${IMAGES_URL}/quiz/putImages`,
|
|
|
|
|
body: formData,
|
|
|
|
|
method: "PUT",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [addedQuizImages];
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const [error] = parseAxiosError(nativeError);
|
|
|
|
|
|
2024-05-15 13:51:41 +00:00
|
|
|
|
return [null, `Не удалось добавить изображение. ${error}`];
|
2024-05-13 13:24:41 +00:00
|
|
|
|
}
|
|
|
|
|
};
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
|
|
|
|
export const quizApi = {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
create: createQuiz,
|
|
|
|
|
getList: getQuizList,
|
|
|
|
|
get: getQuiz,
|
|
|
|
|
edit: editQuiz,
|
|
|
|
|
copy: copyQuiz,
|
|
|
|
|
delete: deleteQuiz,
|
|
|
|
|
addImages: addQuizImages,
|
2023-11-02 16:45:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultCreateQuizBody: CreateQuizRequest = {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
fingerprinting: true,
|
|
|
|
|
repeatable: true,
|
|
|
|
|
note_prevented: true,
|
|
|
|
|
mail_notifications: false,
|
|
|
|
|
unique_answers: true,
|
|
|
|
|
name: "",
|
|
|
|
|
description: "",
|
|
|
|
|
config: JSON.stringify(defaultQuizConfig),
|
|
|
|
|
status: "stop",
|
|
|
|
|
limit: 0,
|
|
|
|
|
due_to: 0,
|
|
|
|
|
time_of_passing: 0,
|
|
|
|
|
pausable: false,
|
|
|
|
|
super: false,
|
|
|
|
|
group_id: 0,
|
2023-11-02 16:45:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultGetQuizBody: GetQuizRequest = {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
quiz_id: "string",
|
|
|
|
|
limit: 0,
|
|
|
|
|
page: 0,
|
|
|
|
|
need_config: true,
|
2023-11-02 16:45:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultGetQuizListBody: GetQuizListRequest = {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
limit: 100,
|
|
|
|
|
offset: 0,
|
2023-11-02 16:45:28 +00:00
|
|
|
|
};
|