2021-07-13 16:27:02 +00:00
|
|
|
<script context="module">
|
2021-08-18 19:47:02 +03:00
|
|
|
import {
|
|
|
|
|
fetchCourse,
|
|
|
|
|
fetchLesson,
|
|
|
|
|
} from '../../../../../utils/services/courses';
|
|
|
|
|
|
|
|
|
|
export async function preload({ params }, session) {
|
|
|
|
|
return {
|
2021-08-24 10:45:43 +03:00
|
|
|
params,
|
2021-08-18 19:47:02 +03:00
|
|
|
};
|
2021-07-13 16:27:02 +00:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<script>
|
2021-08-18 19:47:02 +03:00
|
|
|
import { onMount, afterUpdate } from 'svelte';
|
2021-07-14 09:05:09 +03:00
|
|
|
import { stores } from '@sapper/app';
|
|
|
|
|
import CourseContainer from '../../../../../components/CourseContainer/index.svelte';
|
2021-07-13 16:27:02 +00:00
|
|
|
|
2021-07-14 09:05:09 +03:00
|
|
|
import PrimaryButton from '../../../../../components/PrimaryButton/index.svelte';
|
|
|
|
|
import PageNav from '../../../../../components/PageNav/index.svelte';
|
|
|
|
|
import PageBody from '../../../../../components/PageBody/index.svelte';
|
|
|
|
|
import Materials from '../../../../../components/Course/components/Lesson/Materials/index.svelte';
|
|
|
|
|
import Exercises from '../../../../../components/Course/components/Lesson/Exercises/index.svelte';
|
|
|
|
|
import MODES from '../../../../../utils/constants/mode.js';
|
2021-08-24 10:45:43 +03:00
|
|
|
import {
|
|
|
|
|
setCourseData,
|
|
|
|
|
course,
|
|
|
|
|
} from '../../../../../components/Course/store';
|
2021-08-18 19:47:02 +03:00
|
|
|
import { lesson } from '../../../../../components/Course/components/Lesson/store/lessons';
|
2021-07-13 16:27:02 +00:00
|
|
|
|
2021-08-24 10:45:43 +03:00
|
|
|
export let params;
|
2021-07-13 16:27:02 +00:00
|
|
|
export let lessonId;
|
|
|
|
|
export let exerciseId;
|
2021-08-18 19:47:02 +03:00
|
|
|
export let isMaterialsTabActive;
|
2021-07-13 16:27:02 +00:00
|
|
|
|
|
|
|
|
let path;
|
2021-07-14 09:05:09 +03:00
|
|
|
let mode = MODES.view;
|
2021-08-24 10:45:43 +03:00
|
|
|
let prevLessonId;
|
2021-07-13 16:27:02 +00:00
|
|
|
|
|
|
|
|
const { page } = stores();
|
|
|
|
|
|
2021-08-24 10:45:43 +03:00
|
|
|
async function fetchReqData() {
|
|
|
|
|
const { id: courseId, lessonParams = [] } = params;
|
|
|
|
|
const [_lessonId, exerciseRouteName, _exerciseId] = lessonParams;
|
|
|
|
|
|
|
|
|
|
let courseData;
|
|
|
|
|
let lessonData;
|
|
|
|
|
|
|
|
|
|
if (!$course.id) {
|
|
|
|
|
const [_course, lesson] = await Promise.all([
|
|
|
|
|
fetchCourse(courseId),
|
|
|
|
|
fetchLesson(_lessonId),
|
|
|
|
|
]);
|
|
|
|
|
|
|
|
|
|
courseData = _course.data;
|
|
|
|
|
lessonData = lesson.data;
|
|
|
|
|
} else if (prevLessonId && prevLessonId !== _lessonId) {
|
|
|
|
|
const lesson = await fetchLesson(_lessonId);
|
|
|
|
|
|
|
|
|
|
lessonData = lesson.data;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
prevLessonId = _lessonId;
|
|
|
|
|
lessonId = _lessonId;
|
|
|
|
|
exerciseId = _exerciseId;
|
|
|
|
|
isMaterialsTabActive = !exerciseRouteName;
|
|
|
|
|
|
|
|
|
|
setCourseData(courseData);
|
|
|
|
|
setLesson(lessonData);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function setLesson(lessonData) {
|
2021-08-18 19:47:02 +03:00
|
|
|
if (!lessonData) return;
|
|
|
|
|
|
|
|
|
|
lesson.update((l) => ({
|
|
|
|
|
...l,
|
|
|
|
|
id: lessonId,
|
|
|
|
|
materials: lessonData,
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$: path = $page.path.replace(/\/exercises[\/ 0-9 a-z -]*/, '');
|
2021-08-21 13:26:43 +03:00
|
|
|
|
|
|
|
|
$: {
|
2021-08-24 10:45:43 +03:00
|
|
|
console.log(`params`, params);
|
|
|
|
|
if (params) {
|
2021-08-21 13:26:43 +03:00
|
|
|
mode = MODES.view;
|
2021-08-24 10:45:43 +03:00
|
|
|
fetchReqData();
|
2021-08-21 13:26:43 +03:00
|
|
|
}
|
|
|
|
|
}
|
2021-07-13 16:27:02 +00:00
|
|
|
</script>
|
|
|
|
|
|
2021-08-18 19:47:02 +03:00
|
|
|
<CourseContainer {path} isExercisePage={!isMaterialsTabActive && exerciseId}>
|
2021-07-13 16:27:02 +00:00
|
|
|
<PageNav
|
|
|
|
|
navItems={[
|
|
|
|
|
{
|
2021-07-14 09:05:09 +03:00
|
|
|
label: 'Materials',
|
|
|
|
|
isActive: isMaterialsTabActive,
|
2021-07-13 16:27:02 +00:00
|
|
|
href: path,
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-07-14 09:05:09 +03:00
|
|
|
label: 'Exercises',
|
|
|
|
|
isActive: !isMaterialsTabActive,
|
2021-07-13 16:27:02 +00:00
|
|
|
href: `${path}/exercises`,
|
|
|
|
|
},
|
|
|
|
|
]}
|
2021-07-14 09:05:09 +03:00
|
|
|
>
|
|
|
|
|
<svelte:fragment slot="widget">
|
2021-07-18 19:12:39 +03:00
|
|
|
{#if isMaterialsTabActive}
|
|
|
|
|
<div class="flex items-center">
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
className="mr-2"
|
|
|
|
|
label={mode === MODES.edit ? 'Save' : 'Edit'}
|
|
|
|
|
onClick={() =>
|
|
|
|
|
(mode = mode === MODES.edit ? MODES.view : MODES.edit)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
2021-07-14 09:05:09 +03:00
|
|
|
</svelte:fragment>
|
|
|
|
|
</PageNav>
|
2021-07-13 16:27:02 +00:00
|
|
|
|
2021-08-18 19:47:02 +03:00
|
|
|
{#if !isMaterialsTabActive}
|
|
|
|
|
<Exercises {lessonId} {exerciseId} path={`${path}/exercises`} />
|
2021-08-24 10:45:43 +03:00
|
|
|
{:else if !!lessonId}
|
|
|
|
|
<PageBody>
|
2021-07-14 09:05:09 +03:00
|
|
|
<Materials {lessonId} {mode} />
|
2021-08-18 19:47:02 +03:00
|
|
|
</PageBody>
|
|
|
|
|
{/if}
|
2021-07-13 16:27:02 +00:00
|
|
|
</CourseContainer>
|