Refactored State management store
diff --git a/erpnext/public/js/education/lms/components/ProgramCard.vue b/erpnext/public/js/education/lms/components/ProgramCard.vue
index 6bd8532..d0df94d 100644
--- a/erpnext/public/js/education/lms/components/ProgramCard.vue
+++ b/erpnext/public/js/education/lms/components/ProgramCard.vue
@@ -1,7 +1,7 @@
<template>
<div class='card-deck mt-5'>
<div class="card">
- <img :src="program.hero_image" style='height: 150px; width: auto'>
+ <img v-if="program.hero_image" :src="program.hero_image" style='height: 150px; width: auto'>
<div class='card-body'>
<router-link :to="'/Program/' + program.name">
<h5 class='card-title'>{{ program.program_name }}</h5>
@@ -18,7 +18,7 @@
>
{{ buttonName }}
</a-button>
- <a v-else-if="isLogin" class='btn btn-secondary btn-sm' @click="enroll()">Enroll</a>
+ <a v-else-if="isLogin" class='btn btn-secondary btn-sm' @click="enroll()">{{ enrollButton }}</a>
<a v-else class='btn btn-secondary btn-sm' href="/login#signup">Sign Up</a>
</div>
</div>
@@ -31,20 +31,23 @@
name: "ProgramCard",
data() {
return {
- isLogin: lms.store.isLogin
+ isLogin: lms.store.isLogin,
+ enrollButton: 'Enroll Now',
+ programRoute: { name: 'program', params: { program_name: this.program.name }}
};
},
created() {
},
methods: {
enroll() {
+ this.enrollButton = 'Enrolling...'
lms.call('enroll_in_program', {
program_name: this.program.name,
- }).then(
- lms.store.enrolledPrograms.add(this.program.name),
- lms.store.updateEnrolledPrograms(),
- this.router.push('Program/' + this.program.name)
- )
+ }).then(data => {
+ console.log(data)
+ lms.store.enrolledPrograms.add(data),
+ this.$router.push(this.programRoute)
+ })
}
},
computed: {
@@ -57,7 +60,7 @@
}
},
programPageRoute() {
- return { name: 'program', params: { program_name: this.program.name }}
+ return this.programRoute
},
isEnrolled() {
return lms.store.enrolledPrograms.has(this.program.name)
@@ -71,6 +74,9 @@
<style lang="css" scoped>
a {
- text-decoration: none;
-}
+ text-decoration: none;
+ }
+ a.btn-secondary {
+ color: white !important;
+ }
</style>
\ No newline at end of file
diff --git a/erpnext/public/js/education/lms/components/TopSectionButton.vue b/erpnext/public/js/education/lms/components/TopSectionButton.vue
index a9b227f..85897d8 100644
--- a/erpnext/public/js/education/lms/components/TopSectionButton.vue
+++ b/erpnext/public/js/education/lms/components/TopSectionButton.vue
@@ -30,7 +30,7 @@
}
if(this.isLoggedIn){
- if(lms.store.checkProgramEnrollment(this.$route.params.program_name)){
+ if(lms.store.enrolledPrograms.has(this.$route.params.program_name)){
if(this.$route.name == 'home'){
this.buttonName = 'Explore Courses'
}
@@ -51,7 +51,7 @@
if(this.$route.name == 'home'){
return
}
- else if(this.$route.name == 'program' && lms.store.checkProgramEnrollment(this.$route.params.program_name)){
+ else if(this.$route.name == 'program' && lms.store.enrolledPrograms.has(this.$route.params.program_name)){
this.$router.push({ name: 'content', params: { program_name: this.$route.params.program_name, course: this.nextCourse, type: this.nextContentType, content: this.nextContent}})
}
else {
diff --git a/erpnext/public/js/education/lms/lms.js b/erpnext/public/js/education/lms/lms.js
index 103f45b..a2a552e 100644
--- a/erpnext/public/js/education/lms/lms.js
+++ b/erpnext/public/js/education/lms/lms.js
@@ -9,7 +9,6 @@
var store = {
isLogin: false,
- completedCourses: new Set(),
enrolledPrograms: new Set(),
enrolledCourses: {}
}
@@ -21,65 +20,27 @@
lms.store = new Vue({
data: store,
methods: {
- addCompletedCourses (courseName){
- if (lms.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 (lms.debug) console.log('Updating enrolledPrograms')
- lms.call("get_program_enrollments").then(data => {
- data.forEach(element => {
- this.enrolledPrograms.add(element)
- })
- });
- if (lms.debug) console.log('Updated State', this.enrolledPrograms)
- },
-
- updateEnrolledCourses (){
- if (lms.debug) console.log('Updating enrolledCourses')
- frappe.call({
- method: "erpnext.www.lms.get_course_enrollments",
- args:{
- email: frappe.session.user
- }
- }).then( r => {
- this.enrolledCourses = r.message
- })
- if (lms.debug) console.log('Updated State', this.enrolledCourses)
- },
-
- updateCompletedCourses (){
- if (lms.debug) console.log('Updating States')
- frappe.call({
- method: "erpnext.www.lms.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])
+ updateEnrolledPrograms() {
+ if(this.isLogin) {
+ lms.call("get_program_enrollments").then(data => {
+ if(data){
+ data.forEach(element => {
+ this.enrolledPrograms.add(element)
+ })
}
- }
- })
- if (lms.debug) console.log('Updated State', this.completedCourses)
+ });
+ if (lms.debug) console.log('Updated Enrolled Programs', this.enrolledPrograms)
+ }
},
- checkLogin (){
+ updateEnrolledCourses() {
+ lms.call("get_course_enrollments").then(data => {
+ if(data) this.enrolledCourses = data
+ })
+ if (lms.debug) console.log('Updated Enrolled Courses', this.enrolledCourses)
+ },
+
+ checkLogin() {
if(frappe.session.user === "Guest"){
if (lms.debug) console.log('No Session')
this.isLogin = false
@@ -91,11 +52,10 @@
return this.isLogin
},
- updateState (){
- this.updateCompletedCourses()
+ updateState() {
+ this.checkLogin()
this.updateEnrolledPrograms()
this.updateEnrolledCourses()
- this.checkLogin()
},
}
});
@@ -105,10 +65,8 @@
router: new VueRouter({ routes }),
template: "<lms-root/>",
components: { lmsRoot },
- created: function() {
- if(lms.store.checkLogin()){
+ mounted() {
lms.store.updateState()
- }
}
});
diff --git a/erpnext/public/js/education/lms/pages/ProgramPage.vue b/erpnext/public/js/education/lms/pages/ProgramPage.vue
index bc0f267..6fde566 100644
--- a/erpnext/public/js/education/lms/pages/ProgramPage.vue
+++ b/erpnext/public/js/education/lms/pages/ProgramPage.vue
@@ -28,7 +28,7 @@
}
},
beforeMount() {
- if(lms.store.isLogin) lms.store.updateCompletedCourses()
+ // if(lms.store.isLogin) lms.store.updateCompletedCourses()
},
mounted() {
this.getProgramDetails().then(data => this.program = data);