From a180c5c82568fcb4473fa9454a686a8f72fd1bbb Mon Sep 17 00:00:00 2001 From: Daniel Egger Date: Wed, 4 Oct 2023 17:39:04 +0200 Subject: [PATCH] Filter appointments by course --- .../src/components/dueDates/DueDatesList.vue | 1 + client/src/pages/AppointmentsPage.vue | 119 +++++++++--------- cypress/e2e/appointments.cy.js | 48 +++---- 3 files changed, 83 insertions(+), 85 deletions(-) diff --git a/client/src/components/dueDates/DueDatesList.vue b/client/src/components/dueDates/DueDatesList.vue index 95818ff7..f5ce0702 100644 --- a/client/src/components/dueDates/DueDatesList.vue +++ b/client/src/components/dueDates/DueDatesList.vue @@ -27,6 +27,7 @@ const dueDatesDisplayed = computed(() => {
  • -import { computed, ref, watch } from "vue"; +import { computed, onMounted, ref, watch } from "vue"; import { useCourseSessionsStore } from "@/stores/courseSessions"; import { useLearningPathStore } from "@/stores/learningPath"; import { useTranslation } from "i18next-vue"; @@ -18,74 +18,71 @@ type Item = { name: string; }; +const courses: (Item & { slug: string })[] = + courseSessionsStore.uniqueCourseSessionsByCourse.map((cs) => ({ + id: cs.course.id, + name: cs.course.title, + slug: cs.course.slug, + })); +const selectedCourse = ref(courses[0]); + +const courseSessions = computed(() => { + return [ + { + id: UNFILTERED, + name: t("a.AlleDurchführungen"), + }, + ...courseSessionsStore.allCourseSessions + .filter((cs) => cs.course.id === selectedCourse.value.id) + .map((cs) => ({ id: cs.id, name: cs.title })), + ]; +}); +const selectedSession = ref(courseSessions.value[0]); + const initialItemCircle: Item = { id: UNFILTERED, name: t("a.AlleCircle"), }; - const circles = ref([initialItemCircle]); -const courseSessions: Item[] = [ - { - id: UNFILTERED, - name: t("a.AlleDurchführungen"), - }, - ...courseSessionsStore.allCourseSessions.map((cs) => ({ id: cs.id, name: cs.title })), -]; - -const selectedSession = ref(courseSessions[0]); const selectedCircle = ref(circles.value[0]); -if (courseSessionsStore.currentCourseSession) { - selectedSession.value = { - id: courseSessionsStore.currentCourseSession.id, - name: courseSessionsStore.currentCourseSession.title, - }; -} - -watch(selectedSession, async () => { - // We don't have all circles for the unfiltered session, - // we fetch them from the learning path when the session changes - const isUnfiltered = selectedSession.value.id === UNFILTERED; - const courseSession = courseSessionsStore.allCourseSessions.find( - (item) => item.id === selectedSession.value.id - ); - - if (!courseSession || isUnfiltered) { - resetCircles(); - return; - } - +async function loadCircleValues() { const data = await learningPathStore.loadLearningPath( - `${courseSession.course.slug}-lp`, + `${selectedCourse.value.slug}-lp`, undefined, false, false ); - if (data) { - updateCircles(data.circles); + circles.value = [ + initialItemCircle, + ...data.circles.map((circle) => ({ id: circle.id, name: circle.title })), + ]; } else { - resetCircles(); + circles.value = [initialItemCircle]; } + + selectedCircle.value = circles.value[0]; +} + +watch(selectedCourse, async () => { + selectedSession.value = courseSessions.value[0]; + await loadCircleValues(); }); -const resetCircles = () => { - circles.value = [initialItemCircle]; - selectedCircle.value = circles.value[0]; -}; - -const updateCircles = (newCircles: { id: number; title: string }[]) => { - circles.value = [ - initialItemCircle, - ...newCircles.map((circle) => ({ id: circle.id, name: circle.title })), - ]; - selectedCircle.value = circles.value[0]; -}; +onMounted(async () => { + await loadCircleValues(); +}); const appointments = computed(() => { return courseSessionsStore .allDueDates() - .filter((dueDate) => isMatchingSession(dueDate) && isMatchingCircle(dueDate)); + .filter( + (dueDate) => + isMatchingCourse(dueDate) && + isMatchingSession(dueDate) && + isMatchingCircle(dueDate) + ); }); const isMatchingSession = (dueDate: DueDate) => @@ -96,6 +93,9 @@ const isMatchingCircle = (dueDate: DueDate) => selectedCircle.value.id === UNFILTERED || dueDate.circle?.id === selectedCircle.value.id; +const isMatchingCourse = (dueDate: DueDate) => + courseSessions.value.map((cs) => cs.id).includes(dueDate.course_session as number); + const numAppointmentsToShow = ref(7); const canLoadMore = computed(() => { return numAppointmentsToShow.value < appointments.value.length; @@ -109,8 +109,15 @@ async function loadAdditionalAppointments() {