VBV-302: Add course session switch

This commit is contained in:
Daniel Egger 2023-03-31 16:24:52 +02:00
parent 18f7728793
commit ddc7a3bd70
3 changed files with 24 additions and 26 deletions

View File

@ -2,23 +2,13 @@
import IconLogout from "@/components/icons/IconLogout.vue"; import IconLogout from "@/components/icons/IconLogout.vue";
import type { UserState } from "@/stores/user"; import type { UserState } from "@/stores/user";
import type { CourseSession } from "@/types"; import type { CourseSession } from "@/types";
import { useRouter } from "vue-router";
const router = useRouter();
const props = defineProps<{ const props = defineProps<{
courseSessions: CourseSession[]; courseSessions: CourseSession[];
user: UserState | undefined; user: UserState | undefined;
}>(); }>();
const emits = defineEmits(["closemodal", "logout"]); const emits = defineEmits(["selectCourseSession", "logout"]);
const clickLink = (to: string | undefined) => {
if (to) {
router.push(to);
emits("closemodal");
}
};
</script> </script>
<template> <template>
@ -44,7 +34,7 @@ const clickLink = (to: string | undefined) => {
<div v-if="props.courseSessions.length" class="border-b py-4"> <div v-if="props.courseSessions.length" class="border-b py-4">
<div v-for="cs in props.courseSessions" :key="cs.id"> <div v-for="cs in props.courseSessions" :key="cs.id">
{{ cs.title }} <button @click="$emit('selectCourseSession', cs)">{{ cs.title }}</button>
</div> </div>
</div> </div>

View File

@ -9,6 +9,7 @@ import ItFullScreenModal from "@/components/ui/ItFullScreenModal.vue";
import { useCourseSessionsStore } from "@/stores/courseSessions"; import { useCourseSessionsStore } from "@/stores/courseSessions";
import { useNotificationsStore } from "@/stores/notifications"; import { useNotificationsStore } from "@/stores/notifications";
import { useUserStore } from "@/stores/user"; import { useUserStore } from "@/stores/user";
import type { CourseSession } from "@/types";
import { Popover, PopoverButton, PopoverPanel } from "@headlessui/vue"; import { Popover, PopoverButton, PopoverPanel } from "@headlessui/vue";
import { breakpointsTailwind, useBreakpoints } from "@vueuse/core"; import { breakpointsTailwind, useBreakpoints } from "@vueuse/core";
import { computed, onMounted, reactive } from "vue"; import { computed, onMounted, reactive } from "vue";
@ -56,6 +57,10 @@ function logout() {
userStore.handleLogout(); userStore.handleLogout();
} }
function selectCourseSession(courseSession: CourseSession) {
courseSessionsStore.switchCourseSession(courseSession);
}
function popoverClick(event) { function popoverClick(event) {
if (breakpoints.smaller("lg").value) { if (breakpoints.smaller("lg").value) {
event.preventDefault(); event.preventDefault();
@ -216,6 +221,7 @@ onMounted(() => {
:course-sessions="courseSessionsStore.courseSessionsForRoute" :course-sessions="courseSessionsStore.courseSessionsForRoute"
:user="userStore" :user="userStore"
@logout="logout" @logout="logout"
@select-course-session="selectCourseSession"
/> />
</div> </div>
</PopoverPanel> </PopoverPanel>

View File

@ -6,6 +6,7 @@ import type {
CourseSessionUser, CourseSessionUser,
ExpertSessionUser, ExpertSessionUser,
} from "@/types"; } from "@/types";
import { useLocalStorage } from "@vueuse/core";
import uniqBy from "lodash/uniqBy"; import uniqBy from "lodash/uniqBy";
import log from "loglevel"; import log from "loglevel";
import { defineStore } from "pinia"; import { defineStore } from "pinia";
@ -14,17 +15,13 @@ import { useRoute } from "vue-router";
import { useCircleStore } from "./circle"; import { useCircleStore } from "./circle";
import { useUserStore } from "./user"; import { useUserStore } from "./user";
export type CourseSessionsStoreState = {
courseSessions: CourseSession[] | undefined;
};
export type LearningSequenceCircleDocument = { export type LearningSequenceCircleDocument = {
id: number; id: number;
title: string; title: string;
documents: CircleDocument[]; documents: CircleDocument[];
}; };
const SELECTED_COURSE_SESSIONS_KEY = "selectedCourseSessions"; const SELECTED_COURSE_SESSIONS_KEY = "selectedCourseSessionMap";
function loadCourseSessionsData(reload = false) { function loadCourseSessionsData(reload = false) {
log.debug("loadCourseSessionsData called"); log.debug("loadCourseSessionsData called");
@ -70,6 +67,10 @@ export const useCourseSessionsStore = defineStore("courseSessions", () => {
// store should do own setup, we don't want to have each component initialize it // store should do own setup, we don't want to have each component initialize it
// that's why we call the load function in here // that's why we call the load function in here
const { courseSessions } = loadCourseSessionsData(); const { courseSessions } = loadCourseSessionsData();
const selectedCourseSessionMap = useLocalStorage(
SELECTED_COURSE_SESSIONS_KEY,
new Map<string, number>()
);
// these will become getters // these will become getters
const userCourses = computed(() => const userCourses = computed(() =>
@ -80,16 +81,12 @@ export const useCourseSessionsStore = defineStore("courseSessions", () => {
function selectedCourseSessionForCourse(courseSlug: string) { function selectedCourseSessionForCourse(courseSlug: string) {
try { try {
const data = window.localStorage.getItem(SELECTED_COURSE_SESSIONS_KEY); const courseSessionId = selectedCourseSessionMap.value.get(courseSlug);
if (data) {
const map = new Map(JSON.parse(data));
const courseSessionId = map.get(courseSlug);
if (courseSessionId) { if (courseSessionId) {
return courseSessions.value.find((cs) => { return courseSessions.value.find((cs) => {
return cs.id === courseSessionId; return cs.id === courseSessionId;
}); });
} }
}
} catch (e) { } catch (e) {
log.error("Error while parsing courseSessions from localStorage", e); log.error("Error while parsing courseSessions from localStorage", e);
} }
@ -97,6 +94,10 @@ export const useCourseSessionsStore = defineStore("courseSessions", () => {
return undefined; return undefined;
} }
function switchCourseSession(courseSession: CourseSession) {
selectedCourseSessionMap.value.set(courseSession.course.slug, courseSession.id);
}
function courseSessionForCourse(courseSlug: string) { function courseSessionForCourse(courseSlug: string) {
if (courseSlug) { if (courseSlug) {
const courseSession = selectedCourseSessionForCourse(courseSlug); const courseSession = selectedCourseSessionForCourse(courseSlug);
@ -219,6 +220,7 @@ export const useCourseSessionsStore = defineStore("courseSessions", () => {
courseSessionForRoute, courseSessionForRoute,
courseSessionsForRoute, courseSessionsForRoute,
courseSessionsForCourse, courseSessionsForCourse,
switchCourseSession,
hasCockpit, hasCockpit,
canUploadCircleDocuments, canUploadCircleDocuments,
circleDocuments, circleDocuments,