Merge branch 'dev' into 'staging'
Dev See merge request frontend/squzanswerer!55
This commit is contained in:
commit
1ef93715f3
@ -4,6 +4,9 @@ import type {
|
|||||||
QuestionHint,
|
QuestionHint,
|
||||||
} from "./shared";
|
} from "./shared";
|
||||||
|
|
||||||
|
interface ResultQuestionBranchingRule extends QuestionBranchingRule {
|
||||||
|
minScore?: number
|
||||||
|
}
|
||||||
export interface QuizQuestionResult extends QuizQuestionBase {
|
export interface QuizQuestionResult extends QuizQuestionBase {
|
||||||
type: "result";
|
type: "result";
|
||||||
content: {
|
content: {
|
||||||
@ -15,7 +18,7 @@ export interface QuizQuestionResult extends QuizQuestionBase {
|
|||||||
text: string;
|
text: string;
|
||||||
price: [number] | [number, number];
|
price: [number] | [number, number];
|
||||||
useImage: boolean;
|
useImage: boolean;
|
||||||
rule: QuestionBranchingRule,
|
rule: ResultQuestionBranchingRule,
|
||||||
hint: QuestionHint;
|
hint: QuestionHint;
|
||||||
autofill: boolean;
|
autofill: boolean;
|
||||||
redirect: string
|
redirect: string
|
||||||
|
@ -46,6 +46,7 @@ export type QuestionVariant = {
|
|||||||
extendedText: string;
|
extendedText: string;
|
||||||
/** Оригинал изображения (до кропа) */
|
/** Оригинал изображения (до кропа) */
|
||||||
originalImageUrl: string;
|
originalImageUrl: string;
|
||||||
|
points?: number;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type QuestionType =
|
export type QuestionType =
|
||||||
|
@ -51,6 +51,7 @@ export interface QuizConfig {
|
|||||||
type: QuizType;
|
type: QuizType;
|
||||||
noStartPage: boolean;
|
noStartPage: boolean;
|
||||||
startpageType: QuizStartpageType;
|
startpageType: QuizStartpageType;
|
||||||
|
score?: boolean;
|
||||||
results: QuizResultsType;
|
results: QuizResultsType;
|
||||||
haveRoot: string;
|
haveRoot: string;
|
||||||
theme: QuizTheme;
|
theme: QuizTheme;
|
||||||
|
@ -12,6 +12,7 @@ type FooterProps = {
|
|||||||
export const Footer = ({ stepNumber, nextButton, prevButton }: FooterProps) => {
|
export const Footer = ({ stepNumber, nextButton, prevButton }: FooterProps) => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { questions } = useQuizData();
|
const { questions } = useQuizData();
|
||||||
|
console.log(questions)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Box
|
<Box
|
||||||
|
@ -39,7 +39,7 @@ export const Question = ({
|
|||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const { settings } = useQuizData();
|
const { settings } = useQuizData();
|
||||||
const isMobile = useRootContainerSize() < 650;
|
const isMobile = useRootContainerSize() < 650;
|
||||||
|
console.log(settings)
|
||||||
return (
|
return (
|
||||||
<Box sx={{
|
<Box sx={{
|
||||||
backgroundColor: theme.palette.background.default,
|
backgroundColor: theme.palette.background.default,
|
||||||
|
@ -67,7 +67,8 @@ export const Date = ({ currentQuestion }: DateProps) => {
|
|||||||
|
|
||||||
updateAnswer(
|
updateAnswer(
|
||||||
currentQuestion.id,
|
currentQuestion.id,
|
||||||
date
|
date,
|
||||||
|
0
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
enqueueSnackbar("ответ не был засчитан");
|
enqueueSnackbar("ответ не был засчитан");
|
||||||
|
@ -43,7 +43,8 @@ export const Emoji = ({ currentQuestion }: EmojiProps) => {
|
|||||||
onChange={({ target }) => {
|
onChange={({ target }) => {
|
||||||
updateAnswer(
|
updateAnswer(
|
||||||
currentQuestion.id,
|
currentQuestion.id,
|
||||||
currentQuestion.content.variants[Number(target.value)].answer
|
currentQuestion.content.variants[Number(target.value)].answer,
|
||||||
|
currentQuestion.content.variants[Number(target.value)].points || 0
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -114,7 +115,8 @@ export const Emoji = ({ currentQuestion }: EmojiProps) => {
|
|||||||
|
|
||||||
updateAnswer(
|
updateAnswer(
|
||||||
currentQuestion.id,
|
currentQuestion.id,
|
||||||
currentQuestion.content.variants[index].id
|
currentQuestion.content.variants[index].id,
|
||||||
|
currentQuestion.content.variants[index].points || 0
|
||||||
);
|
);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -159,7 +159,8 @@ export const File = ({ currentQuestion }: FileProps) => {
|
|||||||
|
|
||||||
updateAnswer(
|
updateAnswer(
|
||||||
currentQuestion.id,
|
currentQuestion.id,
|
||||||
`${file.name}|${URL.createObjectURL(file)}`
|
`${file.name}|${URL.createObjectURL(file)}`,
|
||||||
|
0
|
||||||
);
|
);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@ -219,7 +220,7 @@ export const File = ({ currentQuestion }: FileProps) => {
|
|||||||
<IconButton
|
<IconButton
|
||||||
sx={{ p: 0 }}
|
sx={{ p: 0 }}
|
||||||
onClick={() => {
|
onClick={() => {
|
||||||
updateAnswer(currentQuestion.id, "");
|
updateAnswer(currentQuestion.id, "", 0);
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<CloseBold />
|
<CloseBold />
|
||||||
|
@ -79,7 +79,8 @@ export const Images = ({ currentQuestion }: ImagesProps) => {
|
|||||||
|
|
||||||
updateAnswer(
|
updateAnswer(
|
||||||
currentQuestion.id,
|
currentQuestion.id,
|
||||||
currentQuestion.content.variants[index].id
|
currentQuestion.content.variants[index].id,
|
||||||
|
currentQuestion.content.variants[index].points || 0
|
||||||
);
|
);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -41,7 +41,7 @@ export const Number = ({ currentQuestion }: NumberProps) => {
|
|||||||
qid,
|
qid,
|
||||||
});
|
});
|
||||||
|
|
||||||
updateAnswer(currentQuestion.id, value);
|
updateAnswer(currentQuestion.id, value, 0);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
enqueueSnackbar("ответ не был засчитан");
|
enqueueSnackbar("ответ не был засчитан");
|
||||||
}
|
}
|
||||||
@ -138,7 +138,7 @@ export const Number = ({ currentQuestion }: NumberProps) => {
|
|||||||
? `${value[0]}—${value[1]}`
|
? `${value[0]}—${value[1]}`
|
||||||
: String(value);
|
: String(value);
|
||||||
|
|
||||||
updateAnswer(currentQuestion.id, range);
|
updateAnswer(currentQuestion.id, range, 0);
|
||||||
}}
|
}}
|
||||||
onChangeCommitted={async (_, value) => {
|
onChangeCommitted={async (_, value) => {
|
||||||
if (currentQuestion.content.chooseRange && Array.isArray(value)) {
|
if (currentQuestion.content.chooseRange && Array.isArray(value)) {
|
||||||
|
@ -101,7 +101,7 @@ export const Rating = ({ currentQuestion }: RatingProps) => {
|
|||||||
qid,
|
qid,
|
||||||
});
|
});
|
||||||
|
|
||||||
updateAnswer(currentQuestion.id, String(value));
|
updateAnswer(currentQuestion.id, String(value), 0);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
enqueueSnackbar("ответ не был засчитан");
|
enqueueSnackbar("ответ не был засчитан");
|
||||||
|
@ -63,7 +63,7 @@ export const Select = ({ currentQuestion }: SelectProps) => {
|
|||||||
qid,
|
qid,
|
||||||
});
|
});
|
||||||
|
|
||||||
updateAnswer(currentQuestion.id, String(value));
|
updateAnswer(currentQuestion.id, String(value), 0);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
enqueueSnackbar("ответ не был засчитан");
|
enqueueSnackbar("ответ не был засчитан");
|
||||||
|
@ -50,7 +50,7 @@ export const Text = ({ currentQuestion }: TextProps) => {
|
|||||||
//@ts-ignore
|
//@ts-ignore
|
||||||
value={answer || ""}
|
value={answer || ""}
|
||||||
onChange={async ({ target }) => {
|
onChange={async ({ target }) => {
|
||||||
updateAnswer(currentQuestion.id, target.value);
|
updateAnswer(currentQuestion.id, target.value, 0);
|
||||||
inputHC(target.value);
|
inputHC(target.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -185,6 +185,7 @@ const VariantItem = ({
|
|||||||
onClick={async (event) => {
|
onClick={async (event) => {
|
||||||
event.preventDefault();
|
event.preventDefault();
|
||||||
const variantId = currentQuestion.content.variants[index].id;
|
const variantId = currentQuestion.content.variants[index].id;
|
||||||
|
console.log(answer)
|
||||||
|
|
||||||
if (currentQuestion.content.multi) {
|
if (currentQuestion.content.multi) {
|
||||||
const currentAnswer = typeof answer !== "string" ? answer || [] : [];
|
const currentAnswer = typeof answer !== "string" ? answer || [] : [];
|
||||||
@ -202,9 +203,11 @@ const VariantItem = ({
|
|||||||
currentQuestion.id,
|
currentQuestion.id,
|
||||||
currentAnswer.includes(variantId)
|
currentAnswer.includes(variantId)
|
||||||
? currentAnswer?.filter((item) => item !== variantId)
|
? currentAnswer?.filter((item) => item !== variantId)
|
||||||
: [...currentAnswer, variantId]
|
: [...currentAnswer, variantId],
|
||||||
|
currentQuestion.content.variants[index].points || 0
|
||||||
);
|
);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
enqueueSnackbar("ответ не был засчитан");
|
enqueueSnackbar("ответ не был засчитан");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -218,9 +221,14 @@ const VariantItem = ({
|
|||||||
qid,
|
qid,
|
||||||
});
|
});
|
||||||
|
|
||||||
updateAnswer(currentQuestion.id, variantId);
|
updateAnswer(currentQuestion.id, variantId,
|
||||||
|
answer === variantId ? 0
|
||||||
|
:
|
||||||
|
currentQuestion.content.variants[index].points || 0
|
||||||
|
);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
enqueueSnackbar("ответ не был засчитан");
|
enqueueSnackbar("ответ не был засчитан");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -234,6 +242,7 @@ const VariantItem = ({
|
|||||||
});
|
});
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
console.log(e)
|
||||||
enqueueSnackbar("ответ не был засчитан");
|
enqueueSnackbar("ответ не был засчитан");
|
||||||
}
|
}
|
||||||
deleteAnswer(currentQuestion.id);
|
deleteAnswer(currentQuestion.id);
|
||||||
|
@ -99,7 +99,8 @@ export const Varimg = ({ currentQuestion }: VarimgProps) => {
|
|||||||
|
|
||||||
updateAnswer(
|
updateAnswer(
|
||||||
currentQuestion.id,
|
currentQuestion.id,
|
||||||
currentQuestion.content.variants[index].id
|
currentQuestion.content.variants[index].id,
|
||||||
|
currentQuestion.content.variants[index].points || 0
|
||||||
);
|
);
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -19,6 +19,8 @@ type OwnVariant = {
|
|||||||
interface QuizViewStore {
|
interface QuizViewStore {
|
||||||
answers: QuestionAnswer[];
|
answers: QuestionAnswer[];
|
||||||
ownVariants: OwnVariant[];
|
ownVariants: OwnVariant[];
|
||||||
|
pointsSum: number;
|
||||||
|
points: Record<string, number>;
|
||||||
currentQuizStep: QuizStep;
|
currentQuizStep: QuizStep;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -27,6 +29,8 @@ export const useQuizViewStore = create<QuizViewStore>()(
|
|||||||
(set, get) => ({
|
(set, get) => ({
|
||||||
answers: [],
|
answers: [],
|
||||||
ownVariants: [],
|
ownVariants: [],
|
||||||
|
points: {},
|
||||||
|
pointsSum: 0,
|
||||||
currentQuizStep: "startpage",
|
currentQuizStep: "startpage",
|
||||||
}),
|
}),
|
||||||
{
|
{
|
||||||
@ -39,15 +43,24 @@ export const useQuizViewStore = create<QuizViewStore>()(
|
|||||||
|
|
||||||
function setProducedState<A extends string | { type: string; }>(
|
function setProducedState<A extends string | { type: string; }>(
|
||||||
recipe: (state: QuizViewStore) => void,
|
recipe: (state: QuizViewStore) => void,
|
||||||
action: A,
|
action?: A,
|
||||||
) {
|
) {
|
||||||
useQuizViewStore.setState(state => produce(state, recipe), false, action);
|
useQuizViewStore.setState(state => produce(state, recipe), false, action);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const calcPoints = () => {
|
||||||
|
const storePoints = useQuizViewStore.getState().points;
|
||||||
|
let sum = Object.values(storePoints).reduce((accumulator, currentValue) => accumulator + currentValue)
|
||||||
|
console.log("сумма ", sum)
|
||||||
|
useQuizViewStore.setState({ pointsSum: sum })
|
||||||
|
}
|
||||||
|
|
||||||
export const updateAnswer = (
|
export const updateAnswer = (
|
||||||
questionId: string,
|
questionId: string,
|
||||||
answer: string | string[] | Moment
|
answer: string | string[] | Moment,
|
||||||
) => setProducedState(state => {
|
points: number
|
||||||
|
) => {
|
||||||
|
setProducedState(state => {
|
||||||
const index = state.answers.findIndex(answer => questionId === answer.questionId);
|
const index = state.answers.findIndex(answer => questionId === answer.questionId);
|
||||||
|
|
||||||
if (index < 0) {
|
if (index < 0) {
|
||||||
@ -55,11 +68,15 @@ export const updateAnswer = (
|
|||||||
} else {
|
} else {
|
||||||
state.answers[index] = { questionId, answer };
|
state.answers[index] = { questionId, answer };
|
||||||
}
|
}
|
||||||
}, {
|
}, {
|
||||||
type: "updateAnswer",
|
type: "updateAnswer",
|
||||||
questionId,
|
questionId,
|
||||||
answer
|
answer
|
||||||
});
|
})
|
||||||
|
const storePoints = useQuizViewStore.getState().points;
|
||||||
|
useQuizViewStore.setState({ points: { ...storePoints, ...{ [questionId]: points } } })
|
||||||
|
calcPoints()
|
||||||
|
};
|
||||||
|
|
||||||
export const deleteAnswer = (questionId: string) => useQuizViewStore.setState(state => ({
|
export const deleteAnswer = (questionId: string) => useQuizViewStore.setState(state => ({
|
||||||
answers: state.answers.filter(answer => questionId !== answer.questionId)
|
answers: state.answers.filter(answer => questionId !== answer.questionId)
|
||||||
|
@ -9,7 +9,8 @@ import moment from "moment";
|
|||||||
export function useQuestionFlowControl() {
|
export function useQuestionFlowControl() {
|
||||||
const { settings, questions } = useQuizData();
|
const { settings, questions } = useQuizData();
|
||||||
const [currentQuestion, setCurrentQuestion] = useState<AnyTypedQuizQuestion>(getFirstQuestion);
|
const [currentQuestion, setCurrentQuestion] = useState<AnyTypedQuizQuestion>(getFirstQuestion);
|
||||||
const answers = useQuizViewStore(state => state.answers);
|
const { answers, pointsSum } = useQuizViewStore(state => state);
|
||||||
|
|
||||||
|
|
||||||
const linearQuestionIndex = questions.every(({ content }) => content.rule.parentId !== "root") // null when branching enabled
|
const linearQuestionIndex = questions.every(({ content }) => content.rule.parentId !== "root") // null when branching enabled
|
||||||
? questions.indexOf(currentQuestion)
|
? questions.indexOf(currentQuestion)
|
||||||
@ -30,12 +31,22 @@ export function useQuestionFlowControl() {
|
|||||||
return questions[0];
|
return questions[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
const nextQuestionId = useMemo(() => {
|
|
||||||
|
const nextQuestionIdPointsLogic = () => {
|
||||||
|
return questions.find(question =>
|
||||||
|
question.type === "result" && question.content.rule.parentId === "line"
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextQuestionIdMainLogic = () => {
|
||||||
const questionAnswer = answers.find(({ questionId }) => questionId === currentQuestion.id);
|
const questionAnswer = answers.find(({ questionId }) => questionId === currentQuestion.id);
|
||||||
|
|
||||||
|
//Если ответ существует и не является объектом момента
|
||||||
if (questionAnswer && !moment.isMoment(questionAnswer.answer)) {
|
if (questionAnswer && !moment.isMoment(questionAnswer.answer)) {
|
||||||
|
//Ответы должны храниться в массиве
|
||||||
const userAnswers = Array.isArray(questionAnswer.answer) ? questionAnswer.answer : [questionAnswer.answer];
|
const userAnswers = Array.isArray(questionAnswer.answer) ? questionAnswer.answer : [questionAnswer.answer];
|
||||||
|
|
||||||
|
//Сравниваем список условий ветвления и выбираем подходящее
|
||||||
for (const branchingRule of currentQuestion.content.rule.main) {
|
for (const branchingRule of currentQuestion.content.rule.main) {
|
||||||
if (userAnswers.some(answer => branchingRule.rules[0].answers.includes(answer))) {
|
if (userAnswers.some(answer => branchingRule.rules[0].answers.includes(answer))) {
|
||||||
return branchingRule.next;
|
return branchingRule.next;
|
||||||
@ -58,7 +69,13 @@ export function useQuestionFlowControl() {
|
|||||||
return questions.find(q => {
|
return questions.find(q => {
|
||||||
return q.type === "result" && q.content.rule.parentId === currentQuestion.content.id;
|
return q.type === "result" && q.content.rule.parentId === currentQuestion.content.id;
|
||||||
})?.id;
|
})?.id;
|
||||||
}, [answers, currentQuestion, questions]);
|
};
|
||||||
|
const calculateNextQuestionId = useMemo(() => {
|
||||||
|
if (Boolean(settings.cfg.score)) {
|
||||||
|
return nextQuestionIdPointsLogic()
|
||||||
|
}
|
||||||
|
return nextQuestionIdMainLogic()
|
||||||
|
}, [answers, currentQuestion, questions])
|
||||||
|
|
||||||
const prevQuestion = linearQuestionIndex !== null
|
const prevQuestion = linearQuestionIndex !== null
|
||||||
? questions[linearQuestionIndex - 1]
|
? questions[linearQuestionIndex - 1]
|
||||||
@ -67,11 +84,51 @@ export function useQuestionFlowControl() {
|
|||||||
|| q.content.id === currentQuestion.content.rule.parentId
|
|| q.content.id === currentQuestion.content.rule.parentId
|
||||||
);
|
);
|
||||||
|
|
||||||
const nextQuestion = linearQuestionIndex !== null
|
const findResultPointsLogic = () => {
|
||||||
? questions[linearQuestionIndex + 1] ?? questions.find(question =>
|
const results = questions
|
||||||
|
.filter(e => e.type === "result" && e.content.rule.minScore !== undefined && e.content.rule.minScore <= pointsSum)
|
||||||
|
const numbers = results.map(e => e.content.rule.minScore)
|
||||||
|
const indexOfNext = Math.max(...numbers)
|
||||||
|
console.log(results)
|
||||||
|
console.log(numbers)
|
||||||
|
console.log(indexOfNext)
|
||||||
|
return results[numbers.indexOf(indexOfNext)]
|
||||||
|
}
|
||||||
|
|
||||||
|
const nextQuestion = () => {
|
||||||
|
let next
|
||||||
|
//Искать можно двумя логиками. Основной и балловой
|
||||||
|
if (Boolean(settings.cfg.score)) {
|
||||||
|
//Балловая
|
||||||
|
|
||||||
|
|
||||||
|
//Ищем линейно
|
||||||
|
if (linearQuestionIndex !== null) {
|
||||||
|
|
||||||
|
|
||||||
|
next = questions[linearQuestionIndex + 1]
|
||||||
|
if (next.type === "result") next = findResultPointsLogic()
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} else {
|
||||||
|
//Основная
|
||||||
|
if (linearQuestionIndex !== null) {
|
||||||
|
//Ищем линейно
|
||||||
|
next = questions[linearQuestionIndex + 1] ?? questions.find(question =>
|
||||||
question.type === "result" && question.content.rule.parentId === "line"
|
question.type === "result" && question.content.rule.parentId === "line"
|
||||||
)
|
)
|
||||||
: questions.find(q => q.id === nextQuestionId || q.content.id === nextQuestionId);
|
} else {
|
||||||
|
//Ищем ветвлением
|
||||||
|
next = questions.find(q => q.id === calculateNextQuestionId || q.content.id === calculateNextQuestionId);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (!next) throw new Error("Не найден следующий вопрос")
|
||||||
|
console.log("next", next)
|
||||||
|
return next
|
||||||
|
}
|
||||||
|
|
||||||
const showResult = useCallback(() => {
|
const showResult = useCallback(() => {
|
||||||
if (nextQuestion?.type !== "result") throw new Error("Current question is not result");
|
if (nextQuestion?.type !== "result") throw new Error("Current question is not result");
|
||||||
|
Loading…
Reference in New Issue
Block a user