Files
classroomio/src/components/Course/store.ts
T

139 lines
3.0 KiB
TypeScript
Raw Normal View History

import { writable, Writable } from 'svelte/store';
2021-08-18 19:47:02 +03:00
import { lessons } from './components/Lesson/store/lessons';
//@ts-ignore
2021-08-18 19:47:02 +03:00
import { ROLE } from '../../utils/constants/roles';
import type { Course } from '../../utils/types';
2021-08-18 19:47:02 +03:00
export const course: Writable<Course> = writable({
id: '',
title: '',
description: '',
cost: 0,
currency: '',
is_certificate_downloadable: false,
certificate_theme: 'professional',
metadata: {
requirements: '',
description: '',
goals: '',
videoUrl: '',
showDiscount: false,
discount: 0,
reward: {
show: false,
description: '',
},
instructor: {
name: '',
role: '',
coursesNo: 0,
description: '',
imgUrl: '',
},
},
});
2021-08-18 19:47:02 +03:00
export const mockGroupMember = {
id: '434534534535',
group_id: '434534534535',
role_id: 1,
profile_id: 'vcvbcvbcvbb----434534534535',
email: null,
profile: {
id: 'vcvbcvbcvbb----434534534535',
fullname: 'Rotimi Best',
username: 'robertblake',
avatar_url: '',
created_at: '2021-08-08T13:42:13+00:00',
updated_at: '2021-08-08T13:42:13+00:00',
},
};
export const group = writable({
id: null,
tutors: [],
students: [],
2021-08-29 07:23:19 +03:00
people: [],
2021-08-18 19:47:02 +03:00
});
export async function setCourse(data: Course, setLesson = true) {
2021-08-18 19:47:02 +03:00
if (!data || !(Object.values(data) && Object.values(data).length)) return;
2021-08-26 09:34:18 +03:00
// const tutorsById = {};
2021-08-18 19:47:02 +03:00
if (data.group) {
2021-08-26 09:34:18 +03:00
const groupData = Object.assign(data.group, {
tutors: [],
students: [],
people: [],
});
2021-08-18 19:47:02 +03:00
// @ts-ignore
2021-08-18 19:47:02 +03:00
if (Array.isArray(groupData.members)) {
// @ts-ignore
2021-08-18 19:47:02 +03:00
for (const member of groupData.members) {
if (member.role_id === ROLE.STUDENT) {
// @ts-ignore
2021-08-18 19:47:02 +03:00
groupData.students.push(member);
2021-08-26 09:34:18 +03:00
} else if (member.profile) {
// @ts-ignore
2021-08-26 09:34:18 +03:00
groupData.tutors.push(member.profile);
// tutorsById[member.profile.id] = member.profile;
2021-08-18 19:47:02 +03:00
}
}
2021-08-26 09:34:18 +03:00
// @ts-ignore
2021-08-26 09:34:18 +03:00
groupData.people = groupData.members;
2021-08-18 19:47:02 +03:00
}
// @ts-ignore
2021-08-18 19:47:02 +03:00
delete groupData.members;
// @ts-ignore
2021-08-18 19:47:02 +03:00
group.set(groupData);
}
2021-08-24 10:45:43 +03:00
if (setLesson) {
// @ts-ignore
2021-09-14 07:01:44 +03:00
const orderedLessons = (data.lessons || [])
// @ts-ignore
2021-09-14 07:01:44 +03:00
.sort((a, b) => new Date(a.created_at) - new Date(b.created_at))
// @ts-ignore
2021-09-14 07:01:44 +03:00
.sort((a, b) => a.order - b.order);
2021-08-26 09:34:18 +03:00
// .map((lesson) => ({
// ...lesson,
// profile: lesson.profile && tutorsById[lesson.profile.id],
// }));
2021-08-24 10:45:43 +03:00
lessons.set(orderedLessons);
}
2021-08-18 19:47:02 +03:00
//@ts-ignore
2021-08-18 19:47:02 +03:00
delete data.lessons;
if (data.metadata && !Object.values(data.metadata)) {
data.metadata = {
requirements: '',
description: '',
goals: '',
videoUrl: '',
showDiscount: false,
discount: 0,
reward: {
show: false,
description: '',
},
instructor: {
name: '',
role: '',
coursesNo: 0,
description: '',
imgUrl: '',
},
};
}
if (!data.certificate_theme) {
data.certificate_theme = 'professional';
}
2021-08-18 19:47:02 +03:00
course.set(data);
}