2024-02-14 11:03:35 +00:00
|
|
|
|
import { GetQuizDataResponse, parseQuizData } from "@model/api/getQuizData";
|
2024-01-29 11:16:21 +00:00
|
|
|
|
import axios from "axios";
|
2024-03-16 23:40:13 +00:00
|
|
|
|
import MobileDetect from 'mobile-detect';
|
|
|
|
|
import device from "current-device";
|
2024-01-29 11:16:21 +00:00
|
|
|
|
|
|
|
|
|
import type { AxiosError } from "axios";
|
2024-02-14 11:03:35 +00:00
|
|
|
|
import { replaceSpacesToEmptyLines } from "../components/ViewPublicationPage/tools/replaceSpacesToEmptyLines";
|
|
|
|
|
import { QuizSettings } from "@model/settingsData";
|
2024-04-01 00:45:31 +00:00
|
|
|
|
import * as Bowser from "bowser";
|
2024-04-02 13:09:13 +00:00
|
|
|
|
import useSWR from "swr";
|
2024-01-29 11:16:21 +00:00
|
|
|
|
let SESSIONS = "";
|
|
|
|
|
|
2024-02-22 09:36:34 +00:00
|
|
|
|
const domain = location.hostname === "hbpn.link" ? "" : "https://s.hbpn.link";
|
2024-02-16 18:18:34 +00:00
|
|
|
|
|
2024-03-16 23:40:13 +00:00
|
|
|
|
const md = new MobileDetect(window.navigator.userAgent);
|
2024-04-02 13:09:13 +00:00
|
|
|
|
const userAgent = navigator.userAgent;
|
2024-04-01 00:45:31 +00:00
|
|
|
|
//операционная система
|
2024-04-02 13:09:13 +00:00
|
|
|
|
let OSDevice: string | undefined;
|
|
|
|
|
if (userAgent.toLowerCase().includes("linux")) { OSDevice = "Linux"; }
|
|
|
|
|
if (userAgent.toLowerCase().includes("windows")) { OSDevice = "Windows"; }
|
|
|
|
|
if (/iPad|iPhone|iPod/.test(userAgent)) { OSDevice = "IOS"; }
|
|
|
|
|
if (userAgent.toLowerCase().includes("macintosh")) { OSDevice = "Mac OS"; }
|
|
|
|
|
if (OSDevice === undefined) { OSDevice = userAgent; }
|
2024-04-01 00:45:31 +00:00
|
|
|
|
|
|
|
|
|
//браузер
|
2024-04-02 13:09:13 +00:00
|
|
|
|
let browserUser: string;
|
|
|
|
|
if (Bowser.name === "Chrome") {
|
|
|
|
|
browserUser = "Chrome";
|
|
|
|
|
} else if (Bowser.name === "Firefox") {
|
|
|
|
|
browserUser = "Firefox";
|
|
|
|
|
} else if (Bowser.name === "Safari") {
|
|
|
|
|
browserUser = "Safari";
|
|
|
|
|
} else { browserUser = userAgent; }
|
2024-03-16 23:40:13 +00:00
|
|
|
|
|
2024-04-02 13:09:13 +00:00
|
|
|
|
const DeviceType = device.type;
|
2024-04-01 00:45:31 +00:00
|
|
|
|
|
2024-04-02 13:09:13 +00:00
|
|
|
|
let Device = md.mobile();
|
|
|
|
|
if (Device === null) { Device = userAgent; }
|
2024-03-16 23:40:13 +00:00
|
|
|
|
|
2024-01-29 11:16:21 +00:00
|
|
|
|
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",
|
2024-04-07 15:05:11 +00:00
|
|
|
|
"DeviceType" : DeviceType,
|
|
|
|
|
"Device" : Device,
|
|
|
|
|
"OS": OSDevice,
|
|
|
|
|
"Browser" : browserUser
|
2024-02-08 13:42:31 +00:00
|
|
|
|
},
|
|
|
|
|
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>(
|
2024-02-16 18:18:34 +00:00
|
|
|
|
domain + `/answer/settings`,
|
2024-02-08 13:42:31 +00:00
|
|
|
|
{
|
|
|
|
|
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",
|
2024-04-07 15:05:11 +00:00
|
|
|
|
"DeviceType" : DeviceType,
|
|
|
|
|
"Device" : Device,
|
|
|
|
|
"OS": OSDevice,
|
|
|
|
|
"Browser" : userAgent
|
2024-02-08 13:42:31 +00:00
|
|
|
|
},
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
2024-02-14 11:03:35 +00:00
|
|
|
|
export async function getQuizData(quizId: string) {
|
|
|
|
|
const response = await getData(quizId);
|
|
|
|
|
const quizDataResponse = response.data;
|
|
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
|
throw new Error(response.error);
|
|
|
|
|
}
|
|
|
|
|
if (!quizDataResponse) {
|
|
|
|
|
throw new Error("Quiz not found");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const quizSettings = replaceSpacesToEmptyLines(parseQuizData(quizDataResponse));
|
|
|
|
|
|
|
|
|
|
const res = JSON.parse(JSON.stringify({ data: quizSettings }).replaceAll(/\\" \\"/g, '""').replaceAll(/" "/g, '""')).data as QuizSettings;
|
|
|
|
|
res.recentlyCompleted = response.isRecentlyCompleted;
|
|
|
|
|
return res;
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-01 06:08:20 +00:00
|
|
|
|
export function sendAnswer({ questionId, body, qid, preview }: any) {
|
2024-04-02 13:09:13 +00:00
|
|
|
|
if (preview) return;
|
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));
|
2024-02-22 09:36:34 +00:00
|
|
|
|
console.log("QID", qid);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
formData.append("qid", qid);
|
|
|
|
|
|
|
|
|
|
return publicationMakeRequest({
|
2024-02-16 18:18:34 +00:00
|
|
|
|
url: domain + `/answer/answer`,
|
2024-02-08 13:42:31 +00:00
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2023-12-17 21:28:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//body ={file, filename}
|
2024-04-01 06:08:20 +00:00
|
|
|
|
export function sendFile({ questionId, body, qid, preview }: any) {
|
2024-04-02 13:09:13 +00:00
|
|
|
|
if (preview) return;
|
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);
|
2024-02-22 09:36:34 +00:00
|
|
|
|
console.log("QID", qid);
|
2024-02-08 13:42:31 +00:00
|
|
|
|
formData.append("qid", qid);
|
|
|
|
|
|
|
|
|
|
return publicationMakeRequest({
|
2024-02-16 18:18:34 +00:00
|
|
|
|
url: domain + `/answer/answer`,
|
2024-02-08 13:42:31 +00:00
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2023-12-17 21:28:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//форма контактов
|
2024-04-01 06:08:20 +00:00
|
|
|
|
export function sendFC({ questionId, body, qid, preview }: any) {
|
2024-04-02 13:09:13 +00:00
|
|
|
|
if (preview) return;
|
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({
|
2024-02-16 18:18:34 +00:00
|
|
|
|
url: domain + `/answer/answer`,
|
2024-02-08 13:42:31 +00:00
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2024-01-29 11:16:21 +00:00
|
|
|
|
}
|