2024-01-30 16:49:33 +00:00
|
|
|
|
import { GetQuizDataResponse } from "@model/api/getQuizData";
|
2024-01-29 11:16:21 +00:00
|
|
|
|
import axios from "axios";
|
|
|
|
|
|
|
|
|
|
import type { AxiosError } from "axios";
|
|
|
|
|
|
|
|
|
|
let SESSIONS = "";
|
|
|
|
|
|
|
|
|
|
export const publicationMakeRequest = ({ url, body }: any) => {
|
2024-02-08 13:42:31 +00:00
|
|
|
|
return axios(url, {
|
|
|
|
|
data: body,
|
|
|
|
|
headers: {
|
|
|
|
|
"X-Sessionkey": SESSIONS,
|
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
|
},
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2024-01-29 11:16:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function getData(quizId: string): Promise<{
|
2024-02-08 13:42:31 +00:00
|
|
|
|
data: GetQuizDataResponse | null;
|
|
|
|
|
isRecentlyCompleted: boolean;
|
|
|
|
|
error?: string;
|
2024-01-29 11:16:21 +00:00
|
|
|
|
}> {
|
2024-02-08 13:42:31 +00:00
|
|
|
|
try {
|
|
|
|
|
const { data, headers } = await axios<GetQuizDataResponse>(
|
|
|
|
|
`https://s.hbpn.link/answer/settings`,
|
|
|
|
|
{
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
2024-02-10 19:25:08 +00:00
|
|
|
|
"X-Sessionkey": SESSIONS,
|
2024-02-08 13:42:31 +00:00
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
quiz_id: quizId,
|
|
|
|
|
limit: 100,
|
|
|
|
|
page: 0,
|
|
|
|
|
need_config: true,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
const sessions = JSON.parse(localStorage.getItem("sessions") || "{}");
|
|
|
|
|
|
|
|
|
|
if (typeof sessions[quizId] === "number") {
|
|
|
|
|
// unix время. Если меньше суток прошло - выводить ошибку, иначе пустить дальше
|
|
|
|
|
if (Date.now() - sessions[quizId] < 86400000) {
|
|
|
|
|
return { data, isRecentlyCompleted: true };
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-11 23:35:00 +00:00
|
|
|
|
SESSIONS = headers["x-sessionkey"] ? headers["x-sessionkey"] : SESSIONS;
|
2024-02-08 13:42:31 +00:00
|
|
|
|
|
|
|
|
|
return { data, isRecentlyCompleted: false };
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const error = nativeError as AxiosError;
|
|
|
|
|
|
|
|
|
|
return { data: null, isRecentlyCompleted: false, error: error.message };
|
2024-01-29 11:16:21 +00:00
|
|
|
|
}
|
2023-12-17 18:15:59 +00:00
|
|
|
|
}
|
|
|
|
|
|
2023-12-17 21:28:57 +00:00
|
|
|
|
export function sendAnswer({ questionId, body, qid }: any) {
|
2024-02-08 13:42:31 +00:00
|
|
|
|
const formData = new FormData();
|
2024-02-10 12:41:20 +00:00
|
|
|
|
|
2024-02-08 13:42:31 +00:00
|
|
|
|
const answers = [
|
|
|
|
|
{
|
|
|
|
|
question_id: questionId,
|
|
|
|
|
content: body, //тут массив с ответом
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
formData.append("answers", JSON.stringify(answers));
|
|
|
|
|
formData.append("qid", qid);
|
|
|
|
|
|
|
|
|
|
return publicationMakeRequest({
|
|
|
|
|
url: `https://s.hbpn.link/answer/answer`,
|
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2023-12-17 21:28:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//body ={file, filename}
|
|
|
|
|
export function sendFile({ questionId, body, qid }: any) {
|
2024-02-08 13:42:31 +00:00
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
|
|
const answers: any = [
|
|
|
|
|
{
|
|
|
|
|
question_id: questionId,
|
|
|
|
|
content: "file:" + body.name,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
formData.append("answers", JSON.stringify(answers));
|
|
|
|
|
formData.append(body.name, body.file);
|
|
|
|
|
formData.append("qid", qid);
|
|
|
|
|
|
|
|
|
|
return publicationMakeRequest({
|
|
|
|
|
url: `https://s.hbpn.link/answer/answer`,
|
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2023-12-17 21:28:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//форма контактов
|
|
|
|
|
export function sendFC({ questionId, body, qid }: any) {
|
2024-02-08 13:42:31 +00:00
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
|
|
// const keysBody = Object.keys(body)
|
|
|
|
|
// const content:any = {}
|
|
|
|
|
// fields.forEach((key) => {
|
|
|
|
|
// if (keysBody.includes(key)) content[key] = body.key
|
|
|
|
|
// })
|
|
|
|
|
|
|
|
|
|
const answers = [
|
|
|
|
|
{
|
|
|
|
|
question_id: questionId,
|
|
|
|
|
content: JSON.stringify(body),
|
|
|
|
|
result: true,
|
|
|
|
|
qid,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
formData.append("answers", JSON.stringify(answers));
|
|
|
|
|
formData.append("qid", qid);
|
|
|
|
|
|
|
|
|
|
return publicationMakeRequest({
|
|
|
|
|
url: `https://s.hbpn.link/answer/answer`,
|
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2024-01-29 11:16:21 +00:00
|
|
|
|
}
|