fix: various bugs

This commit is contained in:
swve 2024-08-29 20:12:15 +02:00
parent 2c61542eb7
commit 8b1332c840
4 changed files with 49 additions and 73 deletions

View file

@ -377,7 +377,7 @@ def send_invite_email(
<body> <body>
<p>Hello {email}</p> <p>Hello {email}</p>
<p>You have been invited to {org.name} by @{user.username}. Your invite code is {invite['invite_code']}.</p> <p>You have been invited to {org.name} by @{user.username}. Your invite code is {invite['invite_code']}.</p>
<p>Click <a href="{org.slug}.learnhouse.io/signup?inviteCode={invite['invite_code']}">here</a> to sign up.</p> <p>Click <a href="{org.slug}.learnhouse.io/signup?orgslug={org.slug}&inviteCode={invite['invite_code']}">here</a> to sign up.</p>
<p>Thank you</p> <p>Thank you</p>
</body> </body>
</html> </html>

View file

@ -170,7 +170,7 @@ const LoginClient = (props: LoginClientProps) => {
</FormField> </FormField>
<div> <div>
<Link <Link
href={getUriWithOrg(props.org.slug, '/forgot')} href={{ pathname: getUriWithoutOrg('/forgot'), query: props.org.slug ? { orgslug: props.org.slug } : null }}
passHref passHref
className="text-xs text-gray-500 hover:underline" className="text-xs text-gray-500 hover:underline"
> >

View file

@ -32,23 +32,19 @@ function QuizBlockComponent(props: any) {
const isEditable = editorState.isEditable const isEditable = editorState.isEditable
const handleAnswerClick = (question_id: string, answer_id: string) => { 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 = () => { const refreshUserSubmission = () => {
@ -57,38 +53,31 @@ function QuizBlockComponent(props: any) {
} }
const handleUserSubmission = () => { const handleUserSubmission = () => {
if (userAnswers.length === 0) { setSubmitted(true);
setSubmissionMessage('Please answer at least one question!')
return
}
setSubmitted(true) const correctAnswers = questions.every((question: Question) => {
const correctAnswers = question.answers.filter((answer: Answer) => answer.correct);
// check if all submitted answers are correct const userAnswersForQuestion = userAnswers.filter(
const correctAnswers = questions.map((question: Question) => {
const correctAnswer: any = question.answers.find(
(answer: Answer) => answer.correct
)
const userAnswer = userAnswers.find(
(userAnswer: any) => userAnswer.question_id === question.question_id (userAnswer: any) => userAnswer.question_id === question.question_id
) );
if (correctAnswer.answer_id === userAnswer.answer_id) {
return true // If no correct answers are set and user didn't select any, it's correct
} else { if (correctAnswers.length === 0 && userAnswersForQuestion.length === 0) {
return false return true;
} }
})
// check if all answers are correct // Check if user selected all correct answers and no incorrect ones
const allCorrect = correctAnswers.every( return (
(answer: boolean) => answer === true correctAnswers.length === userAnswersForQuestion.length &&
) correctAnswers.every((correctAnswer: Answer) =>
userAnswersForQuestion.some(
(userAnswer: any) => userAnswer.answer_id === correctAnswer.answer_id
)
)
);
});
if (allCorrect) { setSubmissionMessage(correctAnswers ? 'All answers are correct!' : 'Some answers are incorrect!');
setSubmissionMessage('All answers are correct!')
} else {
setSubmissionMessage('Some answers are incorrect!')
}
} }
const getAnswerID = (answerIndex: number, questionId: string) => { const getAnswerID = (answerIndex: number, questionId: string) => {
@ -204,19 +193,14 @@ function QuizBlockComponent(props: any) {
const markAnswerCorrect = (question_id: string, answer_id: string) => { const markAnswerCorrect = (question_id: string, answer_id: string) => {
const newQuestions = questions.map((question: Question) => { const newQuestions = questions.map((question: Question) => {
if (question.question_id === question_id) { if (question.question_id === question_id) {
question.answers.map((answer: Answer) => { question.answers = question.answers.map((answer: Answer) => ({
if (answer.answer_id === answer_id) { ...answer,
answer.correct = true correct: answer.answer_id === answer_id ? !answer.correct : answer.correct,
} else { }));
answer.correct = false
}
return answer
})
} }
return question return question;
}) });
saveQuestions(newQuestions) saveQuestions(newQuestions);
} }
return ( return (
@ -308,29 +292,21 @@ function QuizBlockComponent(props: any) {
key={answer.answer_id} key={answer.answer_id}
className={twMerge( 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', '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 answer.correct && isEditable ? 'outline-lime-300' : 'outline-white',
? 'outline-lime-300' userAnswers.some(
: 'outline-white',
userAnswers.find(
(userAnswer: any) => (userAnswer: any) =>
userAnswer.question_id === question.question_id && userAnswer.question_id === question.question_id &&
userAnswer.answer_id === answer.answer_id && userAnswer.answer_id === answer.answer_id &&
!isEditable !isEditable
) ) ? 'outline-slate-300' : '',
? 'outline-slate-300' submitted && answer.correct ? 'outline-lime-300 text-lime' : '',
: '',
submitted && answer.correct
? 'outline-lime-300 text-lime'
: '',
submitted && submitted &&
!answer.correct && !answer.correct &&
userAnswers.find( userAnswers.some(
(userAnswer: any) => (userAnswer: any) =>
userAnswer.question_id === question.question_id && userAnswer.question_id === question.question_id &&
userAnswer.answer_id === answer.answer_id userAnswer.answer_id === answer.answer_id
) ) ? 'outline-red-400' : ''
? 'outline-red-400'
: ''
)} )}
onClick={() => onClick={() =>
handleAnswerClick(question.question_id, answer.answer_id) handleAnswerClick(question.question_id, answer.answer_id)
@ -347,7 +323,7 @@ function QuizBlockComponent(props: any) {
: '', : '',
submitted && submitted &&
!answer.correct && !answer.correct &&
userAnswers.find( userAnswers.some(
(userAnswer: any) => (userAnswer: any) =>
userAnswer.question_id === question.question_id && userAnswer.question_id === question.question_id &&
userAnswer.answer_id === answer.answer_id userAnswer.answer_id === answer.answer_id

View file

@ -37,7 +37,7 @@ export default async function middleware(req: NextRequest) {
// Out of orgslug paths & rewrite // Out of orgslug paths & rewrite
const standard_paths = ['/home'] const standard_paths = ['/home']
const auth_paths = ['/login', '/signup', '/reset'] const auth_paths = ['/login', '/signup', '/reset', '/forgot']
if (standard_paths.includes(pathname)) { if (standard_paths.includes(pathname)) {
// Redirect to the same pathname with the original search params // Redirect to the same pathname with the original search params
return NextResponse.rewrite(new URL(`${pathname}${search}`, req.url)) return NextResponse.rewrite(new URL(`${pathname}${search}`, req.url))