brotherton-erpnext/erpnext/public/js/education/academy/academy.js

116 lines
2.8 KiB
JavaScript
Raw Normal View History

2018-10-30 12:55:49 +00:00
import Vue from 'vue/dist/vue.js';
import VueRouter from 'vue-router/dist/vue-router.js'
2018-11-09 06:17:09 +00:00
import AcademyRoot from "./AcademyRoot.vue";
import routes from './routes';
import './call';
2018-10-30 12:55:49 +00:00
Vue.use(VueRouter)
2018-11-09 12:55:19 +00:00
var store = {
2018-11-03 15:13:59 +00:00
isLogin: false,
completedCourses: new Set(),
enrolledPrograms: new Set(),
2018-11-09 12:55:19 +00:00
enrolledCourses: {}
}
2018-10-30 12:55:49 +00:00
frappe.ready(() => {
2018-11-09 12:55:19 +00:00
frappe.provide('academy')
// frappe.utils.make_event_emitter(academy);
2018-11-09 06:17:09 +00:00
academy.store = new Vue({
data: store,
methods: {
2018-11-09 12:55:19 +00:00
addCompletedCourses (courseName){
if (academy.debug) console.log('addCompletedCourses triggered with', courseName)
this.completedCourses.add(courseName)
},
checkCourseCompletion (courseName){
return this.completedCourses.has(courseName)
},
checkProgramEnrollment (programName){
return this.enrolledPrograms.has(programName)
},
checkCourseEnrollment (courseName){
course = new Set(Object.keys(enrolledCourses))
return course.has(courseName)
},
updateEnrolledPrograms (){
if (academy.debug) console.log('Updating enrolledPrograms')
academy.call("get_program_enrollments").then(data => {
data.forEach(element => {
this.enrolledPrograms.add(element)
})
});
if (academy.debug) console.log('Updated State', this.enrolledPrograms)
},
updateEnrolledCourses (){
if (academy.debug) console.log('Updating enrolledCourses')
frappe.call({
method: "erpnext.www.academy.get_course_enrollments",
args:{
email: frappe.session.user
}
}).then( r => {
this.enrolledCourses = r.message
})
if (academy.debug) console.log('Updated State', this.enrolledCourses)
},
updateCompletedCourses (){
if (academy.debug) console.log('Updating States')
frappe.call({
method: "erpnext.www.academy.get_completed_courses",
args:{
email: frappe.session.user
}
}).then( r => {
if(r.message){
for(var ii=0; ii < r.message.length; ii++){
this.completedCourses.add(r.message[ii])
}
}
})
if (academy.debug) console.log('Updated State', this.completedCourses)
},
2018-11-09 06:17:09 +00:00
checkLogin (){
if(frappe.session.user === "Guest"){
2018-11-09 12:55:19 +00:00
if (academy.debug) console.log('No Session')
2018-11-09 06:17:09 +00:00
this.isLogin = false
}
else {
2018-11-09 12:55:19 +00:00
if (academy.debug) console.log('Current User: ', frappe.session.user)
2018-11-09 06:17:09 +00:00
this.isLogin = true
}
return this.isLogin
2018-11-09 12:55:19 +00:00
},
updateState (){
this.updateCompletedCourses()
this.updateEnrolledPrograms()
this.updateEnrolledCourses()
this.checkLogin()
},
}
});
academy.view = new Vue({
el: "#lms-app",
2018-11-09 12:55:19 +00:00
router: new VueRouter({ routes }),
template: "<academy-root/>",
components: { AcademyRoot },
created: function() {
if(academy.store.checkLogin()){
academy.store.updateState()
2018-11-09 06:17:09 +00:00
}
}
});
2018-11-09 12:55:19 +00:00
academy.debug = true
2018-10-30 12:55:49 +00:00
})