2021-12-30 22:30:30 +02:00
|
|
|
<script>
|
2023-08-02 19:35:27 +01:00
|
|
|
import PrimaryButton from '$lib/components/PrimaryButton/index.svelte';
|
|
|
|
|
import Modal from '$lib/components/Modal/index.svelte';
|
|
|
|
|
import TextField from '$lib/components/Form/TextField.svelte';
|
|
|
|
|
import TextArea from '$lib/components/Form/TextArea.svelte';
|
|
|
|
|
import { courses, createCourseModal } from '$lib/components/Courses/store';
|
|
|
|
|
import { validateForm } from '$lib/components/Courses/functions';
|
|
|
|
|
import { ROLE } from '$lib/utils/constants/roles';
|
2024-02-28 03:00:31 +01:00
|
|
|
import { addGroupMember, addDefaultNewsFeed } from '$lib/utils/services/courses';
|
2023-08-02 19:35:27 +01:00
|
|
|
import { supabase } from '$lib/utils/functions/supabase';
|
|
|
|
|
import { profile } from '$lib/utils/store/user';
|
|
|
|
|
import { currentOrg } from '$lib/utils/store/org';
|
2023-10-03 12:29:06 +00:00
|
|
|
import { goto } from '$app/navigation';
|
2023-10-30 06:04:53 +00:00
|
|
|
import { capturePosthogEvent } from '$lib/utils/services/posthog';
|
2021-12-30 22:30:30 +02:00
|
|
|
|
2023-10-03 12:29:06 +00:00
|
|
|
let isLoading = false;
|
2021-12-30 22:30:30 +02:00
|
|
|
let errors = {
|
2023-09-18 17:03:56 +00:00
|
|
|
title: '',
|
|
|
|
|
description: ''
|
2021-12-30 22:30:30 +02:00
|
|
|
};
|
|
|
|
|
|
2023-09-24 07:08:37 +00:00
|
|
|
let userPrompt = '';
|
|
|
|
|
|
|
|
|
|
function onClose() {
|
|
|
|
|
createCourseModal.update(() => ({
|
|
|
|
|
open: false,
|
|
|
|
|
title: '',
|
|
|
|
|
description: '',
|
|
|
|
|
emails: '',
|
|
|
|
|
tutors: '',
|
|
|
|
|
students: ''
|
|
|
|
|
}));
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-30 06:04:53 +00:00
|
|
|
async function createCourse() {
|
2023-10-03 12:29:06 +00:00
|
|
|
isLoading = true;
|
2023-09-18 17:03:56 +00:00
|
|
|
const { hasError, fieldErrors } = validateForm($createCourseModal);
|
2021-12-30 22:30:30 +02:00
|
|
|
|
|
|
|
|
errors = fieldErrors;
|
|
|
|
|
if (hasError) return;
|
|
|
|
|
|
|
|
|
|
const { title, description } = $createCourseModal;
|
|
|
|
|
// 1. Create group
|
|
|
|
|
const { data: newGroup } = await supabase
|
|
|
|
|
.from('group')
|
2023-08-02 19:35:27 +01:00
|
|
|
.insert({ name: title, description, organization_id: $currentOrg.id })
|
|
|
|
|
.select();
|
2021-12-30 22:30:30 +02:00
|
|
|
|
|
|
|
|
console.log(`newGroup`, newGroup);
|
2023-10-30 06:04:53 +00:00
|
|
|
|
|
|
|
|
if (!newGroup) return;
|
|
|
|
|
|
2021-12-30 22:30:30 +02:00
|
|
|
const { id: group_id } = newGroup[0];
|
|
|
|
|
|
2023-09-24 07:08:37 +00:00
|
|
|
// Set userPrompt to the description
|
|
|
|
|
userPrompt = description;
|
|
|
|
|
|
2021-12-30 22:30:30 +02:00
|
|
|
// 2. Create course with group_id
|
2023-08-02 19:35:27 +01:00
|
|
|
const { data: newCourse } = await supabase
|
|
|
|
|
.from('course')
|
|
|
|
|
.insert({
|
|
|
|
|
title,
|
|
|
|
|
description,
|
2023-08-07 06:18:30 +00:00
|
|
|
group_id
|
2023-08-02 19:35:27 +01:00
|
|
|
})
|
|
|
|
|
.select();
|
2021-12-30 22:30:30 +02:00
|
|
|
console.log(`newCourse`, newCourse);
|
|
|
|
|
|
2023-10-30 06:04:53 +00:00
|
|
|
if (!newCourse) return;
|
|
|
|
|
|
2021-12-30 22:30:30 +02:00
|
|
|
courses.update((_courses) => [..._courses, newCourse[0]]);
|
|
|
|
|
|
2023-10-30 06:04:53 +00:00
|
|
|
capturePosthogEvent('course_created', {
|
|
|
|
|
course_id: newCourse[0]?.id,
|
|
|
|
|
course_title: newCourse[0]?.title,
|
|
|
|
|
course_description: newCourse[0]?.description,
|
|
|
|
|
organization_id: $currentOrg.id,
|
|
|
|
|
organization_name: $currentOrg.name,
|
|
|
|
|
user_id: $profile.id,
|
|
|
|
|
user_email: $profile.email
|
|
|
|
|
});
|
2021-12-30 22:30:30 +02:00
|
|
|
|
2023-10-30 06:04:53 +00:00
|
|
|
// 3. Add group members
|
2024-02-28 03:00:31 +01:00
|
|
|
const { data } = await addGroupMember({
|
2021-12-30 22:30:30 +02:00
|
|
|
profile_id: $profile.id,
|
|
|
|
|
email: $profile.email,
|
|
|
|
|
group_id,
|
2023-08-07 06:18:30 +00:00
|
|
|
role_id: ROLE.TUTOR
|
2021-12-30 22:30:30 +02:00
|
|
|
});
|
2024-02-28 03:00:31 +01:00
|
|
|
|
|
|
|
|
// 4. Add default news feed.
|
|
|
|
|
if (Array.isArray(data) && data.length) {
|
|
|
|
|
const { id: authorId } = data[0];
|
|
|
|
|
console.log('Add news feed into course');
|
|
|
|
|
|
|
|
|
|
await addDefaultNewsFeed({
|
|
|
|
|
content: `<h2>Welcome to this course 🎉 </h2>
|
|
|
|
|
<p>Thank you for joining this course and I hope you get the best out of it.</p>`,
|
|
|
|
|
course_id: newCourse[0]?.id,
|
|
|
|
|
is_pinned: true,
|
|
|
|
|
author_id: authorId
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-03 12:29:06 +00:00
|
|
|
if (newCourse[0] != null && newCourse[0].id) {
|
|
|
|
|
goto(`/courses/${newCourse[0]?.id}`);
|
|
|
|
|
}
|
2023-09-24 07:08:37 +00:00
|
|
|
onClose();
|
2023-10-03 12:29:06 +00:00
|
|
|
isLoading = false;
|
2021-12-30 22:30:30 +02:00
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<svelte:head>
|
2023-08-17 14:05:01 +01:00
|
|
|
<title>Create a new course</title>
|
2021-12-30 22:30:30 +02:00
|
|
|
</svelte:head>
|
|
|
|
|
|
|
|
|
|
<Modal
|
2023-09-24 07:08:37 +00:00
|
|
|
{onClose}
|
2021-12-30 22:30:30 +02:00
|
|
|
bind:open={$createCourseModal.open}
|
2023-09-08 09:17:05 +00:00
|
|
|
width="w-4/5 md:w-2/5"
|
2021-12-30 22:30:30 +02:00
|
|
|
modalHeading="Create a Course"
|
|
|
|
|
>
|
|
|
|
|
<form on:submit|preventDefault={createCourse}>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Course name"
|
|
|
|
|
bind:value={$createCourseModal.title}
|
|
|
|
|
placeholder="Your course name"
|
|
|
|
|
className="mb-4"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
errorMessage={errors.title}
|
|
|
|
|
autoComplete={false}
|
|
|
|
|
/>
|
2023-09-24 07:08:37 +00:00
|
|
|
|
2021-12-30 22:30:30 +02:00
|
|
|
<TextArea
|
|
|
|
|
label="Short description"
|
|
|
|
|
bind:value={$createCourseModal.description}
|
2023-10-30 06:04:53 +00:00
|
|
|
rows={4}
|
2021-12-30 22:30:30 +02:00
|
|
|
placeholder="A little description about this course"
|
|
|
|
|
className="mb-4"
|
|
|
|
|
isRequired={true}
|
2023-09-18 17:03:56 +00:00
|
|
|
errorMessage={errors.description}
|
2023-09-24 07:08:37 +00:00
|
|
|
isAIEnabled={true}
|
|
|
|
|
initAIPrompt="Write a 30 word description for a course titled: {$createCourseModal.title}"
|
2021-12-30 22:30:30 +02:00
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div class="mt-5 flex items-center">
|
2023-10-03 12:29:06 +00:00
|
|
|
<PrimaryButton
|
|
|
|
|
className="px-6 py-3"
|
|
|
|
|
label="Finish"
|
|
|
|
|
type="submit"
|
|
|
|
|
isDisabled={isLoading}
|
|
|
|
|
{isLoading}
|
|
|
|
|
/>
|
2021-12-30 22:30:30 +02:00
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Modal>
|