Files
classroomio/apps/dashboard/src/routes/courses/[id]/lessons/+page.svelte
T

160 lines
5.8 KiB
Svelte
Raw Normal View History

<script lang="ts">
import { goto } from '$app/navigation';
import { page } from '$app/stores';
import Box from '$lib/components/Box/index.svelte';
import ActivateSectionsModal from '$lib/components/Course/components/Lesson/ActivateSectionsModal.svelte';
import DeleteLessonConfirmation from '$lib/components/Course/components/Lesson/DeleteLessonConfirmation.svelte';
2024-08-14 10:27:37 +01:00
import LessonList from '$lib/components/Course/components/Lesson/LessonList.svelte';
import LessonSectionList from '$lib/components/Course/components/Lesson/LessonSectionList.svelte';
import NewLessonModal from '$lib/components/Course/components/Lesson/NewLessonModal.svelte';
import { handleAddLessonWidget } from '$lib/components/Course/components/Lesson/store';
import {
handleDelete,
lessons,
lessonSections
} from '$lib/components/Course/components/Lesson/store/lessons';
import { course } from '$lib/components/Course/store';
import CourseContainer from '$lib/components/CourseContainer/index.svelte';
import { PageBody, PageNav } from '$lib/components/Page';
import { VARIANTS } from '$lib/components/PrimaryButton/constants';
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
import RoleBasedSecurity from '$lib/components/RoleBasedSecurity/index.svelte';
import { t } from '$lib/utils/functions/translations';
import { profile } from '$lib/utils/store/user';
import type { Lesson } from '$lib/utils/types';
import { COURSE_VERSION } from '$lib/utils/types';
export let data;
2023-09-30 10:43:13 +01:00
const query = new URLSearchParams($page.url.search);
let lessonEditing: string | undefined;
let lessonToDelete: Lesson | undefined;
let openDeleteModal: boolean = false;
2024-08-11 11:59:17 +01:00
let isFetching: boolean = false;
2024-06-29 06:27:29 +01:00
let reorder = false;
let activateSections = false;
2021-09-13 14:02:18 +03:00
function addLesson() {
$handleAddLessonWidget.open = true;
2024-08-14 11:13:44 +01:00
$handleAddLessonWidget.isSection = $course.version === COURSE_VERSION.V2;
2021-09-14 07:01:44 +03:00
}
2024-07-16 10:20:56 +01:00
function hasUserCompletedLesson(completion) {
return completion?.find((c) => c.profile_id === $profile.id);
}
const getLessons = () => {
if ($course.version === COURSE_VERSION.V1) {
return $lessons;
} else {
const _lessons: Lesson[] = [];
$lessonSections.forEach((section) => {
_lessons.push(...section.lessons);
});
return _lessons;
}
};
function findFirstIncompleteLesson() {
return getLessons().find(
2024-07-16 10:20:56 +01:00
(lesson) => !hasUserCompletedLesson(lesson.lesson_completion) && lesson.is_unlocked === true
);
}
function onNextQuery(lessons) {
if (!isFetching && lessons.length > 0) {
const incompleteLesson = findFirstIncompleteLesson();
if (incompleteLesson) {
goto(`/courses/${data.courseId}/lessons/${incompleteLesson.id}`);
} else {
goto(`/courses/${data.courseId}/lessons`);
}
}
}
$: shouldGoToNextLesson = query.get('next') === 'true';
2024-07-18 02:58:57 +01:00
$: !isFetching && shouldGoToNextLesson && onNextQuery($lessons);
2024-08-14 10:27:37 +01:00
$: lessonsLength =
$course.version === COURSE_VERSION.V1 ? $lessons.length : $lessonSections.length;
</script>
2024-08-14 10:27:37 +01:00
<NewLessonModal />
<ActivateSectionsModal bind:open={activateSections} />
<DeleteLessonConfirmation
bind:openDeleteModal
deleteLesson={() => handleDelete(lessonToDelete?.id)}
/>
2024-05-23 07:04:42 +01:00
<CourseContainer bind:isFetching bind:courseId={data.courseId}>
<PageNav title={$t('course.navItem.lessons.heading_v2')}>
2024-06-29 06:27:29 +01:00
<div slot="widget" class="flex w-full justify-end gap-2">
<RoleBasedSecurity allowedRoles={[1, 2]}>
{#if $course.version === COURSE_VERSION.V1}
<PrimaryButton
label={$t(`course.navItem.lessons.section_prompt.cta`)}
variant={VARIANTS.OUTLINED}
onClick={() => (activateSections = !activateSections)}
isDisabled={!!lessonEditing}
/>
{/if}
<PrimaryButton
label={$t(
`course.navItem.lessons.add_lesson.${reorder ? 'end_reorder' : 'start_reorder'}`
)}
variant={VARIANTS.OUTLINED}
onClick={() => (reorder = !reorder)}
isDisabled={!!lessonEditing}
/>
2024-04-19 05:45:48 +01:00
<PrimaryButton
label={$t('course.navItem.lessons.add_lesson.button_title')}
onClick={addLesson}
isDisabled={!!lessonEditing}
/>
2021-09-14 07:01:44 +03:00
</RoleBasedSecurity>
</div>
</PageNav>
2023-08-09 15:25:31 +00:00
<PageBody width="max-w-6xl" padding="p-0">
{#if shouldGoToNextLesson}
<Box className="w-full lg:w-11/12 lg:px-4 m-auto">
<div class="flex flex-col items-center justify-between">
<img src="/images/empty-lesson-icon.svg" alt="Lesson" class="mx-auto my-2.5" />
2024-06-29 06:27:29 +01:00
<h2 class="my-1.5 text-xl font-normal">{$t('course.navItem.lessons.no_lesson')}</h2>
<p class="text-center text-sm text-slate-500">
2024-06-29 06:27:29 +01:00
{$t('course.navItem.lessons.share_your_knowledge')}
</p>
</div>
</Box>
2024-08-14 10:27:37 +01:00
{:else if lessonsLength > 0}
2024-06-29 06:27:29 +01:00
{#if reorder}
<p class="text-center text-xs italic text-gray-400 dark:text-white">
2024-06-29 06:27:29 +01:00
{$t('course.navItem.lessons.drag')}
</p>
{/if}
2021-09-11 21:52:46 +03:00
2024-08-14 10:27:37 +01:00
{#if $course.version === COURSE_VERSION.V1}
<LessonList {reorder} {lessonEditing} bind:lessonToDelete bind:openDeleteModal />
{:else if $course.version === COURSE_VERSION.V2}
<LessonSectionList {reorder} {lessonEditing} />
2024-08-14 10:27:37 +01:00
{/if}
{:else}
<Box className="w-full lg:w-11/12 lg:px-4 m-auto">
<div class="flex flex-col items-center justify-between">
<img src="/images/empty-lesson-icon.svg" alt="Lesson" class="mx-auto my-2.5" />
<h2 class="my-1.5 text-xl font-normal">{$t('course.navItem.lessons.body_header')}</h2>
<p class="text-center text-sm text-slate-500">
2024-04-19 05:45:48 +01:00
{$t('course.navItem.lessons.body_content')}
</p>
</div>
</Box>
{/if}
</PageBody>
</CourseContainer>