линейный сбор баллов, анализ баллового результа
This commit is contained in:
parent
3e8bf766aa
commit
1da332567e
@ -5,7 +5,7 @@ import QuizAnswerer from "./QuizAnswerer";
|
||||
import { QuizIdContext } from "./contexts/QuizIdContext";
|
||||
import { RootContainerWidthContext } from "./contexts/RootContainerWidthContext";
|
||||
|
||||
const defaultQuizId = "45ef7f9c-784d-4e58-badb-f6b337f08ba0"; // branching
|
||||
const defaultQuizId = "9ed8d0e9-d355-4fc1-8b89-4f962e3efc52"; // branching
|
||||
// const defaultQuizId = "a9d31460-132a-4479-a3f0-90241498b6f9"; // linear
|
||||
|
||||
export default function App() {
|
||||
|
@ -4,6 +4,9 @@ import type {
|
||||
QuestionHint,
|
||||
} from "./shared";
|
||||
|
||||
interface ResultQuestionBranchingRule extends QuestionBranchingRule {
|
||||
minScore?: number
|
||||
}
|
||||
export interface QuizQuestionResult extends QuizQuestionBase {
|
||||
type: "result";
|
||||
content: {
|
||||
@ -15,7 +18,7 @@ export interface QuizQuestionResult extends QuizQuestionBase {
|
||||
text: string;
|
||||
price: [number] | [number, number];
|
||||
useImage: boolean;
|
||||
rule: QuestionBranchingRule,
|
||||
rule: ResultQuestionBranchingRule,
|
||||
hint: QuestionHint;
|
||||
autofill: boolean;
|
||||
redirect: string
|
||||
|
@ -46,6 +46,7 @@ export type QuestionVariant = {
|
||||
extendedText: string;
|
||||
/** Оригинал изображения (до кропа) */
|
||||
originalImageUrl: string;
|
||||
points?: number;
|
||||
};
|
||||
|
||||
export type QuestionType =
|
||||
|
@ -51,6 +51,7 @@ export interface QuizConfig {
|
||||
type: QuizType;
|
||||
noStartPage: boolean;
|
||||
startpageType: QuizStartpageType;
|
||||
score?: boolean;
|
||||
results: QuizResultsType;
|
||||
haveRoot: string;
|
||||
theme: QuizTheme;
|
||||
|
@ -12,6 +12,7 @@ type FooterProps = {
|
||||
export const Footer = ({ stepNumber, nextButton, prevButton }: FooterProps) => {
|
||||
const theme = useTheme();
|
||||
const { questions } = useQuizData();
|
||||
console.log(questions)
|
||||
|
||||
return (
|
||||
<Box
|
||||
|
@ -39,7 +39,7 @@ export const Question = ({
|
||||
const theme = useTheme();
|
||||
const { settings } = useQuizData();
|
||||
const isMobile = useRootContainerSize() < 650;
|
||||
|
||||
console.log(settings)
|
||||
return (
|
||||
<Box sx={{
|
||||
backgroundColor: theme.palette.background.default,
|
||||
|
@ -67,7 +67,8 @@ export const Date = ({ currentQuestion }: DateProps) => {
|
||||
|
||||
updateAnswer(
|
||||
currentQuestion.id,
|
||||
date
|
||||
date,
|
||||
0
|
||||
);
|
||||
} catch (e) {
|
||||
enqueueSnackbar("ответ не был засчитан");
|
||||
|
@ -43,7 +43,8 @@ export const Emoji = ({ currentQuestion }: EmojiProps) => {
|
||||
onChange={({ target }) => {
|
||||
updateAnswer(
|
||||
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(
|
||||
currentQuestion.id,
|
||||
currentQuestion.content.variants[index].id
|
||||
currentQuestion.content.variants[index].id,
|
||||
currentQuestion.content.variants[index].points || 0
|
||||
);
|
||||
|
||||
} catch (e) {
|
||||
|
@ -159,7 +159,8 @@ export const File = ({ currentQuestion }: FileProps) => {
|
||||
|
||||
updateAnswer(
|
||||
currentQuestion.id,
|
||||
`${file.name}|${URL.createObjectURL(file)}`
|
||||
`${file.name}|${URL.createObjectURL(file)}`,
|
||||
0
|
||||
);
|
||||
|
||||
} catch (e) {
|
||||
@ -219,7 +220,7 @@ export const File = ({ currentQuestion }: FileProps) => {
|
||||
<IconButton
|
||||
sx={{ p: 0 }}
|
||||
onClick={() => {
|
||||
updateAnswer(currentQuestion.id, "");
|
||||
updateAnswer(currentQuestion.id, "", 0);
|
||||
}}
|
||||
>
|
||||
<CloseBold />
|
||||
|
@ -79,7 +79,8 @@ export const Images = ({ currentQuestion }: ImagesProps) => {
|
||||
|
||||
updateAnswer(
|
||||
currentQuestion.id,
|
||||
currentQuestion.content.variants[index].id
|
||||
currentQuestion.content.variants[index].id,
|
||||
currentQuestion.content.variants[index].points || 0
|
||||
);
|
||||
|
||||
} catch (e) {
|
||||
|
@ -41,7 +41,7 @@ export const Number = ({ currentQuestion }: NumberProps) => {
|
||||
qid,
|
||||
});
|
||||
|
||||
updateAnswer(currentQuestion.id, value);
|
||||
updateAnswer(currentQuestion.id, value, 0);
|
||||
} catch (e) {
|
||||
enqueueSnackbar("ответ не был засчитан");
|
||||
}
|
||||
@ -138,7 +138,7 @@ export const Number = ({ currentQuestion }: NumberProps) => {
|
||||
? `${value[0]}—${value[1]}`
|
||||
: String(value);
|
||||
|
||||
updateAnswer(currentQuestion.id, range);
|
||||
updateAnswer(currentQuestion.id, range, 0);
|
||||
}}
|
||||
onChangeCommitted={async (_, value) => {
|
||||
if (currentQuestion.content.chooseRange && Array.isArray(value)) {
|
||||
|
@ -101,7 +101,7 @@ export const Rating = ({ currentQuestion }: RatingProps) => {
|
||||
qid,
|
||||
});
|
||||
|
||||
updateAnswer(currentQuestion.id, String(value));
|
||||
updateAnswer(currentQuestion.id, String(value), 0);
|
||||
|
||||
} catch (e) {
|
||||
enqueueSnackbar("ответ не был засчитан");
|
||||
|
@ -63,7 +63,7 @@ export const Select = ({ currentQuestion }: SelectProps) => {
|
||||
qid,
|
||||
});
|
||||
|
||||
updateAnswer(currentQuestion.id, String(value));
|
||||
updateAnswer(currentQuestion.id, String(value), 0);
|
||||
|
||||
} catch (e) {
|
||||
enqueueSnackbar("ответ не был засчитан");
|
||||
|
@ -50,7 +50,7 @@ export const Text = ({ currentQuestion }: TextProps) => {
|
||||
//@ts-ignore
|
||||
value={answer || ""}
|
||||
onChange={async ({ target }) => {
|
||||
updateAnswer(currentQuestion.id, target.value);
|
||||
updateAnswer(currentQuestion.id, target.value, 0);
|
||||
inputHC(target.value);
|
||||
}
|
||||
}
|
||||
|
@ -183,6 +183,7 @@ const VariantItem = ({
|
||||
onClick={async (event) => {
|
||||
event.preventDefault();
|
||||
const variantId = currentQuestion.content.variants[index].id;
|
||||
console.log(answer)
|
||||
|
||||
if (currentQuestion.content.multi) {
|
||||
const currentAnswer = typeof answer !== "string" ? answer || [] : [];
|
||||
@ -200,9 +201,11 @@ const VariantItem = ({
|
||||
currentQuestion.id,
|
||||
currentAnswer.includes(variantId)
|
||||
? currentAnswer?.filter((item) => item !== variantId)
|
||||
: [...currentAnswer, variantId]
|
||||
: [...currentAnswer, variantId],
|
||||
currentQuestion.content.variants[index].points || 0
|
||||
);
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
enqueueSnackbar("ответ не был засчитан");
|
||||
}
|
||||
|
||||
@ -216,9 +219,14 @@ const VariantItem = ({
|
||||
qid,
|
||||
});
|
||||
|
||||
updateAnswer(currentQuestion.id, variantId);
|
||||
updateAnswer(currentQuestion.id, variantId,
|
||||
answer === variantId ? 0
|
||||
:
|
||||
currentQuestion.content.variants[index].points || 0
|
||||
);
|
||||
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
enqueueSnackbar("ответ не был засчитан");
|
||||
}
|
||||
|
||||
@ -232,6 +240,7 @@ const VariantItem = ({
|
||||
});
|
||||
|
||||
} catch (e) {
|
||||
console.log(e)
|
||||
enqueueSnackbar("ответ не был засчитан");
|
||||
}
|
||||
deleteAnswer(currentQuestion.id);
|
||||
|
@ -96,7 +96,8 @@ export const Varimg = ({ currentQuestion }: VarimgProps) => {
|
||||
|
||||
updateAnswer(
|
||||
currentQuestion.id,
|
||||
currentQuestion.content.variants[index].id
|
||||
currentQuestion.content.variants[index].id,
|
||||
currentQuestion.content.variants[index].points || 0
|
||||
);
|
||||
|
||||
} catch (e) {
|
||||
|
@ -19,6 +19,8 @@ type OwnVariant = {
|
||||
interface QuizViewStore {
|
||||
answers: QuestionAnswer[];
|
||||
ownVariants: OwnVariant[];
|
||||
pointsSum: number;
|
||||
points: Record<string, number>;
|
||||
currentQuizStep: QuizStep;
|
||||
}
|
||||
|
||||
@ -27,6 +29,8 @@ export const useQuizViewStore = create<QuizViewStore>()(
|
||||
(set, get) => ({
|
||||
answers: [],
|
||||
ownVariants: [],
|
||||
points: {},
|
||||
pointsSum: 0,
|
||||
currentQuizStep: "startpage",
|
||||
}),
|
||||
{
|
||||
@ -39,27 +43,40 @@ export const useQuizViewStore = create<QuizViewStore>()(
|
||||
|
||||
function setProducedState<A extends string | { type: string; }>(
|
||||
recipe: (state: QuizViewStore) => void,
|
||||
action: A,
|
||||
action?: A,
|
||||
) {
|
||||
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 = (
|
||||
questionId: string,
|
||||
answer: string | string[] | Moment
|
||||
) => setProducedState(state => {
|
||||
const index = state.answers.findIndex(answer => questionId === answer.questionId);
|
||||
answer: string | string[] | Moment,
|
||||
points: number
|
||||
) => {
|
||||
setProducedState(state => {
|
||||
const index = state.answers.findIndex(answer => questionId === answer.questionId);
|
||||
|
||||
if (index < 0) {
|
||||
state.answers.push({ questionId, answer });
|
||||
} else {
|
||||
state.answers[index] = { questionId, answer };
|
||||
}
|
||||
}, {
|
||||
type: "updateAnswer",
|
||||
questionId,
|
||||
answer
|
||||
});
|
||||
if (index < 0) {
|
||||
state.answers.push({ questionId, answer });
|
||||
} else {
|
||||
state.answers[index] = { questionId, answer };
|
||||
}
|
||||
}, {
|
||||
type: "updateAnswer",
|
||||
questionId,
|
||||
answer
|
||||
})
|
||||
const storePoints = useQuizViewStore.getState().points;
|
||||
useQuizViewStore.setState({ points: { ...storePoints, ...{ [questionId]: points } } })
|
||||
calcPoints()
|
||||
};
|
||||
|
||||
export const deleteAnswer = (questionId: string) => useQuizViewStore.setState(state => ({
|
||||
answers: state.answers.filter(answer => questionId !== answer.questionId)
|
||||
|
@ -9,7 +9,8 @@ import moment from "moment";
|
||||
export function useQuestionFlowControl() {
|
||||
const { settings, questions } = useQuizData();
|
||||
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
|
||||
? questions.indexOf(currentQuestion)
|
||||
@ -30,12 +31,22 @@ export function useQuestionFlowControl() {
|
||||
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);
|
||||
|
||||
//Если ответ существует и не является объектом момента
|
||||
if (questionAnswer && !moment.isMoment(questionAnswer.answer)) {
|
||||
//Ответы должны храниться в массиве
|
||||
const userAnswers = Array.isArray(questionAnswer.answer) ? questionAnswer.answer : [questionAnswer.answer];
|
||||
|
||||
//Сравниваем список условий ветвления и выбираем подходящее
|
||||
for (const branchingRule of currentQuestion.content.rule.main) {
|
||||
if (userAnswers.some(answer => branchingRule.rules[0].answers.includes(answer))) {
|
||||
return branchingRule.next;
|
||||
@ -58,7 +69,13 @@ export function useQuestionFlowControl() {
|
||||
return questions.find(q => {
|
||||
return q.type === "result" && q.content.rule.parentId === currentQuestion.content.id;
|
||||
})?.id;
|
||||
}, [answers, currentQuestion, questions]);
|
||||
};
|
||||
const calculateNextQuestionId = useMemo(() => {
|
||||
if (Boolean(settings.cfg.score)) {
|
||||
return nextQuestionIdPointsLogic()
|
||||
}
|
||||
return nextQuestionIdMainLogic()
|
||||
}, [answers, currentQuestion, questions])
|
||||
|
||||
const prevQuestion = linearQuestionIndex !== null
|
||||
? questions[linearQuestionIndex - 1]
|
||||
@ -67,11 +84,51 @@ export function useQuestionFlowControl() {
|
||||
|| q.content.id === currentQuestion.content.rule.parentId
|
||||
);
|
||||
|
||||
const nextQuestion = linearQuestionIndex !== null
|
||||
? questions[linearQuestionIndex + 1] ?? questions.find(question =>
|
||||
question.type === "result" && question.content.rule.parentId === "line"
|
||||
)
|
||||
: questions.find(q => q.id === nextQuestionId || q.content.id === nextQuestionId);
|
||||
const findResultPointsLogic = () => {
|
||||
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"
|
||||
)
|
||||
} 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(() => {
|
||||
if (nextQuestion?.type !== "result") throw new Error("Current question is not result");
|
||||
|
Loading…
Reference in New Issue
Block a user