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 = { |
scmmishra | b409f7a | 2018-11-07 22:12:55 +0530 | [diff] [blame] | 19 | debug: true, |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 20 | isLogin: false, |
| 21 | completedCourses: new Set(), |
| 22 | enrolledPrograms: new Set(), |
| 23 | enrolledCourses: {}, |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 24 | |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 25 | addCompletedCourses (courseName){ |
| 26 | if (this.debug) console.log('addCompletedCourses triggered with', courseName) |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 27 | this.completedCourses.add(courseName) |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 28 | }, |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 29 | |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 30 | checkCourseCompletion (courseName){ |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 31 | return this.completedCourses.has(courseName) |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 32 | }, |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 33 | |
| 34 | checkProgramEnrollment (programName){ |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 35 | return this.enrolledPrograms.has(programName) |
| 36 | }, |
| 37 | |
| 38 | checkCourseEnrollment (courseName){ |
| 39 | course = new Set(Object.keys(enrolledCourses)) |
| 40 | return course.has(courseName) |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 41 | }, |
| 42 | |
scmmishra | a054655 | 2018-11-03 19:33:04 +0530 | [diff] [blame] | 43 | updateEnrolledPrograms (){ |
| 44 | if (this.debug) console.log('Updating enrolledPrograms') |
scmmishra | a3afe2a | 2018-11-05 11:01:54 +0530 | [diff] [blame] | 45 | frappe.call({ |
| 46 | method: "erpnext.www.academy.get_program_enrollments", |
| 47 | args:{ |
| 48 | email: frappe.session.user |
| 49 | } |
| 50 | }).then( r => { |
scmmishra | 8fa9ade | 2018-11-08 13:01:55 +0530 | [diff] [blame] | 51 | if(r.message){ |
| 52 | for(var ii=0; ii < r.message.length; ii++){ |
| 53 | this.enrolledPrograms.add(r.message[ii]) |
| 54 | } |
scmmishra | a054655 | 2018-11-03 19:33:04 +0530 | [diff] [blame] | 55 | } |
| 56 | }) |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 57 | if (this.debug) console.log('Updated State', this.enrolledPrograms) |
scmmishra | a054655 | 2018-11-03 19:33:04 +0530 | [diff] [blame] | 58 | }, |
| 59 | |
| 60 | updateEnrolledCourses (){ |
| 61 | if (this.debug) console.log('Updating enrolledCourses') |
scmmishra | a3afe2a | 2018-11-05 11:01:54 +0530 | [diff] [blame] | 62 | frappe.call({ |
| 63 | method: "erpnext.www.academy.get_course_enrollments", |
| 64 | args:{ |
| 65 | email: frappe.session.user |
| 66 | } |
| 67 | }).then( r => { |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 68 | this.enrolledCourses = r.message |
scmmishra | a054655 | 2018-11-03 19:33:04 +0530 | [diff] [blame] | 69 | }) |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 70 | if (this.debug) console.log('Updated State', this.enrolledCourses) |
scmmishra | a054655 | 2018-11-03 19:33:04 +0530 | [diff] [blame] | 71 | }, |
| 72 | |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 73 | updateCompletedCourses (){ |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 74 | if (this.debug) console.log('Updating States') |
scmmishra | a3afe2a | 2018-11-05 11:01:54 +0530 | [diff] [blame] | 75 | frappe.call({ |
| 76 | method: "erpnext.www.academy.get_completed_courses", |
| 77 | args:{ |
| 78 | email: frappe.session.user |
| 79 | } |
| 80 | }).then( r => { |
scmmishra | 8fa9ade | 2018-11-08 13:01:55 +0530 | [diff] [blame] | 81 | if(r.message){ |
| 82 | for(var ii=0; ii < r.message.length; ii++){ |
| 83 | this.completedCourses.add(r.message[ii]) |
| 84 | } |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 85 | } |
| 86 | }) |
scmmishra | 1a04f77 | 2018-11-03 20:43:59 +0530 | [diff] [blame] | 87 | if (this.debug) console.log('Updated State', this.completedCourses) |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 88 | }, |
| 89 | |
| 90 | checkLogin (){ |
| 91 | if(frappe.session.user === "Guest"){ |
| 92 | if (this.debug) console.log('No Session') |
| 93 | this.isLogin = false |
| 94 | } |
| 95 | else { |
| 96 | if (this.debug) console.log('Current User: ', frappe.session.user) |
| 97 | this.isLogin = true |
| 98 | } |
| 99 | return this.isLogin |
| 100 | }, |
| 101 | |
| 102 | updateState (){ |
| 103 | this.updateCompletedCourses() |
scmmishra | a054655 | 2018-11-03 19:33:04 +0530 | [diff] [blame] | 104 | this.updateEnrolledPrograms() |
| 105 | this.updateEnrolledCourses() |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 106 | this.checkLogin() |
| 107 | |
| 108 | }, |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 109 | } |
| 110 | |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 111 | const router = new VueRouter({ |
| 112 | routes: routes, |
| 113 | }); |
| 114 | |
| 115 | frappe.ready(() => { |
scmmishra | 84297fa | 2018-11-01 17:17:30 +0530 | [diff] [blame] | 116 | window.v = new Vue({ |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 117 | el: "#academy", |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 118 | router: router, |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 119 | data: store, |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 120 | template: "<academy-root/>", |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 121 | components: { AcademyRoot }, |
| 122 | created: function() { |
scmmishra | bcafe84 | 2018-11-03 14:48:42 +0530 | [diff] [blame] | 123 | if(store.checkLogin()){ |
| 124 | store.updateState() |
| 125 | } |
scmmishra | 6678f5b | 2018-11-02 20:50:55 +0530 | [diff] [blame] | 126 | } |
scmmishra | 1778b59 | 2018-10-30 18:25:49 +0530 | [diff] [blame] | 127 | }); |
| 128 | }) |