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

72 lines
1.7 KiB
JavaScript
Raw Normal View History

2021-08-18 19:47:02 +03:00
import { writable } from 'svelte/store';
import { lessons } from './components/Lesson/store/lessons';
import { ROLE } from '../../utils/constants/roles';
export const course = writable({});
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-24 10:45:43 +03:00
export async function setCourseData(data, 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
if (Array.isArray(groupData.members)) {
for (const member of groupData.members) {
if (member.role_id === ROLE.STUDENT) {
groupData.students.push(member);
2021-08-26 09:34:18 +03:00
} else if (member.profile) {
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
groupData.people = groupData.members;
2021-08-18 19:47:02 +03:00
}
delete groupData.members;
group.set(groupData);
}
2021-08-24 10:45:43 +03:00
if (setLesson) {
const orderedLessons = (data.lessons || []).sort(
(a, b) => new Date(a.created_at) - new Date(b.created_at)
);
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
delete data.lessons;
course.set(data);
}