chore: clean up the appointment filtering

This commit is contained in:
Livio Bieri 2023-09-28 17:44:38 +02:00
parent daaecb57a0
commit 86e7e0f82e
1 changed files with 45 additions and 44 deletions

View File

@ -3,81 +3,82 @@ import { computed, ref, watch } from "vue";
import { useCourseSessionsStore } from "@/stores/courseSessions";
import { useLearningPathStore } from "@/stores/learningPath";
import ItDropdownSelect from "@/components/ui/ItDropdownSelect.vue";
import type { DueDate } from "@/types";
const UNFILTERED = Number.MAX_SAFE_INTEGER;
const courseSessionsStore = useCourseSessionsStore();
const learningPathStore = useLearningPathStore();
function getCourseSession(id: number) {
return courseSessionsStore.allCourseSessions.find((cs) => cs.id === id);
}
type Item = {
id: number;
name: string;
};
const allItem = {
const initialItem: Item = {
id: UNFILTERED,
name: "Alle",
};
const circles = ref([{ ...allItem }]);
const circles = ref<Item[]>([initialItem]);
const courseSessions = [
{ ...allItem },
...courseSessionsStore.allCourseSessions.map((courseSession) => ({
id: courseSession.id,
name: courseSession.title,
})),
const courseSessions: Item[] = [
initialItem,
...courseSessionsStore.allCourseSessions.map((cs) => ({ id: cs.id, name: cs.title })),
];
const selectedSession = ref(courseSessions[0]);
const selectedCircle = ref(circles.value[0]);
const selectedSession = ref<Item>(courseSessions[0]);
const selectedCircle = ref<Item>(circles.value[0]);
watch(selectedSession, async () => {
const cs = getCourseSession(selectedSession.value.id);
const isUnfiltered = selectedSession.value.id === UNFILTERED;
const courseSession = courseSessionsStore.allCourseSessions.find(
(item) => item.id === selectedSession.value.id
);
if (selectedSession.value.id === UNFILTERED || !cs) {
circles.value = [{ ...allItem }];
selectedCircle.value = circles.value[0];
if (!courseSession || isUnfiltered) {
resetCircles();
return;
}
const data = await learningPathStore.loadLearningPath(
cs.course.slug + "-lp",
`${courseSession.course.slug}-lp`,
undefined,
false,
false
);
circles.value =
data?.circles.map((circle) => ({
id: circle.id,
name: circle.title,
})) || [];
circles.value = [{ ...allItem }, ...circles.value];
selectedCircle.value = circles.value[0];
updateCircles(data?.circles);
});
const resetCircles = () => {
circles.value = [initialItem];
selectedCircle.value = circles.value[0];
};
const updateCircles = (newCircles?: { id: number; title: string }[]) => {
circles.value = newCircles
? [
initialItem,
...newCircles.map((circle) => ({ id: circle.id, name: circle.title })),
]
: [initialItem];
selectedCircle.value = circles.value[0];
};
const appointments = computed(() => {
console.log("recomputing appointments");
console.log("current session", selectedSession.value.name);
console.log("current circle", selectedCircle.value.name);
const filtered = courseSessionsStore
return courseSessionsStore
.allDueDates()
.filter(
(appointment) =>
// first filter by course session
selectedSession.value.id === UNFILTERED ||
appointment.course_session === selectedSession.value.id
)
.filter(
(appointment) =>
// then filter by circle
selectedCircle.value.id === UNFILTERED ||
appointment.circle?.id === selectedCircle.value.id
);
console.table(filtered);
return filtered;
.filter((dueDate) => isMatchingSession(dueDate) && isMatchingCircle(dueDate));
});
const isMatchingSession = (dueDate: DueDate) =>
selectedSession.value.id === UNFILTERED ||
dueDate.course_session === selectedSession.value.id;
const isMatchingCircle = (dueDate: DueDate) =>
selectedCircle.value.id === UNFILTERED ||
dueDate.circle?.id === selectedCircle.value.id;
</script>
<template>