From 4bf1b3f5bc9564070dba1481e380179431ddf62b Mon Sep 17 00:00:00 2001 From: Max Nanis Date: Wed, 4 Jun 2025 05:35:57 +0700 Subject: Saving answers to server. Updating views based on reducer states (pagination disable after question is answered). --- src/pages/Questions.tsx | 162 +++++++++++++++++++++++++++++++++++------------- 1 file changed, 118 insertions(+), 44 deletions(-) (limited to 'src/pages/Questions.tsx') diff --git a/src/pages/Questions.tsx b/src/pages/Questions.tsx index a6f0d7b..e9abe31 100644 --- a/src/pages/Questions.tsx +++ b/src/pages/Questions.tsx @@ -1,8 +1,14 @@ import React, {useMemo, useState} from 'react' -import {UpkQuestion, UpkQuestionChoice} from "@/api"; -import {Card, CardContent, CardTitle, CardFooter, CardHeader} from "@/components/ui/card.tsx"; +import { + BodySubmitProfilingQuestionsProductIdProfilingQuestionsPost, + ProfilingQuestionsApiFactory, + UpkQuestion, + UpkQuestionChoice, + UserQuestionAnswerIn +} from "@/api"; +import {Card, CardContent, CardFooter, CardHeader, CardTitle} from "@/components/ui/card.tsx"; import {useAppDispatch, useAppSelector} from "@/hooks.ts"; -import {addAnswer, makeSelectChoicesByQuestion} from "@/models/answerSlice.ts"; +import answerSlice, {addAnswer, Answer,answerForQuestion, makeSelectChoicesByQuestion, saveAnswer} from "@/models/answerSlice.ts"; import {useSelector} from "react-redux"; import {Button} from "@/components/ui/button" import { @@ -12,41 +18,49 @@ import { PaginationLink, PaginationNext, } from "@/components/ui/pagination" -import { Input } from "@/components/ui/input" +import {Input} from "@/components/ui/input" import {Badge} from "@/components/ui/badge" const TextEntry: React.FC<{ question: UpkQuestion }> = ({question}) => { const dispatch = useAppDispatch() const selectAnswer = useMemo(() => makeSelectChoicesByQuestion(question), [question]); - const answer = useSelector(selectAnswer); + const answer: Answer = useSelector(selectAnswer); + const error: Boolean = answer.error_msg.length > 0 const handleInputChange = (event: React.ChangeEvent) => { dispatch(addAnswer({question: question, val: event.target.value})) }; - // console.log("TextEntry.answer", answer) - return ( - - // {answer.error_msg} + <> + + + { + error &&

{answer.error_msg}

+ } + + ) } const MultiChoiceItem: React.FC<{ question: UpkQuestion, choice: UpkQuestionChoice }> = ({question, choice}) => { const dispatch = useAppDispatch() const selectAnswer = useMemo(() => makeSelectChoicesByQuestion(question), [question]); - const answer = useSelector(selectAnswer); + const answer: Answer = useSelector(selectAnswer); const selected: Boolean = answer.values.includes(choice.choice_id) return (
  • ) } +// type Props = { +// onSetQuestionID: (name: string) => void +// } + +const PaginationIcon: React.FC<{ + question: UpkQuestion, activeQuestionID: string, idx: number, onSetQuestionID: () => void +}> = ({question, activeQuestionID, idx, onSetQuestionID}) => { + + const answers = useAppSelector(state => state.answers) + const answer = answers[question.question_id] + + return ( + + answer?.complete ? e.preventDefault() : onSetQuestionID(question.question_id)} + className={answer?.complete ? "pointer-events-none opacity-50 cursor-not-allowed" : ""}> + {idx + 1} + + + ) +} const QuestionsPage = () => { const questions = useAppSelector(state => state.questions) - const [activeQuestionID, setQuestionID] = useState(() => questions[0].question_id); + const [activeQuestionID, setQuestionID] = useState(() => questions[0].question_id); const question = questions.find(q => q.question_id === activeQuestionID); - console.log("activeQuestionID:", activeQuestionID, question) return (
    @@ -129,18 +214,8 @@ const QuestionsPage = () => { { questions.slice(0, 5).map((q, i) => { - return ( - - setQuestionID(q.question_id)} - isActive={q.question_id === activeQuestionID} - > - {i + 1} - - - ) + return }) } @@ -152,7 +227,6 @@ const QuestionsPage = () => { -
    ) -- cgit v1.2.3