Skip to content

Instantly share code, notes, and snippets.

@reharik
Created May 17, 2016 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reharik/dedfd1a40b245429d2d84369657de5ae to your computer and use it in GitHub Desktop.
Save reharik/dedfd1a40b245429d2d84369657de5ae to your computer and use it in GitHub Desktop.
schema
courses:{
1:{
lastUpdated: '',
id: 1,
courseTitle: 'course title 1',
active: true,
chapters: [1]
},
2:{
lastUpdated: '',
id: 2,
courseTitle: 'course title2',
active: true,
chapters: [2]
}
},
chapters: {
1: {
isExpanded: false,
id: 1,
title: "chapter 1",
assignments: [1]
},
2: {
isExpanded: false,
id: 2,
title: "chapter 2",
assignments: [2]
}
},
assignments: {
1: {
id: 1,
name: 'item1',
type: 'Assesment'
},
2: {
id: 2,
name: item s,
type: 'Assesment'
}
}
import { normalize, Schema, arrayOf } from 'normalizr';
const course = new Schema('courses');
const chapter = new Schema('chapters');
const assignment = new Schema('assignments');
course.define({
chapters: arrayOf(chapter)
});
chapter.define({
assignments: arrayOf(assignment)
});
function getChaptersForCourse(state, courseId){
return state.courses[courseId].chapters.map(c=> getChapterById(state,c));
}
function getChapterById(state, id){
return object.assign({}, state.chapters[id]);
}
function getAssignmentsForChapter(state, chapterId){
return state.chapters[chapterId].assignments.map(a => getAssignmentsById(state, a));
}
function getAssignmentsById(state, id){
return state.Assignments[id];
}
function getCurrentCourse(state){
var course = object.assign({}, state.courses[state.currentCourse]);
course.chapters = object.assign({},course.chapters.map(c=>getChaptersForCourse(c)));
course.chapters.map(c=> c.assignments = getAssignmentForChapter(c));
return course;
}
export {
getCurrentCourse,
course,
chapter,
assignment
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment