Skip to content

Commit

Permalink
feat: apply feedback, refactor duplicates code
Browse files Browse the repository at this point in the history
  • Loading branch information
wielopolski committed Nov 21, 2024
1 parent 157b181 commit d0aa4a7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
11 changes: 5 additions & 6 deletions apps/api/src/questions/questions.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,16 +108,15 @@ export class QuestionsService {
trx,
);

const answerList = answerQuestion.answer.map((a) => {
if (typeof a !== "string") {
return a.value;
const answerList = answerQuestion.answer.map((answerElement) => {
if (typeof answerElement !== "string") {
return answerElement.value;
}

return a;
return answerElement;
});

if (!answerList || answerList.length === 0)
throw new NotFoundException("User answers not found");
if (!(answerList?.length === 0)) throw new NotFoundException("User answers not found");

const answers: { answer: string }[] = await this.questionsRepository.getQuestionAnswers(
answerQuestion.questionId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import Viewer from "~/components/RichText/Viever";
import { Card } from "~/components/ui/card";
import { FillInTheTextBlanks } from "~/modules/Courses/Lesson/LessonItems/FillInTheBlanks/FillInTheTextBlanks";
import { TextBlank } from "~/modules/Courses/Lesson/LessonItems/FillInTheBlanks/TextBlank";
import { handleCompletionForMediaLesson } from "~/utils/handleCompletionForMediaLesson";

type Answer = {
id: string;
Expand Down Expand Up @@ -75,7 +76,10 @@ export const FillInTheBlanks = ({

if (sortedWords.length > 0 && sortedWords.length <= maxAnswersAmount) {
sendAnswer(sortedWords);
if (!isCompleted && !isQuiz && sortedWords.length === maxAnswersAmount) {
if (
handleCompletionForMediaLesson(isCompleted, isQuiz) &&
sortedWords.length === maxAnswersAmount
) {
updateLessonItemCompletion(lessonItemId);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { arrayMove, sortableKeyboardCoordinates } from "@dnd-kit/sortable";
import { type FC, useEffect, useState } from "react";

import Viewer from "~/components/RichText/Viever";
import { handleCompletionForMediaLesson } from "~/utils/handleCompletionForMediaLesson";

import { DndBlank } from "./DndBlank";
import { DraggableWord } from "./DraggableWord";
Expand Down Expand Up @@ -115,6 +116,9 @@ export const FillInTheBlanksDnd: FC<FillInTheBlanksDndProps> = ({
});
};

const handleCompletion = () =>
handleCompletionForMediaLesson(isCompleted, isQuiz) && updateLessonItemCompletion(lessonItemId);

function handleDragEnd(event: DragEndEvent) {
const { active, over } = event;

Expand Down Expand Up @@ -190,9 +194,7 @@ export const FillInTheBlanksDnd: FC<FillInTheBlanksDndProps> = ({
const sortedWords = filteredWords.sort((a, b) => a.index - b.index);
if (sortedWords.length > 0 && sortedWords.length <= maxAnswersAmount) {
sendAnswer(sortedWords);
if (!isCompleted && !isQuiz && sortedWords.length === maxAnswersAmount) {
updateLessonItemCompletion(lessonItemId);
}
handleCompletion();
}
}

Expand All @@ -213,9 +215,7 @@ export const FillInTheBlanksDnd: FC<FillInTheBlanksDndProps> = ({
const sortedWords = filteredWords.sort((a, b) => a.index - b.index);
if (sortedWords.length > 0 && sortedWords.length <= maxAnswersAmount) {
sendAnswer(sortedWords);
if (!isCompleted && !isQuiz && sortedWords.length === maxAnswersAmount) {
updateLessonItemCompletion(lessonItemId);
}
handleCompletion();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useFormContext } from "react-hook-form";
import { Textarea } from "~/components/ui/textarea";
import { useUserRole } from "~/hooks/useUserRole";
import { cn } from "~/lib/utils";
import { handleCompletionForMediaLesson } from "~/utils/handleCompletionForMediaLesson";

import { FillInTheBlanksDnd } from "../FillInTheBlanks/dnd/FillInTheBlanksDnd";
import { FillInTheBlanks } from "../FillInTheBlanks/FillInTheBlanks";
Expand Down Expand Up @@ -71,11 +72,14 @@ export const Question = ({
}
}, [isQuiz, isSubmitted]);

const handleCompletion = () =>
handleCompletionForMediaLesson(isCompleted, isQuiz) && updateLessonItemCompletion(lessonItemId);

const handleClick = async (id: string) => {
if (isSingleQuestion) {
setSelectedOption([id]);
await sendAnswer([id]);
!isCompleted && !isQuiz && updateLessonItemCompletion(lessonItemId);
handleCompletion();
} else {
let newSelectedOptions: string[];

Expand All @@ -87,13 +91,13 @@ export const Question = ({

setSelectedOption(newSelectedOptions);
await sendAnswer(newSelectedOptions);
!isCompleted && !isQuiz && updateLessonItemCompletion(lessonItemId);
handleCompletion();
}
};

const handleOpenAnswerRequest = async (e: ChangeEvent<HTMLTextAreaElement>) => {
await sendOpenAnswer(e.target.value);
!isCompleted && !isQuiz && updateLessonItemCompletion(lessonItemId);
handleCompletion();
};

const canRenderCorrectAnswers =
Expand Down
3 changes: 3 additions & 0 deletions apps/web/app/utils/handleCompletionForMediaLesson.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function handleCompletionForMediaLesson(isCompleted: boolean, isQuiz: boolean): boolean {
return !isCompleted && !isQuiz;
}

0 comments on commit d0aa4a7

Please sign in to comment.