From 2c8ce133f7b4245a62f377b532fbc4ef0a87a096 Mon Sep 17 00:00:00 2001 From: Jannat Patel Date: Mon, 2 Jun 2025 19:12:13 +0530 Subject: [PATCH] fix: quiz and time validation before linking to video --- .../src/components/Modals/QuizInVideo.vue | 58 +++++++++++++++---- frontend/src/components/VideoBlock.vue | 11 +--- frontend/src/pages/LessonForm.vue | 2 +- frontend/src/utils/index.js | 7 +++ .../doctype/course_lesson/course_lesson.py | 3 - 5 files changed, 58 insertions(+), 23 deletions(-) diff --git a/frontend/src/components/Modals/QuizInVideo.vue b/frontend/src/components/Modals/QuizInVideo.vue index 18575fa3..623d8220 100644 --- a/frontend/src/components/Modals/QuizInVideo.vue +++ b/frontend/src/components/Modals/QuizInVideo.vue @@ -60,9 +60,15 @@ @@ -107,17 +113,18 @@ import { } from 'frappe-ui' import { computed, reactive, ref, watch } from 'vue' import { Plus, Trash2 } from 'lucide-vue-next' +import { formatTimestamp } from '@/utils' import Link from '@/components/Controls/Link.vue' type Quiz = { - time: number + time: string quiz: string } const show = defineModel() const allQuizzes = ref([]) const quiz = reactive({ - time: 0, + time: '', quiz: '', }) @@ -137,10 +144,9 @@ const props = defineProps({ }) const addQuiz = () => { - if (quiz.time > props.duration) { - toast.error(__('Time in video exceeds the total duration of the video.')) - return - } + quiz.time = `${getTimeInSeconds()}` + if (!isTimeValid() || !isFormComplete()) return + allQuizzes.value.push({ time: quiz.time, quiz: quiz.quiz, @@ -148,10 +154,42 @@ const addQuiz = () => { props.saveQuizzes(allQuizzes.value) - quiz.time = 0 + quiz.time = '' quiz.quiz = '' } +const getTimeInSeconds = () => { + if (quiz.time && !quiz.time.includes(':')) { + quiz.time = `${quiz.time}:00` + } + const timeParts = quiz.time.split(':') + const timeInSeconds = parseInt(timeParts[0]) * 60 + parseInt(timeParts[1]) + + return timeInSeconds +} + +const isTimeValid = () => { + if (parseInt(quiz.time) > props.duration) { + toast.error(__('Time in video exceeds the total duration of the video.')) + return false + } + return true +} + +const isFormComplete = () => { + if (!quiz.time) { + toast.error(__('Please enter a valid timestamp')) + return false + } + + if (!quiz.quiz) { + toast.error(__('Please select a quiz')) + return false + } + + return true +} + const removeQuiz = (selections: string, unselectAll: () => void) => { Array.from(selections).forEach((selection) => { const index = allQuizzes.value.findIndex((q) => q.quiz === selection) diff --git a/frontend/src/components/VideoBlock.vue b/frontend/src/components/VideoBlock.vue index e5c06a76..edfd3ae8 100644 --- a/frontend/src/components/VideoBlock.vue +++ b/frontend/src/components/VideoBlock.vue @@ -118,10 +118,10 @@ />