scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 1 | import Vue from 'vue/dist/vue.js'; |
| 2 | import VueRouter from 'vue-router/dist/vue-router.js' |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 3 | |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 4 | import AcademyRoot from "./web-academy/AcademyRoot.vue"; |
| 5 | import AcademyHome from "./web-academy/pages/AcademyHome.vue"; |
| 6 | import AcademyProgramPage from "./web-academy/pages/AcademyProgramPage.vue"; |
| 7 | import AcademyCoursePage from "./web-academy/pages/AcademyCoursePage.vue"; |
| 8 | |
| 9 | Vue.use(VueRouter) |
| 10 | |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 11 | |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 12 | const routes = [ |
scmmishra | 84297fa | 2018-11-01 17:17:30 +0530 | [diff] [blame] | 13 | {name: 'home', path: '', component: AcademyHome}, |
| 14 | {name: 'program', path: '/Program/:code', component: AcademyProgramPage, props: true}, |
| 15 | {name: 'content', path: '/Program/:code/:course/:type/:content', component: AcademyCoursePage, props: true}, |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 16 | ]; |
| 17 | |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 18 | var store = { |
| 19 | debug: true, |
| 20 | state: { |
| 21 | completedCourses: new Set(), |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 22 | enrolledPrograms: new Set(), |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 23 | currentEnrollment: '', |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 24 | student: '', |
| 25 | isLogin: false |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 26 | }, |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 27 | |
| 28 | setCurrentEnrollment (enrollment) { |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 29 | if (this.debug) console.log('setCourseEnrollment triggered with', enrollment) |
| 30 | this.state.currentEnrollment = enrollment |
| 31 | }, |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 32 | |
| 33 | getCurrentEnrollment () { |
| 34 | if (this.debug) console.log('getCourseEnrollment triggered') |
| 35 | return this.state.currentEnrollment |
| 36 | }, |
| 37 | |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 38 | addCompletedCourses (courseName){ |
| 39 | if (this.debug) console.log('addCompletedCourses triggered with', courseName) |
| 40 | this.state.completedCourses.add(courseName) |
| 41 | }, |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 42 | |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 43 | checkCourseCompletion (courseName){ |
| 44 | return this.state.completedCourses.has(courseName) |
| 45 | }, |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 46 | |
| 47 | checkProgramEnrollment (programName){ |
| 48 | return this.state.enrolledPrograms.has(programName) |
| 49 | }, |
| 50 | |
| 51 | updateCompletedCourses (){ |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 52 | if (this.debug) console.log('Updating States') |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 53 | frappe.call("erpnext.www.academy.get_completed_courses").then( r => { |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 54 | this.state.completedCourses.clear() |
| 55 | for(var ii=0; ii < r.message.length; ii++){ |
| 56 | this.state.completedCourses.add(r.message[ii]) |
| 57 | } |
| 58 | }) |
| 59 | if (this.debug) console.log('Updated State', this.state.completedCourses) |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 60 | }, |
| 61 | |
| 62 | checkLogin (){ |
| 63 | if(frappe.session.user === "Guest"){ |
| 64 | if (this.debug) console.log('No Session') |
| 65 | this.isLogin = false |
| 66 | } |
| 67 | else { |
| 68 | if (this.debug) console.log('Current User: ', frappe.session.user) |
| 69 | this.isLogin = true |
| 70 | } |
| 71 | return this.isLogin |
| 72 | }, |
| 73 | |
| 74 | updateState (){ |
| 75 | this.updateCompletedCourses() |
| 76 | this.checkLogin() |
| 77 | |
| 78 | }, |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 79 | } |
| 80 | |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 81 | const router = new VueRouter({ |
| 82 | routes: routes, |
| 83 | }); |
| 84 | |
| 85 | frappe.ready(() => { |
scmmishra | 84297fa | 2018-11-01 17:17:30 +0530 | [diff] [blame] | 86 | window.v = new Vue({ |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 87 | el: "#academy", |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 88 | router: router, |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 89 | data: store, |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 90 | template: "<academy-root/>", |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 91 | components: { AcademyRoot }, |
| 92 | created: function() { |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 93 | if(store.checkLogin()){ |
| 94 | store.updateState() |
| 95 | } |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 96 | } |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 97 | }); |
| 98 | }) |