feat: View linear logic
This commit is contained in:
parent
834f8bd556
commit
82d43a3bea
@ -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,28 +29,55 @@ 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 (question?.content.rule.parentId === "root") {
|
if (linear) {
|
||||||
setDisablePreviousButton(true);
|
const questionIndex = questions.findIndex(({ id }) => id === question.id);
|
||||||
|
|
||||||
|
const previousQuestion = questions[questionIndex - 1];
|
||||||
|
|
||||||
|
if (previousQuestion) {
|
||||||
|
setDisablePreviousButton(false);
|
||||||
|
} else {
|
||||||
|
setDisablePreviousButton(true);
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
setDisablePreviousButton(false);
|
if (question?.content.rule.parentId === "root") {
|
||||||
|
setDisablePreviousButton(true);
|
||||||
|
} else {
|
||||||
|
setDisablePreviousButton(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Логика для аргумента disabled у кнопки "Далее"
|
// Логика для аргумента disabled у кнопки "Далее"
|
||||||
const nextQuestionId = getNextQuestionId();
|
if (linear) {
|
||||||
if (nextQuestionId) {
|
const questionIndex = questions.findIndex(({ id }) => id === question.id);
|
||||||
setDisableNextButton(false);
|
|
||||||
} else {
|
const nextQuestion = questions[questionIndex + 1];
|
||||||
const nextQuestion = getQuestionByContentId(
|
|
||||||
question.content.rule.default
|
if (nextQuestion) {
|
||||||
);
|
|
||||||
if (nextQuestion?.type) {
|
|
||||||
setDisableNextButton(false);
|
setDisableNextButton(false);
|
||||||
} else {
|
} else {
|
||||||
setDisableNextButton(true);
|
setDisableNextButton(true);
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
const nextQuestionId = getNextQuestionId();
|
||||||
|
if (nextQuestionId) {
|
||||||
|
setDisableNextButton(false);
|
||||||
|
} else {
|
||||||
|
const nextQuestion = getQuestionByContentId(
|
||||||
|
question.content.rule.default
|
||||||
|
);
|
||||||
|
if (nextQuestion?.type) {
|
||||||
|
setDisableNextButton(false);
|
||||||
|
} else {
|
||||||
|
setDisableNextButton(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}, [question]);
|
}, [question]);
|
||||||
|
|
||||||
@ -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}
|
||||||
/>
|
/>
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user