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

299 lines
10 KiB
Svelte
Raw Normal View History

<script>
2021-08-18 19:47:02 +03:00
import { onMount } from 'svelte';
import { page } from '$app/stores';
2021-09-14 07:01:44 +03:00
import { dndzone } from 'svelte-dnd-action';
import TimeIcon from 'carbon-icons-svelte/lib/Time.svelte';
import EditIcon from 'carbon-icons-svelte/lib/Edit.svelte';
import SaveIcon from 'carbon-icons-svelte/lib/Save.svelte';
import PhoneIcon from 'carbon-icons-svelte/lib/Phone.svelte';
import TrashCanIcon from 'carbon-icons-svelte/lib/TrashCan.svelte';
import CheckmarkFilled24 from 'carbon-icons-svelte/lib/CheckmarkFilled.svelte';
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 PageBody from '$lib/components/PageBody/index.svelte';
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
import TextField from '$lib/components/Form/TextField.svelte';
import IconButton from '$lib/components/IconButton/index.svelte';
import Select from '$lib/components/Form/Select.svelte';
import { fetchCourse, updateLesson } from '$lib/utils/services/courses';
import CourseContainer from '$lib/components/CourseContainer/index.svelte';
import { profile } from '$lib/utils/store/user';
import {
lessons,
handleAddLesson,
2021-08-18 19:47:02 +03:00
handleSaveLesson,
handleDelete
} from '$lib/components/Course/components/Lesson/store/lessons';
import { course, setCourse, group } from '$lib/components/Course/store';
export let data;
const { courseId } = data;
let lessonEditing;
let ref;
2021-09-13 14:02:18 +03:00
let isStudent = true;
2021-09-14 07:01:44 +03:00
const flipDurationMs = 300;
function formatDate(date) {
const d = new Date(date);
2021-08-18 19:47:02 +03:00
const ye = new Intl.DateTimeFormat('en', { year: 'numeric' }).format(d);
const mo = new Intl.DateTimeFormat('en', { month: '2-digit' }).format(d);
const da = new Intl.DateTimeFormat('en', { day: '2-digit' }).format(d);
return `${ye}-${mo}-${da}`;
}
2021-08-18 19:47:02 +03:00
// TODO: CRUD of lessons
function addLesson() {
handleAddLesson();
setTimeout(() => {
lessonEditing = $lessons.length - 1;
if (ref) {
2021-08-18 19:47:02 +03:00
ref.scrollIntoView({
block: 'start',
behavior: 'smooth',
inline: 'nearest'
2021-08-18 19:47:02 +03:00
});
}
}, 100);
}
2021-08-18 19:47:02 +03:00
2021-09-14 07:01:44 +03:00
function handleDndConsider(e) {
$lessons = e.detail.items;
}
function handleDndFinalize(e) {
const prevLessonsByOrder = $lessons.reduce((acc, l) => ({ ...acc, [l.id]: l.order }), {});
2021-09-14 07:01:44 +03:00
$lessons = e.detail.items;
// Only update the lesson order that changed
e.detail.items.forEach((item, index) => {
const order = index + 1;
if (order !== prevLessonsByOrder[item.id]) {
$lessons[index].order = order;
updateLesson({ order }, item.id).then((update) =>
console.log(`updated lesson order`, update)
);
}
});
}
function getLessonOrder(id) {
const index = $lessons.findIndex((lesson) => lesson.id === id);
return index + 1;
}
2021-08-24 10:45:43 +03:00
onMount(async () => {
if ($course.id) return;
const { data: _data } = await fetchCourse(courseId);
setCourse(_data);
2021-08-18 19:47:02 +03:00
});
2021-09-13 14:02:18 +03:00
$: {
const user = $group.people.find((person) => person.profile_id === $profile.id);
2021-09-13 14:02:18 +03:00
if (user) {
isStudent = user.role_id === 3;
}
}
</script>
<CourseContainer bind:isStudent>
<PageNav title="Lessons">
2021-09-14 07:01:44 +03:00
<div slot="widget">
<RoleBasedSecurity allowedRoles={[1, 2]}>
<PrimaryButton label="Add" onClick={addLesson} isDisabled={!!lessonEditing} />
2021-09-14 07:01:44 +03:00
</RoleBasedSecurity>
</div>
</PageNav>
2021-09-14 07:01:44 +03:00
<PageBody padding="p-0">
<section
2021-09-17 06:33:49 +03:00
class="w-full lg:w-11/12 py-3 px-3 lg:px-4 m-auto lesson-list"
2021-09-14 07:01:44 +03:00
use:dndzone={{
items: $lessons,
flipDurationMs,
2021-09-14 07:18:01 +03:00
dragDisabled: isStudent,
2021-09-14 07:01:44 +03:00
dropTargetStyle: {
border: '2px #1d4ed8 solid',
'border-style': 'dashed'
}
2021-09-14 07:01:44 +03:00
}}
on:consider={handleDndConsider}
on:finalize={handleDndFinalize}
>
{#each $lessons as lesson (lesson.id)}
<div
bind:this={ref}
2023-07-31 17:27:04 +00:00
class="group relative m-auto rounded-md border-2 border-gray-100 py-3 px-5 mb-4 flex items-center hover:shadow-md shadow-xl transition delay-150 duration-300 ease-in-out dark:bg-gray-700"
>
<!-- Complete or Not complete icon -->
2021-09-13 14:02:18 +03:00
<div class="absolute -left-6 -top-6 success">
<IconButton
disabled={isStudent}
onClick={() => {
lesson.is_complete = !lesson.is_complete;
handleSaveLesson(lesson, $course.id);
}}
toolTipProps={isStudent
? {}
: {
title: `Click to ${lesson.is_complete ? 'lock' : 'unlock'}`,
direction: 'right'
2021-09-13 14:02:18 +03:00
}}
>
{#if lesson.is_complete}
<CheckmarkFilled24 class="carbon-icon" />
{:else}
<span class="text-2xl">🔒</span>
{/if}
</IconButton>
</div>
2021-09-11 21:52:46 +03:00
<RoleBasedSecurity allowedRoles={[1, 2]}>
2021-09-11 21:52:46 +03:00
<!-- Edit/Save -->
2021-09-17 06:33:49 +03:00
<div class="absolute -top-2 -right-2">
2021-09-14 07:01:44 +03:00
{#if lessonEditing === lesson.id}
2021-09-11 21:52:46 +03:00
<IconButton
onClick={() => {
lessonEditing = null;
handleSaveLesson(lesson, $course.id);
}}
>
<SaveIcon size={24} class="carbon-icon" />
2021-09-11 21:52:46 +03:00
</IconButton>
{:else}
2021-09-14 07:01:44 +03:00
<IconButton onClick={() => (lessonEditing = lesson.id)}>
<EditIcon size={24} class="carbon-icon" />
2021-09-11 21:52:46 +03:00
</IconButton>
{/if}
</div>
</RoleBasedSecurity>
<div class="">
<h3 class="dark:text-white text-3xl font-bold">
2021-09-14 07:01:44 +03:00
{getLessonOrder(lesson.id)}.
</h3>
</div>
<div class="ml-8 w-4/5">
2021-09-14 07:01:44 +03:00
{#if lessonEditing === lesson.id}
<TextField bind:value={lesson.title} autofocus={true} />
{:else}
<h3 class="dark:text-white text-2xl m-0 flex items-center">
2021-08-18 19:47:02 +03:00
<a
2021-09-13 14:02:18 +03:00
href={isStudent && !lesson.is_complete
? $page.url.pathname
2021-09-13 14:19:23 +03:00
: '/courses/' + $course.id + '/lessons/' + lesson.id}
2023-07-31 17:27:04 +00:00
class="dark:text-white hover:underline text-black {isStudent &&
2021-09-13 14:02:18 +03:00
!lesson.is_complete
? 'cursor-not-allowed'
: ''}"
2021-08-18 19:47:02 +03:00
>
{lesson.title}
</a>
</h3>
{/if}
2021-08-18 19:47:02 +03:00
<!-- <div class="flex items-center mb-2">
<div class="flex items-center mr-2">
2021-08-18 19:47:02 +03:00
{resources.map((r) => `${r.value} ${r.label}`).join(', ')}
</div>
2021-08-18 19:47:02 +03:00
</div> -->
2021-09-17 06:33:49 +03:00
<div
class="flex items-start justify-between flex-col lg:flex-row lg:items-center mt-6 w-full"
>
<div class="mb-3 lg:mb-0">
{#if lessonEditing === lesson.id}
<Select bind:value={lesson.profile} options={$group.tutors} labelKey="fullname" />
2021-09-17 06:33:49 +03:00
{:else if !lesson.profile}
<p class="dark:text-white ml-2 text-sm">No tutor added</p>
2021-09-17 06:33:49 +03:00
{:else}
<a href="." class="flex items-center hover:underline">
<img
alt="Placeholder"
class="block rounded-full"
width="24"
height="20"
src={lesson.profile.avatar_url}
/>
<p class="dark:text-white ml-2 text-sm">
2021-09-17 06:33:49 +03:00
{lesson.profile.fullname}
</p>
</a>
{/if}
</div>
2021-09-17 06:33:49 +03:00
<div class="flex items-center mb-3 lg:mb-0">
2021-09-14 07:01:44 +03:00
{#if lessonEditing === lesson.id}
<input
type="date"
name="lesson-date-picker"
class="p-2 rounded-md w-40"
2021-08-18 19:47:02 +03:00
value={formatDate(lesson.lesson_at)}
on:input={(e) => (lesson.lesson_at = e.target.value)}
/>
{:else}
<TimeIcon size={24} class="carbon-icon" />
<p class="dark:text-white text-md ml-2">
2021-08-18 19:47:02 +03:00
{new Date(lesson.lesson_at).toDateString()}
</p>
{/if}
</div>
2021-08-18 19:47:02 +03:00
2021-09-17 06:33:49 +03:00
<div class="flex items-center mb-3 lg:mb-0">
2021-09-14 07:01:44 +03:00
{#if lessonEditing === lesson.id}
2021-08-18 19:47:02 +03:00
<TextField
bind:value={lesson.call_url}
autofocus={true}
className="w-40"
placeholder="https://meet.google.com/mga-dsjs-fmb"
/>
{:else}
<PhoneIcon size={24} class="carbon-icon" />
<a class="text-md ml-2" href={lesson.call_url || '#'} target="_blank">
2021-08-18 19:47:02 +03:00
{lesson.call_url ? 'Join lesson' : 'No link'}
</a>
{/if}
</div>
</div>
</div>
2021-08-18 19:47:02 +03:00
<RoleBasedSecurity allowedRoles={[1, 2]}>
2021-09-11 21:52:46 +03:00
<div class="absolute bottom-2 right-0">
<IconButton onClick={handleDelete(lesson.id)}>
<TrashCanIcon size={24} class="carbon-icon" />
2021-09-11 21:52:46 +03:00
</IconButton>
</div>
</RoleBasedSecurity>
</div>
2021-08-18 19:47:02 +03:00
{:else}
<Box>
<div class="flex justify-between flex-col items-center w-96">
<img src="/images/empty-lesson-icon.svg" alt="Lesson" class="my-2.5 mx-auto" />
<h2 class="text-xl my-1.5 font-normal">No lessons yet</h2>
<p class="text-sm text-center text-slate-500">
Share your knowledge with the world by creating engaging lessons. Start by clicking on
the New Lesson button.
</p>
</div>
2021-08-18 19:47:02 +03:00
</Box>
{/each}
2021-09-14 07:01:44 +03:00
</section>
</PageBody>
</CourseContainer>
<style>
2021-09-14 07:01:44 +03:00
.lesson-list {
overflow-y: auto;
height: 84vh;
}
.group {
min-height: 170px;
}
</style>