2023-12-03 18:05:11 +03:00
|
|
|
<script lang="ts">
|
2024-04-16 08:51:53 +01:00
|
|
|
import { goto } from '$app/navigation';
|
2025-11-01 15:49:29 +01:00
|
|
|
import { page } from '$app/state';
|
2025-01-04 05:55:14 +01:00
|
|
|
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';
|
2025-01-04 05:55:14 +01:00
|
|
|
import { handleAddLessonWidget } from '$lib/components/Course/components/Lesson/store';
|
2025-11-01 15:49:29 +01:00
|
|
|
import { handleDelete, lessons, lessonSections } from '$lib/components/Course/components/Lesson/store/lessons';
|
2025-01-04 05:55:14 +01:00
|
|
|
import { course } from '$lib/components/Course/store';
|
2025-11-01 15:49:29 +01:00
|
|
|
import { CourseContainer } from '$lib/components/CourseContainer';
|
2025-01-04 05:55:14 +01:00
|
|
|
import { PageBody, PageNav } from '$lib/components/Page';
|
|
|
|
|
import { VARIANTS } from '$lib/components/PrimaryButton/constants';
|
|
|
|
|
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
2025-12-02 09:54:38 +01:00
|
|
|
import { RoleBasedSecurity } from '$lib/features/ui';
|
2025-01-04 05:55:14 +01:00
|
|
|
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';
|
2021-07-10 19:16:10 +00:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
let { data } = $props();
|
2023-09-30 10:43:13 +01:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
const query = new URLSearchParams(page.url.search);
|
|
|
|
|
|
|
|
|
|
const lessonsLength = $derived($course.version === COURSE_VERSION.V1 ? $lessons.length : $lessonSections.length);
|
2024-04-16 08:51:53 +01:00
|
|
|
|
2023-12-03 18:05:11 +03:00
|
|
|
let lessonEditing: string | undefined;
|
2025-11-01 15:49:29 +01:00
|
|
|
let lessonToDelete: Lesson | undefined = $state();
|
|
|
|
|
let openDeleteModal: boolean = $state(false);
|
|
|
|
|
let isFetching: boolean = $state(false);
|
|
|
|
|
let reorder = $state(false);
|
|
|
|
|
let activateSections = $state(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-08-17 17:10:15 +01:00
|
|
|
const getLessons = () => {
|
|
|
|
|
if ($course.version === COURSE_VERSION.V1) {
|
|
|
|
|
return $lessons;
|
|
|
|
|
} else {
|
|
|
|
|
const _lessons: Lesson[] = [];
|
|
|
|
|
|
|
|
|
|
$lessonSections.forEach((section) => {
|
|
|
|
|
_lessons.push(...section.lessons);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return _lessons;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2024-04-16 08:51:53 +01:00
|
|
|
function findFirstIncompleteLesson() {
|
2024-08-17 17:10:15 +01:00
|
|
|
return getLessons().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`);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
let shouldGoToNextLesson = $derived(query.get('next') === 'true');
|
|
|
|
|
$effect(() => {
|
|
|
|
|
!isFetching && shouldGoToNextLesson && onNextQuery($lessons);
|
|
|
|
|
});
|
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
|
|
|
|
2024-08-17 17:10:15 +01:00
|
|
|
<ActivateSectionsModal bind:open={activateSections} />
|
|
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
<DeleteLessonConfirmation bind:openDeleteModal deleteLesson={() => handleDelete(lessonToDelete?.id)} />
|
2023-12-03 18:05:11 +03:00
|
|
|
|
2025-11-01 15:49:29 +01:00
|
|
|
<CourseContainer
|
|
|
|
|
onFetchingChange={(fetching) => {
|
|
|
|
|
isFetching = fetching;
|
|
|
|
|
}}
|
|
|
|
|
courseId={data.courseId}
|
|
|
|
|
>
|
2024-08-17 17:10:15 +01:00
|
|
|
<PageNav title={$t('course.navItem.lessons.heading_v2')}>
|
2025-11-01 15:49:29 +01:00
|
|
|
{#snippet widget()}
|
|
|
|
|
<div 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}
|
2024-08-17 17:10:15 +01:00
|
|
|
<PrimaryButton
|
2025-11-01 15:49:29 +01:00
|
|
|
label={$t(`course.navItem.lessons.add_lesson.${reorder ? 'end_reorder' : 'start_reorder'}`)}
|
2024-08-17 17:10:15 +01:00
|
|
|
variant={VARIANTS.OUTLINED}
|
2025-11-01 15:49:29 +01:00
|
|
|
onClick={() => (reorder = !reorder)}
|
2024-08-17 17:10:15 +01:00
|
|
|
isDisabled={!!lessonEditing}
|
|
|
|
|
/>
|
2025-11-01 15:49:29 +01:00
|
|
|
<PrimaryButton
|
|
|
|
|
label={$t('course.navItem.lessons.add_lesson.button_title')}
|
|
|
|
|
onClick={addLesson}
|
|
|
|
|
isDisabled={!!lessonEditing}
|
|
|
|
|
/>
|
|
|
|
|
</RoleBasedSecurity>
|
|
|
|
|
</div>
|
|
|
|
|
{/snippet}
|
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}
|
2025-01-04 05:55:14 +01:00
|
|
|
<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}
|
2024-08-17 17:10:15 +01:00
|
|
|
<LessonSectionList {reorder} {lessonEditing} />
|
2024-08-14 10:27:37 +01:00
|
|
|
{/if}
|
2023-09-24 07:08:37 +00:00
|
|
|
{:else}
|
|
|
|
|
<Box className="w-full lg:w-11/12 lg:px-4 m-auto">
|
2025-01-04 05:55:14 +01:00
|
|
|
<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')}
|
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>
|