From 8b1332c840a58382d826186c0c8f0ce3e186db82 Mon Sep 17 00:00:00 2001 From: swve Date: Thu, 29 Aug 2024 20:12:15 +0200 Subject: [PATCH] fix: various bugs --- apps/api/src/services/orgs/invites.py | 2 +- apps/web/app/auth/login/login.tsx | 2 +- .../Extensions/Quiz/QuizBlockComponent.tsx | 116 +++++++----------- apps/web/middleware.ts | 2 +- 4 files changed, 49 insertions(+), 73 deletions(-) diff --git a/apps/api/src/services/orgs/invites.py b/apps/api/src/services/orgs/invites.py index c8cb5f69..e2e030e3 100644 --- a/apps/api/src/services/orgs/invites.py +++ b/apps/api/src/services/orgs/invites.py @@ -377,7 +377,7 @@ def send_invite_email(

Hello {email}

You have been invited to {org.name} by @{user.username}. Your invite code is {invite['invite_code']}.

-

Click here to sign up.

+

Click here to sign up.

Thank you

diff --git a/apps/web/app/auth/login/login.tsx b/apps/web/app/auth/login/login.tsx index 49dd7161..6420396e 100644 --- a/apps/web/app/auth/login/login.tsx +++ b/apps/web/app/auth/login/login.tsx @@ -170,7 +170,7 @@ const LoginClient = (props: LoginClientProps) => {
diff --git a/apps/web/components/Objects/Editor/Extensions/Quiz/QuizBlockComponent.tsx b/apps/web/components/Objects/Editor/Extensions/Quiz/QuizBlockComponent.tsx index a33aab0e..3e81aa8e 100644 --- a/apps/web/components/Objects/Editor/Extensions/Quiz/QuizBlockComponent.tsx +++ b/apps/web/components/Objects/Editor/Extensions/Quiz/QuizBlockComponent.tsx @@ -32,23 +32,19 @@ function QuizBlockComponent(props: any) { const isEditable = editorState.isEditable const handleAnswerClick = (question_id: string, answer_id: string) => { - // if the quiz is submitted, do nothing - if (submitted) { - return + if (submitted) return; + + const existingAnswerIndex = userAnswers.findIndex( + (answer: any) => answer.question_id === question_id && answer.answer_id === answer_id + ); + + if (existingAnswerIndex !== -1) { + // Remove the answer if it's already selected + setUserAnswers(userAnswers.filter((_, index) => index !== existingAnswerIndex)); + } else { + // Add the answer + setUserAnswers([...userAnswers, { question_id, answer_id }]); } - - const userAnswer = { - question_id: question_id, - answer_id: answer_id, - } - const newAnswers = [...userAnswers, userAnswer] - - // only accept one answer per question - const filteredAnswers = newAnswers.filter( - (answer: any) => answer.question_id !== question_id - ) - - setUserAnswers([...filteredAnswers, userAnswer]) } const refreshUserSubmission = () => { @@ -57,38 +53,31 @@ function QuizBlockComponent(props: any) { } const handleUserSubmission = () => { - if (userAnswers.length === 0) { - setSubmissionMessage('Please answer at least one question!') - return - } + setSubmitted(true); - setSubmitted(true) - - // check if all submitted answers are correct - const correctAnswers = questions.map((question: Question) => { - const correctAnswer: any = question.answers.find( - (answer: Answer) => answer.correct - ) - const userAnswer = userAnswers.find( + const correctAnswers = questions.every((question: Question) => { + const correctAnswers = question.answers.filter((answer: Answer) => answer.correct); + const userAnswersForQuestion = userAnswers.filter( (userAnswer: any) => userAnswer.question_id === question.question_id - ) - if (correctAnswer.answer_id === userAnswer.answer_id) { - return true - } else { - return false + ); + + // If no correct answers are set and user didn't select any, it's correct + if (correctAnswers.length === 0 && userAnswersForQuestion.length === 0) { + return true; } - }) - // check if all answers are correct - const allCorrect = correctAnswers.every( - (answer: boolean) => answer === true - ) + // Check if user selected all correct answers and no incorrect ones + return ( + correctAnswers.length === userAnswersForQuestion.length && + correctAnswers.every((correctAnswer: Answer) => + userAnswersForQuestion.some( + (userAnswer: any) => userAnswer.answer_id === correctAnswer.answer_id + ) + ) + ); + }); - if (allCorrect) { - setSubmissionMessage('All answers are correct!') - } else { - setSubmissionMessage('Some answers are incorrect!') - } + setSubmissionMessage(correctAnswers ? 'All answers are correct!' : 'Some answers are incorrect!'); } const getAnswerID = (answerIndex: number, questionId: string) => { @@ -204,19 +193,14 @@ function QuizBlockComponent(props: any) { const markAnswerCorrect = (question_id: string, answer_id: string) => { const newQuestions = questions.map((question: Question) => { if (question.question_id === question_id) { - question.answers.map((answer: Answer) => { - if (answer.answer_id === answer_id) { - answer.correct = true - } else { - answer.correct = false - } - - return answer - }) + question.answers = question.answers.map((answer: Answer) => ({ + ...answer, + correct: answer.answer_id === answer_id ? !answer.correct : answer.correct, + })); } - return question - }) - saveQuestions(newQuestions) + return question; + }); + saveQuestions(newQuestions); } return ( @@ -308,29 +292,21 @@ function QuizBlockComponent(props: any) { key={answer.answer_id} className={twMerge( 'outline outline-3 pr-2 shadow w-full flex items-center space-x-2 h-[30px] bg-opacity-50 hover:bg-opacity-100 hover:shadow-md rounded-s rounded-lg bg-white text-sm hover:scale-105 active:scale-110 duration-150 cursor-pointer ease-linear', - answer.correct && isEditable - ? 'outline-lime-300' - : 'outline-white', - userAnswers.find( + answer.correct && isEditable ? 'outline-lime-300' : 'outline-white', + userAnswers.some( (userAnswer: any) => userAnswer.question_id === question.question_id && userAnswer.answer_id === answer.answer_id && !isEditable - ) - ? 'outline-slate-300' - : '', - submitted && answer.correct - ? 'outline-lime-300 text-lime' - : '', + ) ? 'outline-slate-300' : '', + submitted && answer.correct ? 'outline-lime-300 text-lime' : '', submitted && !answer.correct && - userAnswers.find( + userAnswers.some( (userAnswer: any) => userAnswer.question_id === question.question_id && userAnswer.answer_id === answer.answer_id - ) - ? 'outline-red-400' - : '' + ) ? 'outline-red-400' : '' )} onClick={() => handleAnswerClick(question.question_id, answer.answer_id) @@ -347,7 +323,7 @@ function QuizBlockComponent(props: any) { : '', submitted && !answer.correct && - userAnswers.find( + userAnswers.some( (userAnswer: any) => userAnswer.question_id === question.question_id && userAnswer.answer_id === answer.answer_id diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index 6cae7a10..7ce10397 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -37,7 +37,7 @@ export default async function middleware(req: NextRequest) { // Out of orgslug paths & rewrite const standard_paths = ['/home'] - const auth_paths = ['/login', '/signup', '/reset'] + const auth_paths = ['/login', '/signup', '/reset', '/forgot'] if (standard_paths.includes(pathname)) { // Redirect to the same pathname with the original search params return NextResponse.rewrite(new URL(`${pathname}${search}`, req.url))