2021-07-12 05:55:57 +03:00
|
|
|
<script>
|
2021-08-18 19:47:02 +03:00
|
|
|
import { onMount } from 'svelte';
|
2021-08-29 07:23:19 +03:00
|
|
|
import AudioConsole32 from 'carbon-icons-svelte/lib/AudioConsole32';
|
|
|
|
|
import Box from '../../../../Box/index.svelte';
|
2021-07-12 05:55:57 +03:00
|
|
|
import PrimaryButton from '../../../../PrimaryButton/index.svelte';
|
|
|
|
|
import PageBody from '../../../../PageBody/index.svelte';
|
2021-07-17 22:29:12 +03:00
|
|
|
import Modal from '../../../../Modal/index.svelte';
|
2021-07-12 05:55:57 +03:00
|
|
|
import TextField from '../../../../Form/TextField.svelte';
|
|
|
|
|
import Exercise from '../Exercise/index.svelte';
|
2021-08-18 19:47:02 +03:00
|
|
|
import { supabase } from '../../../../../utils/functions/supabase';
|
|
|
|
|
import { createExercise } from '../../../../../utils/services/courses';
|
|
|
|
|
import { QUESTION_TYPES } from '../../../../Question/constants';
|
2021-07-14 09:05:09 +03:00
|
|
|
import { lesson } from '../store/lessons';
|
2021-08-18 19:47:02 +03:00
|
|
|
import { questionnaire, isQuestionnaireFetching } from '../store/exercise';
|
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;
|
|
|
|
|
let newExercise = {
|
|
|
|
|
id: 1,
|
|
|
|
|
title: '',
|
2021-08-18 19:47:02 +03: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,
|
|
|
|
|
lesson_id: lessonId,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
console.log(`data, error `, data, error);
|
2021-07-14 09:05:09 +03:00
|
|
|
lesson.update((_lesson) => ({
|
|
|
|
|
..._lesson,
|
2021-08-18 19:47:02 +03: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 = {
|
2021-07-14 09:05:09 +03:00
|
|
|
id: $lesson.exercises.length + 1,
|
2021-07-12 05:55:57 +03:00
|
|
|
title: '',
|
2021-08-18 19:47:02 +03: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-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;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function getExercise(exerciseId) {
|
|
|
|
|
console.log('getExercise', exerciseId);
|
|
|
|
|
|
|
|
|
|
if (!exerciseId) return;
|
|
|
|
|
|
|
|
|
|
isQuestionnaireFetching.update(() => true);
|
|
|
|
|
|
|
|
|
|
const { data } = await supabase
|
|
|
|
|
.from('exercise')
|
|
|
|
|
.select(
|
|
|
|
|
`
|
|
|
|
|
id, title, description,
|
|
|
|
|
questions:question(
|
|
|
|
|
*,
|
|
|
|
|
options:option(*),
|
|
|
|
|
question_type:question_type_id(id, label)
|
|
|
|
|
)
|
|
|
|
|
`
|
|
|
|
|
)
|
|
|
|
|
.eq('id', exerciseId)
|
|
|
|
|
.single();
|
|
|
|
|
|
|
|
|
|
// 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.is_title_dirty = false;
|
|
|
|
|
data.is_description_dirty = false;
|
|
|
|
|
|
|
|
|
|
questionnaire.set(data);
|
|
|
|
|
console.log('getExercise', exerciseId, $questionnaire);
|
|
|
|
|
|
|
|
|
|
isQuestionnaireFetching.update(() => false);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
|
getExercises();
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
$: getExercise(exerciseId);
|
2021-07-12 05:55:57 +03:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
{#if exerciseId}
|
|
|
|
|
<Exercise {exerciseId} />
|
|
|
|
|
{:else}
|
|
|
|
|
<Modal
|
2021-07-17 22:29:12 +03:00
|
|
|
onClose={handleCancelAddExercise}
|
2021-07-12 05:55:57 +03:00
|
|
|
bind:open
|
|
|
|
|
modalHeading="Create an Exercise"
|
2021-08-18 19:47:02 +03:00
|
|
|
width="w-80"
|
2021-07-12 05:55:57 +03: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">
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
className="px-6 py-2"
|
|
|
|
|
label="Submit"
|
|
|
|
|
onClick={handleAddExercise}
|
|
|
|
|
/>
|
|
|
|
|
</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">
|
|
|
|
|
<PrimaryButton
|
|
|
|
|
className="mr-2"
|
|
|
|
|
label="Add"
|
|
|
|
|
onClick={() => (open = !open)}
|
|
|
|
|
/>
|
|
|
|
|
</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
|
|
|
|
|
class="w-52 bg-gray-100 px-4 py-7 mr-4 mb-4"
|
|
|
|
|
href="{path}/{exercise.id}"
|
|
|
|
|
>
|
2021-07-12 05:55:57 +03:00
|
|
|
<h3 class="text-xl">{exercise.title}</h3>
|
|
|
|
|
<p class="mt-4 text-sm">Created Jul 3, 2021</p>
|
|
|
|
|
</a>
|
2021-08-29 07:23:19 +03:00
|
|
|
{:else}
|
|
|
|
|
<Box>
|
|
|
|
|
<AudioConsole32 class="carbon-icon w-80" />
|
|
|
|
|
<h3 class="text-3xl text-gray-500">No Exercise Added</h3>
|
|
|
|
|
</Box>
|
2021-07-12 05:55:57 +03:00
|
|
|
{/each}
|
|
|
|
|
</div>
|
|
|
|
|
</PageBody>
|
|
|
|
|
{/if}
|