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-05-31 16:41:18 +00:00
|
|
|
|
import MobileDetect from "mobile-detect";
|
2024-03-16 23:40:13 +00:00
|
|
|
|
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-18 14:13:52 +00:00
|
|
|
|
import { domain } from "../utils/defineDomain";
|
2024-01-29 11:16:21 +00:00
|
|
|
|
let SESSIONS = "";
|
|
|
|
|
|
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;
|
2024-05-31 16:41:18 +00:00
|
|
|
|
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") {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
browserUser = "Chrome";
|
2024-04-02 13:09:13 +00:00
|
|
|
|
} else if (Bowser.name === "Firefox") {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
browserUser = "Firefox";
|
2024-04-02 13:09:13 +00:00
|
|
|
|
} else if (Bowser.name === "Safari") {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
browserUser = "Safari";
|
2024-04-11 12:49:55 +00:00
|
|
|
|
} else if (Bowser.name === "Yandex Browser") {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
browserUser = "Yandex Browser";
|
|
|
|
|
} 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();
|
2024-05-31 16:41:18 +00:00
|
|
|
|
if (Device === null) {
|
|
|
|
|
Device = userAgent;
|
|
|
|
|
}
|
2024-03-16 23:40:13 +00:00
|
|
|
|
|
2024-04-17 16:08:40 +00:00
|
|
|
|
type PublicationMakeRequestParams = {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
url: string;
|
|
|
|
|
body: FormData;
|
|
|
|
|
method: "POST";
|
2024-05-01 08:12:06 +00:00
|
|
|
|
};
|
2024-04-17 16:08:40 +00:00
|
|
|
|
|
|
|
|
|
export const publicationMakeRequest = ({ url, body }: PublicationMakeRequestParams) => {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
return axios(url, {
|
|
|
|
|
data: body,
|
|
|
|
|
headers: {
|
|
|
|
|
"X-Sessionkey": SESSIONS,
|
|
|
|
|
"Content-Type": "multipart/form-data",
|
|
|
|
|
DeviceType: DeviceType,
|
|
|
|
|
Device: Device,
|
|
|
|
|
OS: OSDevice,
|
|
|
|
|
Browser: browserUser,
|
|
|
|
|
},
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2024-01-29 11:16:21 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export async function getData(quizId: string): Promise<{
|
2024-05-31 16:41:18 +00:00
|
|
|
|
data: GetQuizDataResponse | null;
|
|
|
|
|
isRecentlyCompleted: boolean;
|
|
|
|
|
error?: AxiosError;
|
2024-01-29 11:16:21 +00:00
|
|
|
|
}> {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
try {
|
|
|
|
|
const { data, headers } = await axios<GetQuizDataResponse>(domain + `/answer/settings`, {
|
|
|
|
|
method: "POST",
|
|
|
|
|
headers: {
|
|
|
|
|
"X-Sessionkey": SESSIONS,
|
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
|
DeviceType: DeviceType,
|
|
|
|
|
Device: Device,
|
|
|
|
|
OS: OSDevice,
|
|
|
|
|
Browser: userAgent,
|
|
|
|
|
},
|
|
|
|
|
data: {
|
|
|
|
|
quiz_id: quizId,
|
|
|
|
|
limit: 100,
|
|
|
|
|
page: 0,
|
|
|
|
|
need_config: true,
|
|
|
|
|
},
|
|
|
|
|
});
|
|
|
|
|
const sessions = JSON.parse(localStorage.getItem("sessions") || "{}");
|
2023-12-17 18:15:59 +00:00
|
|
|
|
|
2024-05-31 16:41:18 +00:00
|
|
|
|
if (typeof sessions[quizId] === "number") {
|
|
|
|
|
// unix время. Если меньше суток прошло - выводить ошибку, иначе пустить дальше
|
|
|
|
|
if (Date.now() - sessions[quizId] < 86400000) {
|
|
|
|
|
return { data, isRecentlyCompleted: true };
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-05-01 08:12:06 +00:00
|
|
|
|
|
2024-05-31 16:41:18 +00:00
|
|
|
|
SESSIONS = headers["x-sessionkey"] ? headers["x-sessionkey"] : SESSIONS;
|
2024-02-14 11:03:35 +00:00
|
|
|
|
|
2024-05-31 16:41:18 +00:00
|
|
|
|
return { data, isRecentlyCompleted: false };
|
|
|
|
|
} catch (nativeError) {
|
|
|
|
|
const error = nativeError as AxiosError;
|
2024-02-14 11:03:35 +00:00
|
|
|
|
|
2024-05-31 16:41:18 +00:00
|
|
|
|
return { data: null, isRecentlyCompleted: false, error: error };
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-02-14 11:03:35 +00:00
|
|
|
|
|
2024-05-31 16:41:18 +00:00
|
|
|
|
export async function getQuizData(quizId: string) {
|
|
|
|
|
if (!quizId) throw new Error("No quiz id");
|
|
|
|
|
|
|
|
|
|
const response = await getData(quizId);
|
|
|
|
|
const quizDataResponse = response.data;
|
|
|
|
|
|
|
|
|
|
if (response.error) {
|
|
|
|
|
throw 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-02-14 11:03:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-17 16:08:40 +00:00
|
|
|
|
type SendAnswerProps = {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
questionId: string;
|
|
|
|
|
body: string | string[];
|
|
|
|
|
qid: string;
|
|
|
|
|
preview: boolean;
|
2024-05-01 08:12:06 +00:00
|
|
|
|
};
|
2024-04-17 16:08:40 +00:00
|
|
|
|
|
|
|
|
|
export function sendAnswer({ questionId, body, qid, preview }: SendAnswerProps) {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
if (preview) return;
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
|
|
const answers = [
|
|
|
|
|
{
|
|
|
|
|
question_id: questionId,
|
|
|
|
|
content: body, //тут массив с ответом
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
formData.append("answers", JSON.stringify(answers));
|
|
|
|
|
console.log("QID", qid);
|
|
|
|
|
formData.append("qid", qid);
|
|
|
|
|
|
|
|
|
|
return publicationMakeRequest({
|
|
|
|
|
url: domain + `/answer/answer`,
|
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2023-12-17 21:28:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//body ={file, filename}
|
2024-04-17 16:08:40 +00:00
|
|
|
|
type SendFileParams = {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
questionId: string;
|
|
|
|
|
body: {
|
|
|
|
|
name: string;
|
|
|
|
|
file: File;
|
|
|
|
|
preview: boolean;
|
|
|
|
|
};
|
|
|
|
|
qid: string;
|
2024-05-01 08:12:06 +00:00
|
|
|
|
};
|
2024-04-17 16:08:40 +00:00
|
|
|
|
|
|
|
|
|
type Answer = {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
question_id: string;
|
|
|
|
|
content: string;
|
2024-04-17 16:08:40 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export function sendFile({ questionId, body, qid }: SendFileParams) {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
if (body.preview) return;
|
|
|
|
|
const formData = new FormData();
|
|
|
|
|
|
|
|
|
|
const answers: Answer[] = [
|
|
|
|
|
{
|
|
|
|
|
question_id: questionId,
|
|
|
|
|
content: "file:" + body.name,
|
|
|
|
|
},
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
formData.append("answers", JSON.stringify(answers));
|
|
|
|
|
formData.append(body.name, body.file);
|
|
|
|
|
console.log("QID", qid);
|
|
|
|
|
formData.append("qid", qid);
|
|
|
|
|
|
|
|
|
|
return publicationMakeRequest({
|
|
|
|
|
url: domain + `/answer/answer`,
|
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2023-12-17 21:28:57 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//форма контактов
|
2024-04-17 16:08:40 +00:00
|
|
|
|
export type SendFCParams = {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
questionId: string;
|
|
|
|
|
body: {
|
|
|
|
|
name?: string;
|
|
|
|
|
email?: string;
|
|
|
|
|
phone?: string;
|
|
|
|
|
address?: string;
|
|
|
|
|
customs?: Record<string, string>;
|
|
|
|
|
};
|
|
|
|
|
qid: string;
|
|
|
|
|
preview: boolean;
|
2024-05-01 08:12:06 +00:00
|
|
|
|
};
|
2024-04-17 16:08:40 +00:00
|
|
|
|
|
|
|
|
|
export function sendFC({ questionId, body, qid, preview }: SendFCParams) {
|
2024-05-31 16:41:18 +00:00
|
|
|
|
if (preview) return;
|
|
|
|
|
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: domain + `/answer/answer`,
|
|
|
|
|
body: formData,
|
|
|
|
|
method: "POST",
|
|
|
|
|
});
|
2024-01-29 11:16:21 +00:00
|
|
|
|
}
|