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';
|
|
|
|
|
import { addGroupMember } from '$lib/utils/services/courses';
|
|
|
|
|
import { supabase } from '$lib/utils/functions/supabase';
|
|
|
|
|
import { profile } from '$lib/utils/store/user';
|
|
|
|
|
import { currentOrg } from '$lib/utils/store/org';
|
2021-12-30 22:30:30 +02:00
|
|
|
|
|
|
|
|
let errors = {
|
2023-08-07 06:18:30 +00:00
|
|
|
emails: ''
|
2021-12-30 22:30:30 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
async function createCourse(e) {
|
2023-08-07 06:18:30 +00:00
|
|
|
const { hasError, fieldErrors, students, tutors } = 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);
|
|
|
|
|
const { id: group_id } = newGroup[0];
|
|
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
|
|
courses.update((_courses) => [..._courses, newCourse[0]]);
|
|
|
|
|
|
|
|
|
|
// 3. Add group members
|
|
|
|
|
|
|
|
|
|
// Get profile of author and add as member
|
|
|
|
|
await addGroupMember({
|
|
|
|
|
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
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let membersStack = [];
|
|
|
|
|
|
|
|
|
|
// Create groupmemebers from group_id and add teachers and students
|
|
|
|
|
for (const student of students) {
|
|
|
|
|
membersStack.push({
|
|
|
|
|
email: student,
|
|
|
|
|
group_id,
|
2023-08-07 06:18:30 +00:00
|
|
|
role_id: ROLE.STUDENT
|
2021-12-30 22:30:30 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const tutor of tutors) {
|
|
|
|
|
membersStack.push({
|
|
|
|
|
email: tutor,
|
|
|
|
|
group_id,
|
2023-08-07 06:18:30 +00:00
|
|
|
role_id: ROLE.TUTOR
|
2021-12-30 22:30:30 +02:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addGroupMember(membersStack).then((membersAdded) => {
|
|
|
|
|
console.log(`membersAdded`, membersAdded);
|
|
|
|
|
$createCourseModal.open = false;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
<svelte:head>
|
|
|
|
|
<title>Courses - ClassroomIO</title>
|
|
|
|
|
</svelte:head>
|
|
|
|
|
|
|
|
|
|
<Modal
|
|
|
|
|
onClose={() => ($createCourseModal.open = false)}
|
|
|
|
|
bind:open={$createCourseModal.open}
|
|
|
|
|
width="w-2/5"
|
|
|
|
|
modalHeading="Create a Course"
|
|
|
|
|
>
|
|
|
|
|
<form on:submit|preventDefault={createCourse}>
|
|
|
|
|
<TextField
|
|
|
|
|
label="Course name"
|
|
|
|
|
bind:value={$createCourseModal.title}
|
2023-08-07 06:18:30 +00:00
|
|
|
autoFocus={true}
|
2021-12-30 22:30:30 +02:00
|
|
|
placeholder="Your course name"
|
|
|
|
|
className="mb-4"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
errorMessage={errors.title}
|
|
|
|
|
autoComplete={false}
|
|
|
|
|
/>
|
|
|
|
|
<TextArea
|
|
|
|
|
label="Invite Tutors"
|
|
|
|
|
bind:value={$createCourseModal.tutors}
|
|
|
|
|
rows="2"
|
|
|
|
|
maxRows="3"
|
|
|
|
|
placeholder="tutor@company.com, john@gmail.com"
|
|
|
|
|
helperMessage="To invite people, add their emails separated by a comma"
|
|
|
|
|
errorMessage={errors.tutors}
|
|
|
|
|
className="mb-4"
|
|
|
|
|
/>
|
|
|
|
|
<TextArea
|
|
|
|
|
label="Invite Students"
|
|
|
|
|
bind:value={$createCourseModal.students}
|
|
|
|
|
rows="2"
|
|
|
|
|
maxRows="3"
|
|
|
|
|
placeholder="student@gmail.com, john@gmail.com"
|
|
|
|
|
helperMessage="To invite people, add their emails separated by a comma"
|
|
|
|
|
errorMessage={errors.students}
|
|
|
|
|
className="mb-4"
|
|
|
|
|
/>
|
|
|
|
|
<TextArea
|
|
|
|
|
label="Short description"
|
|
|
|
|
bind:value={$createCourseModal.description}
|
|
|
|
|
rows="4"
|
|
|
|
|
maxRows="4"
|
|
|
|
|
placeholder="A little description about this course"
|
|
|
|
|
className="mb-4"
|
|
|
|
|
isRequired={true}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<div class="mt-5 flex items-center">
|
2022-12-30 13:08:54 +01:00
|
|
|
<PrimaryButton className="px-6 py-3" label="Finish" type="submit" />
|
2021-12-30 22:30:30 +02:00
|
|
|
</div>
|
|
|
|
|
</form>
|
|
|
|
|
</Modal>
|