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