From 6aa972cff98dc1fea53ee0710f77ca460479eb94 Mon Sep 17 00:00:00 2001 From: Max Nanis Date: Tue, 3 Jun 2025 21:56:16 +0700 Subject: Cleaning up the Questions page, re-use Card components, showing live Task counts. --- src/pages/Questions.tsx | 140 ++++++++++++++++++++++++++---------------------- 1 file changed, 77 insertions(+), 63 deletions(-) diff --git a/src/pages/Questions.tsx b/src/pages/Questions.tsx index 9eb7cba..a6f0d7b 100644 --- a/src/pages/Questions.tsx +++ b/src/pages/Questions.tsx @@ -1,9 +1,8 @@ import React, {useMemo, useState} from 'react' import {UpkQuestion, UpkQuestionChoice} from "@/api"; -import {Card, CardContent, CardHeader} from "@/components/ui/card.tsx"; +import {Card, CardContent, CardTitle, CardFooter, CardHeader} from "@/components/ui/card.tsx"; import {useAppDispatch, useAppSelector} from "@/hooks.ts"; import {addAnswer, makeSelectChoicesByQuestion} from "@/models/answerSlice.ts"; -import {selectQuestionById} from "@/models/questionSlice.ts"; import {useSelector} from "react-redux"; import {Button} from "@/components/ui/button" import { @@ -13,6 +12,8 @@ import { PaginationLink, PaginationNext, } from "@/components/ui/pagination" +import { Input } from "@/components/ui/input" +import {Badge} from "@/components/ui/badge" const TextEntry: React.FC<{ question: UpkQuestion }> = ({question}) => { const dispatch = useAppDispatch() @@ -23,24 +24,16 @@ const TextEntry: React.FC<{ question: UpkQuestion }> = ({question}) => { dispatch(addAnswer({question: question, val: event.target.value})) }; - console.log("TextEntry.answer", answer) + // console.log("TextEntry.answer", answer) return ( - - - {question.question_text} - - - - - {answer.error_msg} - - + + // {answer.error_msg} ) } @@ -48,67 +41,83 @@ const MultiChoiceItem: React.FC<{ question: UpkQuestion, choice: UpkQuestionChoi const dispatch = useAppDispatch() const selectAnswer = useMemo(() => makeSelectChoicesByQuestion(question), [question]); const answer = useSelector(selectAnswer); - const selected = answer.values.includes(choice.choice_id) + const selected: Boolean = answer.values.includes(choice.choice_id) return ( - +
  • + +
  • ) } const MultipleChoice: React.FC<{ question: UpkQuestion }> = ({question}) => { - const selectAnswer = useMemo(() => makeSelectChoicesByQuestion(question), [question]); - const answer = useSelector(selectAnswer); + // const selectAnswer = useMemo(() => makeSelectChoicesByQuestion(question), [question]); + // const answer = useSelector(selectAnswer); return ( - - - {question.question_text} - {answer.error_msg} - - - -
      - { - question.choices.map(c => { - return - }) - } -
    -
    -
    + // {answer.error_msg} +
      + { + question.choices.map(c => { + return + }) + } +
    ) } const ProfileQuestionFull: React.FC = ({question}) => { - switch (question.question_type) { - case "TE": - return - case "MC": - return - default: - throw new Error("Incorrect Question Type provided"); - } + const renderContent = () => { + switch (question.question_type) { + case 'TE': + return + case 'MC': + return + } + }; + + return ( + + + {question.task_count.toLocaleString()} + + + + {question.question_text} + + + {renderContent()} + + + + + + ) } const QuestionsPage = () => { const questions = useAppSelector(state => state.questions) const [activeQuestionID, setQuestionID] = useState(() => questions[0].question_id); - const selectQuestion = useMemo(() => selectQuestionById(activeQuestionID), [questions]); - const question = useSelector(selectQuestion); - - console.log(activeQuestionID, questions) + const question = questions.find(q => q.question_id === activeQuestionID); + console.log("activeQuestionID:", activeQuestionID, question) return (
    @@ -116,16 +125,19 @@ const QuestionsPage = () => { A total of {questions.length} questions are available.

    - + { - questions.map((q, i) => { + questions.slice(0, 5).map((q, i) => { return ( setQuestionID(q.question_id)} + isActive={q.question_id === activeQuestionID} > - {i+1} + {i + 1} ) @@ -133,7 +145,9 @@ const QuestionsPage = () => { } - + @@ -141,7 +155,7 @@ const QuestionsPage = () => {
    - ); + ) } export { -- cgit v1.2.3