2024-05-13 13:24:41 +00:00
|
|
|
|
import { makeRequest } from "@api/makeRequest";
|
2024-05-16 13:40:52 +00:00
|
|
|
|
|
|
|
|
|
import { replaceSpacesToEmptyLines } from "@utils/replaceSpacesToEmptyLines";
|
|
|
|
|
import { parseAxiosError } from "@utils/parse-error";
|
|
|
|
|
|
|
|
|
|
import type { CreateQuestionRequest } from "model/question/create";
|
|
|
|
|
import type { RawQuestion } from "model/question/question";
|
|
|
|
|
import type {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
GetQuestionListRequest,
|
|
|
|
|
GetQuestionListResponse,
|
|
|
|
|
} from "@model/question/getList";
|
2024-05-16 13:40:52 +00:00
|
|
|
|
import type {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
EditQuestionRequest,
|
|
|
|
|
EditQuestionResponse,
|
|
|
|
|
} from "@model/question/edit";
|
2024-05-16 13:40:52 +00:00
|
|
|
|
import type {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
DeleteQuestionRequest,
|
|
|
|
|
DeleteQuestionResponse,
|
|
|
|
|
} from "@model/question/delete";
|
2024-05-16 13:40:52 +00:00
|
|
|
|
import type {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
CopyQuestionRequest,
|
|
|
|
|
CopyQuestionResponse,
|
|
|
|
|
} from "@model/question/copy";
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
2024-05-15 12:37:42 +00:00
|
|
|
|
const API_URL = `${process.env.REACT_APP_DOMAIN}/squiz`;
|
2024-05-13 13:24:41 +00:00
|
|
|
|
|
|
|
|
|
export const createQuestion = async (
|
|
|
|
|
body: CreateQuestionRequest,
|
|
|
|
|
): Promise<[RawQuestion | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const createdQuestion = await makeRequest<
|
|
|
|
|
CreateQuestionRequest,
|
|
|
|
|
RawQuestion
|
|
|
|
|
>({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: `${API_URL}/question/create`,
|
|
|
|
|
body,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [createdQuestion];
|
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const getQuestionList = async (
|
|
|
|
|
body?: Partial<GetQuestionListRequest>,
|
|
|
|
|
): Promise<[RawQuestion[] | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
if (!body?.quiz_id) return [null, "Квиз не найден"];
|
|
|
|
|
|
|
|
|
|
const response = await makeRequest<
|
|
|
|
|
GetQuestionListRequest,
|
|
|
|
|
GetQuestionListResponse
|
|
|
|
|
>({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: `${API_URL}/question/getList`,
|
|
|
|
|
body: { ...defaultGetQuestionListBody, ...body },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const clearArrayFromEmptySpaceBlaBlaValue = response.items?.map(
|
|
|
|
|
(question) => {
|
|
|
|
|
let data = question;
|
|
|
|
|
|
|
|
|
|
for (let key in question) {
|
|
|
|
|
if (question[key as keyof RawQuestion] === " ") {
|
|
|
|
|
//@ts-ignore
|
|
|
|
|
data[key] = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return data;
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
replaceSpacesToEmptyLines(clearArrayFromEmptySpaceBlaBlaValue) ?? null,
|
|
|
|
|
];
|
|
|
|
|
} 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 editQuestion = async (
|
|
|
|
|
body: EditQuestionRequest,
|
|
|
|
|
signal?: AbortSignal,
|
|
|
|
|
): Promise<[EditQuestionResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const editedQuestion = await makeRequest<
|
|
|
|
|
EditQuestionRequest,
|
|
|
|
|
EditQuestionResponse
|
|
|
|
|
>({
|
|
|
|
|
method: "PATCH",
|
|
|
|
|
url: `${API_URL}/question/edit`,
|
|
|
|
|
body,
|
|
|
|
|
signal,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [editedQuestion];
|
|
|
|
|
} 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 copyQuestion = async (
|
|
|
|
|
questionId: number,
|
|
|
|
|
quizId: number,
|
|
|
|
|
): Promise<[CopyQuestionResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const copiedQuestion = await makeRequest<
|
|
|
|
|
CopyQuestionRequest,
|
|
|
|
|
CopyQuestionResponse
|
|
|
|
|
>({
|
|
|
|
|
method: "POST",
|
|
|
|
|
url: `${API_URL}/question/copy`,
|
|
|
|
|
body: { id: questionId, quiz_id: quizId },
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [copiedQuestion];
|
|
|
|
|
} 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 deleteQuestion = async (
|
|
|
|
|
id: number,
|
|
|
|
|
): Promise<[DeleteQuestionResponse | null, string?]> => {
|
|
|
|
|
try {
|
|
|
|
|
const deletedQuestion = await makeRequest<
|
|
|
|
|
DeleteQuestionRequest,
|
|
|
|
|
DeleteQuestionResponse
|
|
|
|
|
>({
|
|
|
|
|
url: `${API_URL}/question/delete`,
|
|
|
|
|
body: { id },
|
|
|
|
|
method: "DELETE",
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return [deletedQuestion];
|
|
|
|
|
} 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
|
|
|
|
}
|
|
|
|
|
};
|
2023-11-02 16:45:28 +00:00
|
|
|
|
|
|
|
|
|
export const questionApi = {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
create: createQuestion,
|
|
|
|
|
getList: getQuestionList,
|
|
|
|
|
edit: editQuestion,
|
|
|
|
|
copy: copyQuestion,
|
|
|
|
|
delete: deleteQuestion,
|
2023-11-02 16:45:28 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const defaultGetQuestionListBody: GetQuestionListRequest = {
|
2023-12-31 02:53:25 +00:00
|
|
|
|
limit: 100,
|
|
|
|
|
offset: 0,
|
|
|
|
|
type: "",
|
2023-11-17 15:42:49 +00:00
|
|
|
|
};
|