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