Added custom store for state management

This commit is contained in:
scmmishra 2018-11-02 20:50:55 +05:30 committed by Aditya Hase
parent ef9a75e50d
commit 6678f5b88e

View File

@ -1,5 +1,6 @@
import Vue from 'vue/dist/vue.js'; import Vue from 'vue/dist/vue.js';
import VueRouter from 'vue-router/dist/vue-router.js' import VueRouter from 'vue-router/dist/vue-router.js'
import AcademyRoot from "./web-academy/AcademyRoot.vue"; import AcademyRoot from "./web-academy/AcademyRoot.vue";
import AcademyHome from "./web-academy/pages/AcademyHome.vue"; import AcademyHome from "./web-academy/pages/AcademyHome.vue";
import AcademyProgramPage from "./web-academy/pages/AcademyProgramPage.vue"; import AcademyProgramPage from "./web-academy/pages/AcademyProgramPage.vue";
@ -7,21 +8,56 @@ import AcademyCoursePage from "./web-academy/pages/AcademyCoursePage.vue";
Vue.use(VueRouter) Vue.use(VueRouter)
const routes = [ const routes = [
{name: 'home', path: '', component: AcademyHome}, {name: 'home', path: '', component: AcademyHome},
{name: 'program', path: '/Program/:code', component: AcademyProgramPage, props: true}, {name: 'program', path: '/Program/:code', component: AcademyProgramPage, props: true},
{name: 'content', path: '/Program/:code/:course/:type/:content', component: AcademyCoursePage, props: true}, {name: 'content', path: '/Program/:code/:course/:type/:content', component: AcademyCoursePage, props: true},
]; ];
var store = {
debug: true,
state: {
completedCourses: new Set(),
currentEnrollment: '',
currentStudentID: '',
},
setCourseEnrollment (enrollment) {
if (this.debug) console.log('setCourseEnrollment triggered with', enrollment)
this.state.currentEnrollment = enrollment
},
addCompletedCourses (courseName){
if (this.debug) console.log('addCompletedCourses triggered with', courseName)
this.state.completedCourses.add(courseName)
},
checkCourseCompletion (courseName){
return this.state.completedCourses.has(courseName)
},
updateState (){
if (this.debug) console.log('Updating States')
frappe.call("erpnext.www.academy.get_state").then( r => {
this.state.completedCourses.clear()
for(var ii=0; ii < r.message.length; ii++){
this.state.completedCourses.add(r.message[ii])
}
})
if (this.debug) console.log('Updated State', this.state.completedCourses)
}
}
const router = new VueRouter({ const router = new VueRouter({
routes: routes, routes: routes,
}); });
frappe.ready(() => { frappe.ready(() => {
window.v = new Vue({ window.v = new Vue({
el: "#web-academy", el: "#academy",
router: router, router: router,
data: store,
template: "<academy-root/>", template: "<academy-root/>",
components: { AcademyRoot } components: { AcademyRoot },
created: function() {
store.updateState()
}
}); });
}) })