176 lines
5.1 KiB
TypeScript
176 lines
5.1 KiB
TypeScript
import { GetQuizDataResponse, parseQuizData } from "@model/api/getQuizData";
|
||
import axios from "axios";
|
||
import MobileDetect from 'mobile-detect';
|
||
import device from "current-device";
|
||
|
||
import type { AxiosError } from "axios";
|
||
import { replaceSpacesToEmptyLines } from "../components/ViewPublicationPage/tools/replaceSpacesToEmptyLines";
|
||
import { QuizSettings } from "@model/settingsData";
|
||
|
||
let SESSIONS = "";
|
||
|
||
const domain = location.hostname === "hbpn.link" ? "" : "https://s.hbpn.link";
|
||
|
||
const md = new MobileDetect(window.navigator.userAgent);
|
||
|
||
const userAgent = navigator.userAgent
|
||
let OSDevice = md.os()
|
||
if (OSDevice === null) {OSDevice = userAgent}
|
||
|
||
const DeviceType = device.type
|
||
let Device = md.mobile()
|
||
if (Device === null) {Device = userAgent}
|
||
|
||
export const publicationMakeRequest = ({ url, body }: any) => {
|
||
return axios(url, {
|
||
data: body,
|
||
headers: {
|
||
"X-Sessionkey": SESSIONS,
|
||
"Content-Type": "multipart/form-data",
|
||
"DeviceType" : DeviceType,
|
||
"Device" : Device,
|
||
"OS": OSDevice,
|
||
"Browser" : userAgent
|
||
},
|
||
method: "POST",
|
||
});
|
||
};
|
||
|
||
export async function getData(quizId: string): Promise<{
|
||
data: GetQuizDataResponse | null;
|
||
isRecentlyCompleted: boolean;
|
||
error?: string;
|
||
}> {
|
||
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") || "{}");
|
||
|
||
if (typeof sessions[quizId] === "number") {
|
||
// unix время. Если меньше суток прошло - выводить ошибку, иначе пустить дальше
|
||
if (Date.now() - sessions[quizId] < 86400000) {
|
||
return { data, isRecentlyCompleted: true };
|
||
}
|
||
}
|
||
|
||
SESSIONS = headers["x-sessionkey"] ? headers["x-sessionkey"] : SESSIONS;
|
||
|
||
return { data, isRecentlyCompleted: false };
|
||
} catch (nativeError) {
|
||
const error = nativeError as AxiosError;
|
||
|
||
return { data: null, isRecentlyCompleted: false, error: error.message };
|
||
}
|
||
}
|
||
|
||
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;
|
||
}
|
||
|
||
export function sendAnswer({ questionId, body, qid, preview }: any) {
|
||
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",
|
||
});
|
||
}
|
||
|
||
//body ={file, filename}
|
||
export function sendFile({ questionId, body, qid, preview }: any) {
|
||
if (preview) return
|
||
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);
|
||
console.log("QID", qid);
|
||
formData.append("qid", qid);
|
||
|
||
return publicationMakeRequest({
|
||
url: domain + `/answer/answer`,
|
||
body: formData,
|
||
method: "POST",
|
||
});
|
||
}
|
||
|
||
//форма контактов
|
||
export function sendFC({ questionId, body, qid, preview }: any) {
|
||
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",
|
||
});
|
||
}
|