feat: View linear logic

This commit is contained in:
IlyaDoronin 2023-12-06 15:09:25 +03:00
parent 834f8bd556
commit 82d43a3bea
3 changed files with 91 additions and 22 deletions

@ -68,7 +68,7 @@ export default function QuestionsPageCard({ question, draggableProps, isDragging
return ( return (
<> <>
<Paper <Paper
id={question.content.id} id={question.id}
data-cy="quiz-question-card" data-cy="quiz-question-card"
sx={{ sx={{
maxWidth: "796px", maxWidth: "796px",

@ -1,5 +1,5 @@
import { useState, useEffect } from "react"; import { useState, useEffect } from "react";
import { Box, Typography, Button, useTheme } from "@mui/material"; import { Box, Button, useTheme } from "@mui/material";
import { useQuizViewStore } from "@root/quizView"; import { useQuizViewStore } from "@root/quizView";
@ -11,11 +11,16 @@ import { getQuestionByContentId } from "@root/questions/actions";
import { enqueueSnackbar } from "notistack"; import { enqueueSnackbar } from "notistack";
type FooterProps = { type FooterProps = {
questions: AnyTypedQuizQuestion[];
setCurrentQuestion: (step: AnyTypedQuizQuestion) => void; setCurrentQuestion: (step: AnyTypedQuizQuestion) => void;
question: QuizQuestionBase; question: QuizQuestionBase;
}; };
export const Footer = ({ setCurrentQuestion, question }: FooterProps) => { export const Footer = ({
setCurrentQuestion,
questions,
question,
}: FooterProps) => {
const [disabledQuestionsId, setDisabledQuestionsId] = useState<Set<string>>( const [disabledQuestionsId, setDisabledQuestionsId] = useState<Set<string>>(
new Set() new Set()
); );
@ -24,16 +29,42 @@ export const Footer = ({ setCurrentQuestion, question }: FooterProps) => {
const [disableNextButton, setDisableNextButton] = useState<boolean>(false); const [disableNextButton, setDisableNextButton] = useState<boolean>(false);
const { answers } = useQuizViewStore(); const { answers } = useQuizViewStore();
const theme = useTheme(); const theme = useTheme();
const linear = !questions.find(
({ content }) => content.rule.parentId === "root"
);
useEffect(() => { useEffect(() => {
// Логика для аргумента disabled у кнопки "Назад" // Логика для аргумента disabled у кнопки "Назад"
if (linear) {
const questionIndex = questions.findIndex(({ id }) => id === question.id);
const previousQuestion = questions[questionIndex - 1];
if (previousQuestion) {
setDisablePreviousButton(false);
} else {
setDisablePreviousButton(true);
}
} else {
if (question?.content.rule.parentId === "root") { if (question?.content.rule.parentId === "root") {
setDisablePreviousButton(true); setDisablePreviousButton(true);
} else { } else {
setDisablePreviousButton(false); setDisablePreviousButton(false);
} }
}
// Логика для аргумента disabled у кнопки "Далее" // Логика для аргумента disabled у кнопки "Далее"
if (linear) {
const questionIndex = questions.findIndex(({ id }) => id === question.id);
const nextQuestion = questions[questionIndex + 1];
if (nextQuestion) {
setDisableNextButton(false);
} else {
setDisableNextButton(true);
}
} else {
const nextQuestionId = getNextQuestionId(); const nextQuestionId = getNextQuestionId();
if (nextQuestionId) { if (nextQuestionId) {
setDisableNextButton(false); setDisableNextButton(false);
@ -47,6 +78,7 @@ export const Footer = ({ setCurrentQuestion, question }: FooterProps) => {
setDisableNextButton(true); setDisableNextButton(true);
} }
} }
}
}, [question]); }, [question]);
const getNextQuestionId = () => { const getNextQuestionId = () => {
@ -79,6 +111,18 @@ export const Footer = ({ setCurrentQuestion, question }: FooterProps) => {
}; };
const followPreviousStep = () => { const followPreviousStep = () => {
if (linear) {
const questionIndex = questions.findIndex(({ id }) => id === question.id);
const previousQuestion = questions[questionIndex - 1];
if (previousQuestion) {
setCurrentQuestion(previousQuestion);
}
return;
}
if (question?.content.rule.parentId !== "root") { if (question?.content.rule.parentId !== "root") {
const parent = getQuestionByContentId(question?.content.rule.parentId); const parent = getQuestionByContentId(question?.content.rule.parentId);
if (parent?.type) { if (parent?.type) {
@ -92,6 +136,18 @@ export const Footer = ({ setCurrentQuestion, question }: FooterProps) => {
}; };
const followNextStep = () => { const followNextStep = () => {
if (linear) {
const questionIndex = questions.findIndex(({ id }) => id === question.id);
const nextQuestion = questions[questionIndex + 1];
if (nextQuestion) {
setCurrentQuestion(nextQuestion);
}
return;
}
const nextQuestionId = getNextQuestionId(); const nextQuestionId = getNextQuestionId();
if (nextQuestionId) { if (nextQuestionId) {

@ -13,11 +13,11 @@ import { Page } from "./questions/Page";
import { Rating } from "./questions/Rating"; import { Rating } from "./questions/Rating";
import { Footer } from "./Footer"; import { Footer } from "./Footer";
import { useState, type FC } from "react"; import { useState, type FC, useEffect } from "react";
import type { QuestionType } from "../../model/question/question"; import type { QuestionType } from "../../model/question/question";
import type { AnyTypedQuizQuestion } from "../../model/questionTypes/shared"; import type { AnyTypedQuizQuestion } from "../../model/questionTypes/shared";
import { useCurrentQuiz } from "@root/quizes/hooks"; import { useCurrentQuiz } from "@root/quizes/hooks";
import { getQuestionByContentId } from "@root/questions/actions"; import { getQuestionByContentId } from "@root/questions/actions";
type QuestionProps = { type QuestionProps = {
stepNumber: number; stepNumber: number;
@ -39,12 +39,24 @@ const QUESTIONS_MAP: any = {
rating: Rating, rating: Rating,
}; };
export const Question = ({ export const Question = ({ questions }: QuestionProps) => {
questions,
}: QuestionProps) => {
const quiz = useCurrentQuiz(); const quiz = useCurrentQuiz();
const [currentQuestion, setCurrentQuestion] = useState(getQuestionByContentId(quiz?.config.haveRoot || "")) const [currentQuestion, setCurrentQuestion] =
if (!currentQuestion) return <>не смог отобразить вопрос</> useState<AnyTypedQuizQuestion>();
useEffect(() => {
const nextQuestion = getQuestionByContentId(quiz?.config.haveRoot || "");
if (nextQuestion?.type) {
setCurrentQuestion(nextQuestion);
return;
}
setCurrentQuestion(questions[0]);
}, []);
if (!currentQuestion) return <>не смог отобразить вопрос</>;
const QuestionComponent = const QuestionComponent =
QUESTIONS_MAP[currentQuestion.type as Exclude<QuestionType, "nonselected">]; QUESTIONS_MAP[currentQuestion.type as Exclude<QuestionType, "nonselected">];
@ -60,9 +72,10 @@ export const Question = ({
margin: "0 auto", margin: "0 auto",
}} }}
> >
<QuestionComponent currentQuestion={currentQuestion}/> <QuestionComponent currentQuestion={currentQuestion} />
</Box> </Box>
<Footer <Footer
questions={questions}
question={currentQuestion} question={currentQuestion}
setCurrentQuestion={setCurrentQuestion} setCurrentQuestion={setCurrentQuestion}
/> />