Skip to content

Instantly share code, notes, and snippets.

@benjcal
Last active March 28, 2018 22:04
Show Gist options
  • Save benjcal/3681ff22301f9dc29acc26dce641d626 to your computer and use it in GitHub Desktop.
Save benjcal/3681ff22301f9dc29acc26dce641d626 to your computer and use it in GitHub Desktop.
import { observable, computed } from 'mobx'
const store = observable({
students: observable.map(),
courses: observable.map(),
enrollment: observable.map(),
addStudent(student) {
if (this.students.has(student.id)) {
throw new Error('student already exists')
} else {
this.students.set(student.id, student)
}
},
updateStudent(student) {
this.students.set(student.id, student)
},
removeStudent(id) {
this.students.delete(id)
},
addCourse(course) {
if (this.courses.has(course.id)) {
throw new Error('course already exists')
} else {
this.courses.set(course.id, course)
}
},
updateCourse(course) {
this.courses.set(course.id, course)
},
removeCourse(id) {
this.courses.delete(id)
},
enrollStudent(courseId, studentId) {
if (!this.enrollment.get(courseId)) {
this.enrollment.set(courseId, observable.array([]))
this.enrollment.get(courseId).push(studentId)
} else if (!this.enrollment.get(courseId).includes(studentId)){
this.enrollment.get(courseId).push(studentId)
}
},
unenrollStudent(courseId, studentId) {
this.enrollment.get(courseId).remove(studentId)
},
enrolledStudents(courseId) {
return computed(() =>
this.enrollment.get(courseId).map(n =>
this.students.get(n)))
.get()
}
})
export default store
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment