2021-07-12 05:55:57 +03:00
|
|
|
<script>
|
2023-08-02 19:35:27 +01:00
|
|
|
import { goto } from '$app/navigation';
|
2021-08-18 19:47:02 +03:00
|
|
|
import { onMount } from 'svelte';
|
2023-08-02 19:35:27 +01:00
|
|
|
import Box from '$lib/components/Box/index.svelte';
|
2022-12-30 13:08:54 +01:00
|
|
|
import { Moon } from 'svelte-loading-spinners';
|
2023-08-02 19:35:27 +01:00
|
|
|
import Backdrop from '$lib/components/Backdrop/index.svelte';
|
|
|
|
|
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
|
|
|
|
import RoleBasedSecurity from '$lib/components/RoleBasedSecurity/index.svelte';
|
|
|
|
|
import PageBody from '$lib/components/PageBody/index.svelte';
|
|
|
|
|
import Modal from '$lib/components/Modal/index.svelte';
|
|
|
|
|
import TextField from '$lib/components/Form/TextField.svelte';
|
2021-07-12 05:55:57 +03:00
|
|
|
import Exercise from '../Exercise/index.svelte';
|
2023-08-02 19:35:27 +01:00
|
|
|
import { supabase } from '$lib/utils/functions/supabase';
|
|
|
|
|
import { createExercise } from '$lib/utils/services/courses';
|
|
|
|
|
import { QUESTION_TYPES } from '$lib/components/Question/constants';
|
2021-07-14 09:05:09 +03:00
|
|
|
import { lesson } from '../store/lessons';
|
2023-08-30 14:31:40 +00:00
|
|
|
import { questionnaire, isQuestionnaireFetching, handleAddQuestion } from '../store/exercise';
|
|
|
|
|
import { exerciseMode } from '../Exercise/store';
|
2021-07-14 09:05:09 +03:00
|
|
|
|
2021-07-12 05:55:57 +03:00
|
|
|
export let path;
|
|
|
|
|
export let exerciseId;
|
2021-08-18 19:47:02 +03:00
|
|
|
export let lessonId;
|
2021-07-12 05:55:57 +03:00
|
|
|
|
|
|
|
|
let open = false;
|
2021-12-30 22:30:30 +02:00
|
|
|
let isFetching = false;
|
2021-07-12 05:55:57 +03:00
|
|
|
let newExercise = {
|
|
|
|
|
id: 1,
|
|
|
|
|
title: '',
|
2023-08-02 21:17:17 +00:00
|
|
|
description: ''
|
2021-07-12 05:55:57 +03:00
|
|
|
};
|
|
|
|
|
|
2021-08-18 19:47:02 +03:00
|
|
|
async function handleAddExercise() {
|
|
|
|
|
const { data, error } = await createExercise({
|
|
|
|
|
title: newExercise.title,
|
2023-08-02 21:17:17 +00:00
|
|
|
lesson_id: lessonId
|
2021-08-18 19:47:02 +03:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`data, error `, data, error);
|
2021-07-14 09:05:09 +03:00
|
|
|
lesson.update((_lesson) => ({
|
|
|
|
|
..._lesson,
|
2023-08-02 21:17:17 +00:00
|
|
|
exercises: [..._lesson.exercises, data[0]]
|
2021-07-14 09:05:09 +03:00
|
|
|
}));
|
2021-07-12 05:55:57 +03:00
|
|
|
|
|
|
|
|
handleCancelAddExercise();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function handleCancelAddExercise() {
|
|
|
|
|
open = false;
|
|
|
|
|
newExercise = {
|
2023-08-30 14:31:40 +00:00
|
|
|
id: new Date().getTime(),
|
2021-07-12 05:55:57 +03:00
|
|
|
title: '',
|
2023-08-02 21:17:17 +00:00
|
|
|
description: ''
|
2021-07-12 05:55:57 +03:00
|
|
|
};
|
|
|
|
|
}
|
2021-08-18 19:47:02 +03:00
|
|
|
|
|
|
|
|
async function getExercises() {
|
2021-08-24 10:45:43 +03:00
|
|
|
if (!lessonId) return;
|
2021-12-30 22:30:30 +02:00
|
|
|
isFetching = true;
|
2021-08-18 19:47:02 +03:00
|
|
|
const exercisesData = await supabase
|
|
|
|
|
.from('exercise')
|
|
|
|
|
.select(`id, title, created_at`)
|
|
|
|
|
.match({ lesson_id: lessonId });
|
|
|
|
|
|
|
|
|
|
$lesson.exercises = exercisesData.data;
|
2021-12-30 22:30:30 +02:00
|
|
|
isFetching = false;
|
2021-08-18 19:47:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getExercise(exerciseId) {
|
|
|
|
|
if (!exerciseId) return;
|
2021-12-30 22:59:09 +02:00
|
|
|
isFetching = true;
|
2021-08-18 19:47:02 +03:00
|
|
|
isQuestionnaireFetching.update(() => true);
|
|
|
|
|
|
2023-08-02 19:35:27 +01:00
|
|
|
let { data, error } = await supabase
|
2021-08-18 19:47:02 +03:00
|
|
|
.from('exercise')
|
|
|
|
|
.select(
|
|
|
|
|
`
|
2021-09-23 16:39:38 +03:00
|
|
|
id, title, description, due_by,
|
2021-08-18 19:47:02 +03:00
|
|
|
questions:question(
|
|
|
|
|
*,
|
|
|
|
|
options:option(*),
|
|
|
|
|
question_type:question_type_id(id, label)
|
|
|
|
|
)
|
|
|
|
|
`
|
|
|
|
|
)
|
|
|
|
|
.eq('id', exerciseId)
|
|
|
|
|
.single();
|
|
|
|
|
|
2023-08-02 19:35:27 +01:00
|
|
|
if (error) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-10-18 21:33:31 +03:00
|
|
|
if (data && Array.isArray(data.questions)) {
|
|
|
|
|
// Need to set the question type inorder for the select in the questionnaire builder to match
|
|
|
|
|
data.questions.forEach((question) => {
|
|
|
|
|
question.question_type = QUESTION_TYPES.find(
|
|
|
|
|
(type) => type.id === question.question_type.id
|
|
|
|
|
);
|
|
|
|
|
return question;
|
|
|
|
|
});
|
|
|
|
|
data.questions = data.questions.sort((a, b) => a.order - b.order);
|
2023-08-02 19:35:27 +01:00
|
|
|
} else if (data) {
|
2021-10-18 21:33:31 +03:00
|
|
|
data.questions = [];
|
2023-08-02 19:35:27 +01:00
|
|
|
} else {
|
|
|
|
|
data = {};
|
2021-10-18 21:33:31 +03:00
|
|
|
}
|
2021-08-18 19:47:02 +03:00
|
|
|
|
|
|
|
|
data.is_title_dirty = false;
|
|
|
|
|
data.is_description_dirty = false;
|
2021-09-23 16:39:38 +03:00
|
|
|
data.is_due_by_dirty = false;
|
2021-08-18 19:47:02 +03:00
|
|
|
|
|
|
|
|
questionnaire.set(data);
|
|
|
|
|
console.log('getExercise', exerciseId, $questionnaire);
|
|
|
|
|
|
|
|
|
|
isQuestionnaireFetching.update(() => false);
|
2021-12-30 22:59:09 +02:00
|
|
|
isFetching = false;
|
2021-08-18 19:47:02 +03:00
|
|
|
}
|
|
|
|
|
|
2021-10-18 21:33:31 +03:00
|
|
|
function goBack() {
|
|
|
|
|
goto(path);
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-18 19:47:02 +03:00
|
|
|
onMount(() => {
|
|
|
|
|
getExercises();
|
|
|
|
|
});
|
|
|
|
|
|
2023-08-30 14:31:40 +00:00
|
|
|
const gotoEdit = () => {
|
|
|
|
|
$exerciseMode.editMode = true;
|
|
|
|
|
};
|
|
|
|
|
|
2021-08-18 19:47:02 +03:00
|
|
|
$: getExercise(exerciseId);
|
2021-07-12 05:55:57 +03:00
|
|
|
</script>
|
|
|
|
|
|
2021-12-30 22:30:30 +02:00
|
|
|
{#if isFetching}
|
|
|
|
|
<Backdrop>
|
2022-12-30 13:08:54 +01:00
|
|
|
<Moon size="60" color="#1d4ed8" unit="px" duration="1s" />
|
2021-12-30 22:30:30 +02:00
|
|
|
</Backdrop>
|
|
|
|
|
{/if}
|
|
|
|
|
|
2021-07-12 05:55:57 +03:00
|
|
|
{#if exerciseId}
|
2023-09-01 15:28:47 +01:00
|
|
|
<Exercise {exerciseId} {goBack} />
|
2021-07-12 05:55:57 +03:00
|
|
|
{:else}
|
2023-08-02 21:17:17 +00:00
|
|
|
<Modal onClose={handleCancelAddExercise} bind:open modalHeading="Create an Exercise" width="w-80">
|
2023-09-01 15:28:47 +01:00
|
|
|
<TextField bind:value={newExercise.title} autoFocus={true} placeholder="Exercise name" />
|
2021-07-17 22:29:12 +03:00
|
|
|
|
|
|
|
|
<div class="mt-5 flex items-center">
|
2023-08-02 21:17:17 +00:00
|
|
|
<PrimaryButton className="px-6 py-3" label="Submit" onClick={handleAddExercise} />
|
2021-07-17 22:29:12 +03:00
|
|
|
</div>
|
2021-07-12 05:55:57 +03:00
|
|
|
</Modal>
|
|
|
|
|
|
2021-08-24 10:45:43 +03:00
|
|
|
<PageBody>
|
2021-07-12 05:55:57 +03:00
|
|
|
<slot:fragment slot="header">
|
2023-08-02 19:35:27 +01:00
|
|
|
<RoleBasedSecurity allowedRoles={[1, 2]}>
|
2023-08-30 14:31:40 +00:00
|
|
|
<PrimaryButton className="mr-2 mb-2" label="Add" onClick={() => (open = !open)} />
|
2021-09-11 21:52:46 +03:00
|
|
|
</RoleBasedSecurity>
|
2021-07-12 05:55:57 +03:00
|
|
|
</slot:fragment>
|
|
|
|
|
|
|
|
|
|
<div class="flex flex-wrap">
|
2021-07-14 09:05:09 +03:00
|
|
|
{#each $lesson.exercises as exercise}
|
2021-07-18 19:12:39 +03:00
|
|
|
<a
|
2023-09-16 22:44:20 +01:00
|
|
|
class="w-52 bg-gray-100 dark:bg-neutral-800 px-4 py-7 mr-4 mb-4 rounded-lg"
|
2021-07-18 19:12:39 +03:00
|
|
|
href="{path}/{exercise.id}"
|
2023-08-30 14:31:40 +00:00
|
|
|
on:click={gotoEdit}
|
2021-07-18 19:12:39 +03:00
|
|
|
>
|
2023-05-05 20:19:45 +00:00
|
|
|
<h3 class="dark:text-white text-xl">{exercise.title}</h3>
|
|
|
|
|
<p class="dark:text-white mt-4 text-sm">Created Jul 3, 2021</p>
|
2021-07-12 05:55:57 +03:00
|
|
|
</a>
|
2021-08-29 07:23:19 +03:00
|
|
|
{:else}
|
2023-09-14 20:59:51 +00:00
|
|
|
<Box className="mt-3 text-center">
|
|
|
|
|
<div class="flex justify-between flex-col items-center w-[80%] md:w-96">
|
2023-08-02 21:17:17 +00:00
|
|
|
<img src="/images/empty-exercise-icon.svg" alt="Exercise" class="my-2.5 mx-auto" />
|
|
|
|
|
<h2 class="text-xl my-1.5 font-normal">No exercises added for this lesson</h2>
|
|
|
|
|
<p class="text-sm text-center text-slate-500">
|
|
|
|
|
Share your knowledge with the world by creating engaging exercises. Add an exercise
|
|
|
|
|
now.
|
|
|
|
|
</p>
|
|
|
|
|
</div>
|
2021-08-29 07:23:19 +03:00
|
|
|
</Box>
|
2021-07-12 05:55:57 +03:00
|
|
|
{/each}
|
|
|
|
|
</div>
|
|
|
|
|
</PageBody>
|
|
|
|
|
{/if}
|