From 32fcd6960d80b4b108fbdd93938d480e303c33af Mon Sep 17 00:00:00 2001 From: amber Date: Fri, 31 Jul 2020 15:27:50 +0530 Subject: [PATCH] added logout if all students disabled --- src/pages/Authentication/Login.vue | 124 +++++++++++++++++++++++++++++++------ src/pages/Dashboard/dashboard.vue | 67 +++++++++----------- src/store/store.js | 45 +++++++++++--- 3 files changed, 170 insertions(+), 66 deletions(-) diff --git a/src/pages/Authentication/Login.vue b/src/pages/Authentication/Login.vue index be897a3..99ddf35 100644 --- a/src/pages/Authentication/Login.vue +++ b/src/pages/Authentication/Login.vue @@ -2,14 +2,19 @@ + {{ text }} + :color="snackbarColor" + > + {{ text }} + + X + import http from "@/Services/http.js"; +import AllApiCalls from "@/Services/AllApiCalls.js"; export default { + mixins: [AllApiCalls], data() { return { - snackbar: false, - y: "top", - x: "right", - mode: "", - timeout: 3000, - text: "", + // snackbar: false, + // y: "top", + // x: "right", + // mode: "", + // timeout: 3000, + // text: "", append: "", e1: true, loading: false, remember: false, valid: false, userLogincredentials: {}, - nameRules: [v => !!v || "Username is required"], + nameRules: [(v) => !!v || "Username is required"], password: "", email: "", rules: { - required: value => !!value || "password is Required." - } + required: (value) => !!value || "password is Required.", + }, + selectedStudent: {}, }; }, methods: { @@ -111,7 +119,7 @@ export default { this.loading = true; var userdata = { email: this.userLogincredentials.email, - password: this.userLogincredentials.password + password: this.userLogincredentials.password, }; if (/^[0-9]{10}$/.test(this.userLogincredentials.email)) { userdata.fatherCellNo = this.userLogincredentials.email; @@ -119,7 +127,7 @@ export default { } http() .post("/schoolLogin", userdata) - .then(response => { + .then((response) => { // console.log("token",response.data.data); this.loading = false; if (response.data.data.role === "ADMIN") { @@ -152,10 +160,90 @@ export default { this.$store.dispatch("Id", response.data.data.id); this.$store.dispatch("Role", response.data.data.role); localStorage.setItem("parentStudentId", response.data.data.id); - this.$router.push("/dashboard"); + + http() + .get("/parentStudentsList") + .then((response) => { + /* set disabled students values */ + console.log("students - ", response.data.data.students); + /* prepare an array of false status students */ + var indexStatusFalse = []; + for (var i = 0; i < response.data.data.students.length; i++) { + if (response.data.data.students[i].status == false) { + indexStatusFalse.push(i); + } + } + /* introduce a property named disabled in response to make false staus students disbled */ + for (var i = 0; i < indexStatusFalse.length; i++) { + response.data.data.students[ + indexStatusFalse[i] + ].disabled = true; + } + /* make an array of students */ + this.$store.dispatch( + "SET_STUDENTS_DATA", + response.data.data.students + ); + + /* counter to keep a track of number of students that are disabled or false */ + var counter = 0; + /* if zero element of false students list is > 0 then make first student as defalut selected */ + if (indexStatusFalse[0] > 0) { + this.selectedStudent = response.data.data.students[0]._id; + } + console.log("indexStatusFalse - ", indexStatusFalse); + /* if false student is the first one in the list then see if the next is also false */ + if (indexStatusFalse[0] == 0) { + if (indexStatusFalse.length > 1) { + for (var i = 1; i < indexStatusFalse.length; i++) { + if (indexStatusFalse[i] == i) { + if (indexStatusFalse[i - 1] == i - 1) { + counter = i + 1; + continue; + } + } else { + counter = i; + break; + } + } + } else { + counter = 1; + } + } + console.log("counter - ", counter); + if (counter == response.data.data.students.length) { + this.seeSnackbar( + "Your wards have been removed ", + "warning" + ); + + this.$store.dispatch("setToken", null); + // this.$router.replace({ path: "/" }); + this.$store.dispatch("Id", null); + // this.$router.replace({ path: "/" }); + } else { + this.selectedStudent = + response.data.data.students[counter]._id; + + localStorage.setItem( + "parentStudentId", + this.selectedStudent + ); + localStorage.setItem( + "parentClassId", + response.data.data.students[counter].classId + ); + this.$router.push("/dashboard"); + } + this.showLoader = false; + }) + .catch((err) => { + console.log("err====>", err); + this.showLoader = false; + }); } }) - .catch(error => { + .catch((error) => { if (error.response.data.message) { this.text = error.response.data.message; this.snackbar = true; @@ -167,7 +255,7 @@ export default { } }); } - } + }, }, mounted() { if (this.$store.state.isUserLoggedIn == true) { @@ -179,8 +267,8 @@ export default { computed: { color() { return this.loading ? "success" : "error"; - } - } + }, + }, }; diff --git a/src/pages/Dashboard/dashboard.vue b/src/pages/Dashboard/dashboard.vue index c40f48f..a8b875e 100644 --- a/src/pages/Dashboard/dashboard.vue +++ b/src/pages/Dashboard/dashboard.vue @@ -778,6 +778,8 @@ export default { activityList: [], }; }, + + methods: { test(e) { console.log(" test - ", e); @@ -1008,46 +1010,28 @@ export default { this.showLoader = false; }); }, - async getparentStudents() { - this.showLoader = true; - var parentStudentsId; - var classId; - await http() - .get("/parentStudentsList") - .then((response) => { - this.studentsData = response.data.data; - localStorage.setItem( - "parentStudentId", - this.studentsData.students[0]._id - ); - localStorage.setItem( - "parentClassId", - this.studentsData.students[0].classId - ); - - if (localStorage.getItem("parentStudentId") == null) { - parentStudentsId = response.data.data.students[0].classId; - classId = response.data.data.students[0]._id; - } - if (localStorage.getItem("parentStudentId")) { - parentStudentsId = localStorage.getItem("parentStudentId"); - classId = localStorage.getItem("parentClassId"); - } - - this.showLoader = false; - }) - .catch((err) => { - console.log("err====>", err); - this.showLoader = false; - }); - await this.getCourses(parentStudentsId, classId); - await this.getAnnoucementes(classId); - }, + // async getparentStudents() { + // this.showLoader = true; + // await http() + // .get("/parentStudentsList") + // .then((response) => { + + // /* set values in local storage */ + // this.studentsData = response.data.data.students; + // this.showLoader = false; + // }) + // .catch((err) => { + // console.log("err====>", err); + // this.showLoader = false; + // }); + // await this.getCourses(parentStudentsId, classId); + // await this.getAnnoucementes(classId); + // }, async getCourses(parentStudentsId, classId) { /* getStudentCourses - to get courseData - defined in GetApis.js*/ await this.getStudentCourses({ - classId: classId, - studentId: parentStudentsId, + classId: localStorage.getItem("parentClassId"), + studentId: localStorage.getItem("parentStudentId"), }); }, getAnnoucementes(classId) { @@ -1055,7 +1039,7 @@ export default { http() .get("/getAnnoucementesList", { params: { - classId: classId, + classId: localStorage.getItem("parentClassId"), }, }) .then((response) => { @@ -1097,6 +1081,7 @@ export default { }); } }, + }, mounted() { @@ -1122,7 +1107,11 @@ export default { this.token = this.$store.state.token; } else if (this.$store.state.role === "PARENT") { this.token = this.$store.state.token; - await this.getparentStudents(); + // await this.getparentStudents(); + this.studentsData = this.$store.getters.GET_STUDENTS_DATA; + console.log("students data - ",this.studentsData) + await this.getCourses(); + await this.getAnnoucementes(); } this.role = this.$store.state.role; diff --git a/src/store/store.js b/src/store/store.js index 6819c29..04fe8f7 100644 --- a/src/store/store.js +++ b/src/store/store.js @@ -17,13 +17,20 @@ export default new Vuex.Store({ role: null, // schoolId: null, schoolToken: null, - schoolRole: null + schoolRole: null, + studentsData: [], + }, + getters: { + GET_STUDENTS_DATA: state => { + return state.studentsData + }, + }, // serve as the one and only way to change the state of the data in the state object mutations: { setToken(state, token) { state.token = token - //state.isUserLoggedIn = !!(token) + //state.isUserLoggedIn = !!(token) if (token != null) { state.isUserLoggedIn = true } else { @@ -32,7 +39,7 @@ export default new Vuex.Store({ }, setSchoolToken(state, schoolToken) { state.schoolToken = schoolToken - //state.isUserLoggedIn = !!(token) + //state.isUserLoggedIn = !!(token) if (schoolToken != null) { state.isSchoolLoggedIn = true } else { @@ -51,28 +58,48 @@ export default new Vuex.Store({ setSchoolRole(state, schoolRole) { state.schoolRole = schoolRole }, + SET_STUDENTS_DATA(state, data) { + state.studentsData = data + }, // SchoolId(state, schoolId) { // state.schoolId = schoolId // } }, //Action methods are referred to as being "dispatched" actions: { - setToken({ commit }, token) { + setToken({ + commit + }, token) { commit('setToken', token) }, - setSchoolToken({ commit }, schoolToken) { + setSchoolToken({ + commit + }, schoolToken) { commit('setSchoolToken', schoolToken) }, - setUser({ commit }, data) { + setUser({ + commit + }, data) { commit('setUser', data) }, - Id({ commit }, id) { + SET_STUDENTS_DATA({ + commit + }, data) { + commit('SET_STUDENTS_DATA', data) + }, + Id({ + commit + }, id) { commit('Id', id) }, - Role({ commit }, role) { + Role({ + commit + }, role) { commit('Role', role) }, - setSchoolRole({ commit }, schoolRole) { + setSchoolRole({ + commit + }, schoolRole) { commit('setSchoolRole', schoolRole) }, // SchoolId({ commit }, schoolId) { -- 2.0.0