diff --git a/src/api/quizRelase.ts b/src/api/quizRelase.ts index 4a1cf26..cfa6541 100644 --- a/src/api/quizRelase.ts +++ b/src/api/quizRelase.ts @@ -1,19 +1,98 @@ import { makeRequest } from "@frontend/kitui"; -function get(quizId: string) { +export function getData(quizId: string) { return makeRequest({ url: `https://squiz.pena.digital/answer/settings`, - body: { + body: { quiz_id: quizId, limit: 100, page: 0, need_config: true, - }, + }, method: "POST", }); } -export const relaseApi = { - get: get, -}; +export function sendAnswer({ questionId, body, qid }: any) { + const formData = new FormData(); + + const answers = [{ + question_id: questionId, + content: body, //тут массив с ответом + qid + }] + formData.append("answers", JSON.stringify(answers)); + + return makeRequest({ + url: `https://squiz.pena.digital/answer/answer`, + body: formData, + method: "POST", + }); +} + +//body ={file, filename} +export function sendFile({ questionId, body, qid }: any) { + const formData = new FormData(); + + const fd: any = { + question_id: questionId, + content: body.name, + qid + } + + fd[body.name] = body.filen //target.files[0] + + const answers = [fd] + formData.append("answers", JSON.stringify(answers)); + + return makeRequest({ + url: `https://squiz.pena.digital/answer/answer`, + body: formData, + method: "POST", + }); +} + + +const fields = [ + "name", + "email", + "phone", + "adress", + "telegram", + "wechat", + "viber", + "vk", + "skype", + "whatsup", + "messenger", + "text" +] + +//форма контактов +export function sendFC({ questionId, body, qid }: any) { + console.log("start fetch") + 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: body, + result: true, + qid + }] + + formData.append("answers", JSON.stringify(answers)); + + return makeRequest({ + url: `https://squiz.pena.digital/answer/answer`, + body: formData, + method: "POST", + }); +} diff --git a/src/pages/ViewPublicationPage/ApologyPage.tsx b/src/pages/ViewPublicationPage/ApologyPage.tsx index b9357d0..ac27537 100644 --- a/src/pages/ViewPublicationPage/ApologyPage.tsx +++ b/src/pages/ViewPublicationPage/ApologyPage.tsx @@ -6,7 +6,8 @@ export const ApologyPage = ({message}:{message: string}) => { sx={{ display: "flex", alignItems: "center", - justifyContent: "center" + justifyContent: "center", + height: "100vh" }} > {settings?.cfg.formContact.title || "Заполните форму, чтобы получить результаты теста"} - + { settings?.cfg.formContact.desc && - - {settings?.cfg.formContact.desc} - + + {settings?.cfg.formContact.desc} + } @@ -98,7 +101,7 @@ export const ContactForm = ({ my: "20px" }} > - + @@ -136,33 +139,92 @@ export const ContactForm = ({ ); }; -const Inputs = () => { - const { settings } = useQuestionsStore() +const Inputs = (currentQuestion: any) => { + const { settings, items } = useQuestionsStore() - let someUsed:any = [] - const Icons = icons.map((data) => { + const [name, setName] = useState("") + const [email, setEmail] = useState("") + const [phone, setPhone] = useState("") + const [text, setText] = useState("") + const [adress, setAdress] = useState("") + + + const inputHC = useDebouncedCallback(async () => { + console.log("start input") + + const body = {} //@ts-ignore - const FC:any = settings?.cfg.formContact[data.type] - if (FC.used) someUsed.push() - return - }) + if (name.length > 0) body.name = name + //@ts-ignore + if (email.length > 0) body.email = email + //@ts-ignore + if (phone.length > 0) body.phone = phone + //@ts-ignore + if (text.length > 0) body.text = text + //@ts-ignore + if (adress.length > 0) body.adress = adress - if (someUsed.length) { - return <>{someUsed} + console.log(body) + if (Object.keys(body).length > 0) { + try { + await sendFC({ + questionId: currentQuestion.id, + body: body, + //@ts-ignore + qid: settings.qid + }) + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + + } + }, 1000); + + useEffect(() => { + inputHC() + }, [name, email, phone, text, adress]) + //@ts-ignore + const FC: any = settings?.cfg.formContact + + //@ts-ignore + const Name = setName(target.value)} id={name} title={FC["name"].innerText || "Введите имя"} desc={FC["name"].text || "имя"} Icon={NameIcon} /> + //@ts-ignore + const Email = setEmail(target.value)} id={email} title={FC["email"].innerText || "Введите Email"} desc={FC["email"].text || "Email"} Icon={EmailIcon} /> + //@ts-ignore + const Phone = setPhone(target.value)} id={phone} title={FC["phone"].innerText || "Введите номер телефона"} desc={FC["phone"].text || "номер телефона"} Icon={PhoneIcon} /> + //@ts-ignore + const Text = setText(target.value)} id={text} title={FC["text"].innerText || "Введите фамилию"} desc={FC["text"].text || "фамилию"} Icon={TextIcon} /> + //@ts-ignore + const Adress = setAdress(target.value)} id={adress} title={FC["address"].innerText || "Введите адрес"} desc={FC["address"].text || "адрес"} Icon={AddressIcon} /> + + + + //@ts-ignore + if (items.some((data) => data.used)) { + return <> + {FC["name"].used ? Name : <>} + {FC["email"].used ? Email : <>} + {FC["phone"].used ? Phone : <>} + {FC["text"].used ? Text : <>} + {FC["address"].used ? Adress : <>} + } else { return <> - {Icons[0]} - {Icons[1]} - {Icons[2]} + {Name} + {Email} + {Phone} } } -const CustomInput = ({ title, desc, Icon }: any) => { +const CustomInput = ({ title, desc, Icon, onChange }: any) => { return {title} { const { settings, cnt, items } = useQuestionsStore() const [visualStartPage, setVisualStartPage] = useState(); + const [errormessage, setErrormessage] = useState(""); useEffect(() => { async function get() { - const data = await relaseApi.get("c1ee11d2-ed5a-47d1-b500-bda6fd442114") + try { + const data = await getData("c1ee11d2-ed5a-47d1-b500-bda6fd442114") //@ts-ignore const settings = data.settings console.log(data) @@ -51,6 +53,12 @@ export const ViewPage = () => { } console.log(parseData) useQuestionsStore.setState(parseData) + + } catch (e) { + + //@ts-ignore + if (e?.response?.status === 423) setErrormessage("квиз не активирован") + } } get() },[]) @@ -76,6 +84,7 @@ console.log(items) console.log(filteredQuestions) + if (errormessage) return if (visualStartPage === undefined) return ; if (cnt === 0) return return ( diff --git a/src/pages/ViewPublicationPage/questions/Date.tsx b/src/pages/ViewPublicationPage/questions/Date.tsx index 199e71e..20c381d 100644 --- a/src/pages/ViewPublicationPage/questions/Date.tsx +++ b/src/pages/ViewPublicationPage/questions/Date.tsx @@ -6,12 +6,16 @@ import { useQuizViewStore, updateAnswer } from "@root/quizView/store"; import type { QuizQuestionDate } from "../../../model/questionTypes/date"; import CalendarIcon from "@icons/CalendarIcon"; +import { enqueueSnackbar } from "notistack"; +import { sendAnswer } from "@api/quizRelase"; +import { useQuestionsStore } from "@root/quizData/store" type DateProps = { currentQuestion: QuizQuestionDate; }; export const Date = ({ currentQuestion }: DateProps) => { + const { settings } = useQuestionsStore() const { answers } = useQuizViewStore(); const answer = answers.find( ({ questionId }) => questionId === currentQuestion.id @@ -38,21 +42,39 @@ export const Date = ({ currentQuestion }: DateProps) => { ? new window.Date(`${month}.${day}.${year}`) : new window.Date() )} - onChange={(date) => { + onChange={async (date) => { if (!date) { return; } - updateAnswer( - currentQuestion.id, - String( - new window.Date(date.toDate()).toLocaleDateString("ru-RU", { + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: new window.Date(date.toDate()).toLocaleDateString("ru-RU", { year: "numeric", month: "2-digit", day: "2-digit", - }) - ) - ); + }), + //@ts-ignore + qid: settings.qid + }) + + updateAnswer( + currentQuestion.id, + String( + new window.Date(date.toDate()).toLocaleDateString("ru-RU", { + year: "numeric", + month: "2-digit", + day: "2-digit", + }) + ) + ); + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + }} slotProps={{ openPickerButton: { diff --git a/src/pages/ViewPublicationPage/questions/Emoji.tsx b/src/pages/ViewPublicationPage/questions/Emoji.tsx index a75dee5..3ce3112 100644 --- a/src/pages/ViewPublicationPage/questions/Emoji.tsx +++ b/src/pages/ViewPublicationPage/questions/Emoji.tsx @@ -14,12 +14,16 @@ import RadioCheck from "@ui_kit/RadioCheck"; import RadioIcon from "@ui_kit/RadioIcon"; import type { QuizQuestionEmoji } from "../../../model/questionTypes/emoji"; +import { enqueueSnackbar } from "notistack"; +import { sendAnswer } from "@api/quizRelase"; +import { useQuestionsStore } from "@root/quizData/store" type EmojiProps = { currentQuestion: QuizQuestionEmoji; }; export const Emoji = ({ currentQuestion }: EmojiProps) => { + const { settings } = useQuestionsStore() const { answers } = useQuizViewStore(); const theme = useTheme(); const { answer } = @@ -94,13 +98,29 @@ export const Emoji = ({ currentQuestion }: EmojiProps) => { gap: "10px", }} value={index} - onClick={(event) => { + onClick={async (event) => { event.preventDefault(); - updateAnswer( - currentQuestion.id, - currentQuestion.content.variants[index].id - ); + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: currentQuestion.content.variants[index].id, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer( + currentQuestion.id, + currentQuestion.content.variants[index].id + ); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + + if (answer === currentQuestion.content.variants[index].id) { deleteAnswer(currentQuestion.id); diff --git a/src/pages/ViewPublicationPage/questions/File.tsx b/src/pages/ViewPublicationPage/questions/File.tsx index f8a5f17..14002e9 100644 --- a/src/pages/ViewPublicationPage/questions/File.tsx +++ b/src/pages/ViewPublicationPage/questions/File.tsx @@ -15,6 +15,9 @@ import type { ChangeEvent } from "react"; import type { QuizQuestionFile } from "../../../model/questionTypes/file"; import type { DragEvent } from "react"; import type { UploadFileType } from "@model/questionTypes/file"; +import { enqueueSnackbar } from "notistack"; +import { sendFile } from "@api/quizRelase"; +import { useQuestionsStore } from "@root/quizData/store" type FileProps = { currentQuestion: QuizQuestionFile; @@ -38,19 +41,39 @@ export const UPLOAD_FILE_DESCRIPTIONS_MAP: Record< } as const; export const File = ({ currentQuestion }: FileProps) => { + const { settings } = useQuestionsStore() const { answers } = useQuizViewStore(); const answer = answers.find( ({ questionId }) => questionId === currentQuestion.id )?.answer as string; const theme = useTheme(); - const uploadFile = ({ target }: ChangeEvent) => { + const uploadFile = async ({ target }: ChangeEvent) => { const file = target.files?.[0]; - +console.log(file) if (file) { - updateAnswer( - currentQuestion.id, - `${file.name}|${URL.createObjectURL(file)}` - ); + + try { + + await sendFile({ + questionId: currentQuestion.id, + body: { + file: `${file.name}|${URL.createObjectURL(file)}`, + name: file.name + }, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer( + currentQuestion.id, + `${file.name}|${URL.createObjectURL(file)}` + ); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + } }; @@ -84,7 +107,9 @@ export const File = ({ currentQuestion }: FileProps) => { { - updateAnswer(currentQuestion.id, ""); + + updateAnswer(currentQuestion.id, ""); + }} > diff --git a/src/pages/ViewPublicationPage/questions/Images.tsx b/src/pages/ViewPublicationPage/questions/Images.tsx index 4817c61..ba2fc16 100644 --- a/src/pages/ViewPublicationPage/questions/Images.tsx +++ b/src/pages/ViewPublicationPage/questions/Images.tsx @@ -13,12 +13,16 @@ import RadioCheck from "@ui_kit/RadioCheck"; import RadioIcon from "@ui_kit/RadioIcon"; import type { QuizQuestionImages } from "../../../model/questionTypes/images"; +import { enqueueSnackbar } from "notistack"; +import { sendAnswer } from "@api/quizRelase"; +import { useQuestionsStore } from "@root/quizData/store" type ImagesProps = { currentQuestion: QuizQuestionImages; }; export const Images = ({ currentQuestion }: ImagesProps) => { + const { settings } = useQuestionsStore() const { answers } = useQuizViewStore(); const theme = useTheme(); const { answer } = @@ -64,13 +68,28 @@ export const Images = ({ currentQuestion }: ImagesProps) => { borderRadius: "5px", border: `1px solid ${theme.palette.grey2.main}`, }} - onClick={(event) => { + onClick={async(event) => { event.preventDefault(); - updateAnswer( - currentQuestion.id, - currentQuestion.content.variants[index].id - ); + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: currentQuestion.content.variants[index].id, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer( + currentQuestion.id, + currentQuestion.content.variants[index].id + ); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + if (answer === currentQuestion.content.variants[index].id) { deleteAnswer(currentQuestion.id); diff --git a/src/pages/ViewPublicationPage/questions/Number.tsx b/src/pages/ViewPublicationPage/questions/Number.tsx index 714f5e8..905995c 100644 --- a/src/pages/ViewPublicationPage/questions/Number.tsx +++ b/src/pages/ViewPublicationPage/questions/Number.tsx @@ -8,29 +8,63 @@ import { CustomSlider } from "@ui_kit/CustomSlider"; import { useQuizViewStore, updateAnswer } from "@root/quizView/store"; import type { QuizQuestionNumber } from "../../../model/questionTypes/number"; +import { enqueueSnackbar } from "notistack"; +import { sendAnswer } from "@api/quizRelase"; +import { useQuestionsStore } from "@root/quizData/store" type NumberProps = { currentQuestion: QuizQuestionNumber; }; export const Number = ({ currentQuestion }: NumberProps) => { + const { settings } = useQuestionsStore() const [minRange, setMinRange] = useState("0"); const [maxRange, setMaxRange] = useState("100000000000"); const theme = useTheme(); const { answers } = useQuizViewStore(); - const updateMinRangeDebounced = useDebouncedCallback((value, crowded = false) => { + const updateMinRangeDebounced = useDebouncedCallback(async (value, crowded = false) => { if (crowded) { setMinRange(maxRange); } - updateAnswer(currentQuestion.id, value); + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: value, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer(currentQuestion.id, value); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + + }, 1000); - const updateMaxRangeDebounced = useDebouncedCallback((value, crowded = false) => { + const updateMaxRangeDebounced = useDebouncedCallback(async(value, crowded = false) => { if (crowded) { setMaxRange(minRange); } + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: value, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer(currentQuestion.id, value); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } - updateAnswer(currentQuestion.id, value); }, 1000); const answer = answers.find(({ questionId }) => questionId === currentQuestion.id)?.answer as string; @@ -73,9 +107,29 @@ export const Number = ({ currentQuestion }: NumberProps) => { min={min} max={max} step={currentQuestion.content.step || 1} - onChange={(_, value) => { + onChange={async (_, value) => { const range = String(value).replace(",", "—"); - updateAnswer(currentQuestion.id, range); + + + + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: range, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer(currentQuestion.id, range); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + + + }} onChangeCommitted={(_, value) => { if (currentQuestion.content.chooseRange) { @@ -91,15 +145,41 @@ export const Number = ({ currentQuestion }: NumberProps) => { { - updateAnswer( - currentQuestion.id, - window.Number(target.value) > max + onChange={async({ target }) => { + + + + + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: window.Number(target.value) > max ? String(max) : window.Number(target.value) < min ? String(min) - : target.value - ); + : target.value, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer( + currentQuestion.id, + window.Number(target.value) > max + ? String(max) + : window.Number(target.value) < min + ? String(min) + : target.value + ); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + + + + }} sx={{ maxWidth: "80px", diff --git a/src/pages/ViewPublicationPage/questions/Rating.tsx b/src/pages/ViewPublicationPage/questions/Rating.tsx index 5de9fa7..fc82b6d 100644 --- a/src/pages/ViewPublicationPage/questions/Rating.tsx +++ b/src/pages/ViewPublicationPage/questions/Rating.tsx @@ -16,6 +16,9 @@ import HashtagIcon from "@icons/questionsPage/hashtagIcon"; import StarIconMini from "@icons/questionsPage/StarIconMini"; import type { QuizQuestionRating } from "../../../model/questionTypes/rating"; +import { enqueueSnackbar } from "notistack"; +import { sendAnswer } from "@api/quizRelase"; +import { useQuestionsStore } from "@root/quizData/store" type RatingProps = { currentQuestion: QuizQuestionRating; @@ -53,6 +56,7 @@ const buttonRatingForm = [ ]; export const Rating = ({ currentQuestion }: RatingProps) => { + const { settings } = useQuestionsStore() const { answers } = useQuizViewStore(); const theme = useTheme(); const { answer } = @@ -85,8 +89,26 @@ export const Rating = ({ currentQuestion }: RatingProps) => { > - updateAnswer(currentQuestion.id, String(value)) + onChange={async (_, value) => { + + + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: String(value), + //@ts-ignore + qid: settings.qid + }) + + updateAnswer(currentQuestion.id, String(value)) + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + } + } sx={{ height: "50px", gap: "15px" }} max={currentQuestion.content.steps} diff --git a/src/pages/ViewPublicationPage/questions/Select.tsx b/src/pages/ViewPublicationPage/questions/Select.tsx index 445efd8..efe9587 100644 --- a/src/pages/ViewPublicationPage/questions/Select.tsx +++ b/src/pages/ViewPublicationPage/questions/Select.tsx @@ -5,12 +5,16 @@ import { Select as SelectComponent } from "../tools//Select"; import { useQuizViewStore, updateAnswer, deleteAnswer } from "@root/quizView/store"; import type { QuizQuestionSelect } from "../../../model/questionTypes/select"; +import { enqueueSnackbar } from "notistack"; +import { sendAnswer } from "@api/quizRelase"; +import { useQuestionsStore } from "@root/quizData/store" type SelectProps = { currentQuestion: QuizQuestionSelect; }; export const Select = ({ currentQuestion }: SelectProps) => { + const { settings } = useQuestionsStore() const { answers } = useQuizViewStore(); const { answer } = answers.find( @@ -32,14 +36,30 @@ export const Select = ({ currentQuestion }: SelectProps) => { placeholder={currentQuestion.content.default} activeItemIndex={answer ? Number(answer) : -1} items={currentQuestion.content.variants.map(({ answer }) => answer)} - onChange={(_, value) => { + onChange={async(_, value) => { if (value < 0) { deleteAnswer(currentQuestion.id); return; } - updateAnswer(currentQuestion.id, String(value)); + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: String(value), + //@ts-ignore + qid: settings.qid + }) + + updateAnswer(currentQuestion.id, String(value)); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + + }} /> diff --git a/src/pages/ViewPublicationPage/questions/Text.tsx b/src/pages/ViewPublicationPage/questions/Text.tsx index 2863028..4f9e14c 100644 --- a/src/pages/ViewPublicationPage/questions/Text.tsx +++ b/src/pages/ViewPublicationPage/questions/Text.tsx @@ -5,12 +5,16 @@ import CustomTextField from "@ui_kit/CustomTextField"; import { useQuizViewStore, updateAnswer } from "@root/quizView/store"; import type { QuizQuestionText } from "../../../model/questionTypes/text"; +import { enqueueSnackbar } from "notistack"; +import { useQuestionsStore } from "@root/quizData/store" +import { sendAnswer } from "@api/quizRelase"; type TextProps = { currentQuestion: QuizQuestionText; }; export const Text = ({ currentQuestion }: TextProps) => { + const { settings } = useQuestionsStore() const { answers } = useQuizViewStore(); const { answer } = answers.find(({ questionId }) => questionId === currentQuestion.id) ?? {}; @@ -29,7 +33,24 @@ export const Text = ({ currentQuestion }: TextProps) => { placeholder={currentQuestion.content.placeholder} //@ts-ignore value={answer || ""} - onChange={({ target }) => updateAnswer(currentQuestion.id, target.value)} + onChange={async ({ target }) => { + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: target.value, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer(currentQuestion.id, target.value) + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + } + } /> diff --git a/src/pages/ViewPublicationPage/questions/Variant.tsx b/src/pages/ViewPublicationPage/questions/Variant.tsx index 11f390f..2e14116 100644 --- a/src/pages/ViewPublicationPage/questions/Variant.tsx +++ b/src/pages/ViewPublicationPage/questions/Variant.tsx @@ -25,6 +25,9 @@ import { CheckboxIcon } from "@icons/Checkbox"; import type { QuizQuestionVariant } from "../../../model/questionTypes/variant"; import type { QuestionVariant } from "../../../model/questionTypes/shared"; +import { enqueueSnackbar } from "notistack"; +import { sendAnswer } from "@api/quizRelase"; +import { useQuestionsStore } from "@root/quizData/store" type VariantProps = { stepNumber: number; @@ -40,6 +43,7 @@ type VariantItemProps = { }; export const Variant = ({ currentQuestion }: VariantProps) => { + const { settings } = useQuestionsStore() const { answers, ownVariants } = useQuizViewStore(); const { answer } = answers.find( @@ -159,24 +163,55 @@ const VariantItem = ({ } //@ts-ignore label={own ? : variant.answer} - onClick={(event) => { + onClick={async(event) => { event.preventDefault(); const variantId = currentQuestion.content.variants[index].id; if (currentQuestion.content.multi) { const currentAnswer = typeof answer !== "string" ? answer || [] : []; - updateAnswer( - currentQuestion.id, - currentAnswer.includes(variantId) + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: currentAnswer.includes(variantId) ? currentAnswer?.filter((item) => item !== variantId) - : [...currentAnswer, variantId] - ); + : [...currentAnswer, variantId], + //@ts-ignore + qid: settings.qid + }) + + updateAnswer( + currentQuestion.id, + currentAnswer.includes(variantId) + ? currentAnswer?.filter((item) => item !== variantId) + : [...currentAnswer, variantId] + ); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + return; } - updateAnswer(currentQuestion.id, variantId); + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: variantId, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer(currentQuestion.id, variantId); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } if (answer === variantId) { deleteAnswer(currentQuestion.id); diff --git a/src/pages/ViewPublicationPage/questions/Varimg.tsx b/src/pages/ViewPublicationPage/questions/Varimg.tsx index 8b96b0e..351f274 100644 --- a/src/pages/ViewPublicationPage/questions/Varimg.tsx +++ b/src/pages/ViewPublicationPage/questions/Varimg.tsx @@ -9,18 +9,22 @@ import { import gag from "./gag.png" +import { useQuestionsStore } from "@root/quizData/store" import { useQuizViewStore, updateAnswer, deleteAnswer } from "@root/quizView/store"; import RadioCheck from "@ui_kit/RadioCheck"; import RadioIcon from "@ui_kit/RadioIcon"; import type { QuizQuestionVarImg } from "../../../model/questionTypes/varimg"; +import { enqueueSnackbar } from "notistack"; +import { sendAnswer } from "@api/quizRelase"; type VarimgProps = { currentQuestion: QuizQuestionVarImg; }; export const Varimg = ({ currentQuestion }: VarimgProps) => { + const { settings } = useQuestionsStore() const { answers } = useQuizViewStore(); const theme = useTheme(); const { answer } = @@ -63,13 +67,30 @@ export const Varimg = ({ currentQuestion }: VarimgProps) => { display: "flex", }} value={index} - onClick={(event) => { + onClick={async(event) => { event.preventDefault(); - updateAnswer( - currentQuestion.id, - currentQuestion.content.variants[index].id - ); + + + try { + + await sendAnswer({ + questionId: currentQuestion.id, + body: currentQuestion.content.variants[index].id, + //@ts-ignore + qid: settings.qid + }) + + updateAnswer( + currentQuestion.id, + currentQuestion.content.variants[index].id + ); + + } catch (e) { + console.log(e) + enqueueSnackbar("ответ не был засчитан") + } + if (answer === currentQuestion.content.variants[index].id) { deleteAnswer(currentQuestion.id); diff --git a/src/utils/hooks/useGetSettings.ts b/src/utils/hooks/useGetSettings.ts index c49f062..0836850 100644 --- a/src/utils/hooks/useGetSettings.ts +++ b/src/utils/hooks/useGetSettings.ts @@ -1,7 +1,7 @@ import { useEffect, useLayoutEffect, useRef, useState } from "react" import { useQuestionsStore } from "@root/quizData/store"; -import { relaseApi } from "@api/quizRelase" +import { getData } from "@api/quizRelase" interface SettingsGetter { quizId: string @@ -13,7 +13,7 @@ export function useGetSettings(quizId: string) { useEffect(() => { async function get() { - const data = await relaseApi.get(quizId) + const data = await getData(quizId) //@ts-ignore const settings = data.settings console.log(data)