diff --git a/src/components/admin/Charts.vue b/src/components/admin/Charts.vue index fc9bcfb6..147998d9 100644 --- a/src/components/admin/Charts.vue +++ b/src/components/admin/Charts.vue @@ -40,8 +40,7 @@ export default { totalAvailable: 0, totalUsed: 0, unused: 0, - user: {}, - usage: {}, + user: {}, sections: [ { label: "REQUESTS CONSUMED", value: this.totalUsed, color: "#ed5c5c" }, { label: "REQUESTS LEFT", value: this.unused, color: "#60c860" }, @@ -55,47 +54,14 @@ export default { this.user = { ...JSON.parse(usrStr), }; - await this.fetchSubscription(); - - this.totalAvailable = this.usage["totalAvailable"]; - this.totalUsed = this.usage["totalUsed"]; + this.totalAvailable = this.user.usage["totalAvailable"]; + this.totalUsed = this.user.usage["totalUsed"]; this.unused = this.totalAvailable - this.totalUsed; this.sections[0].value = this.totalUsed; this.sections[1].value = this.unused; - }, methods: { handleSectionClick() {}, - async fetchSubscription() { - try { - this.isLoading = true; - - // if (!this.user.id) throw new Error("No project owner found"); - - const url = `${this.$config.studioServer.BASE_URL}api/v1/subscription?usage=true`; - const headers = { - Authorization: `Bearer ${this.authToken}`, - AccessToken:`Bearer ${this.accessToken}` - }; - const resp = await fetch(url, { - headers, - method: "GET", - }); - - if (!resp.ok) { - return this.notifyErr(resp.statusText); - } - const json = await resp.json(); - this.usage = json["usage"]; - - // localStorage.setItem("subscriptions", JSON.stringify(json)); - // this.notifySuccess("No. of projects fetched " + this.projects.length); - } catch (e) { - this.notifyErr(e.message); - } finally { - this.isLoading = false; - } - }, }, }; diff --git a/src/router.js b/src/router.js index 3953448d..3fb7e3cd 100644 --- a/src/router.js +++ b/src/router.js @@ -190,8 +190,11 @@ router.beforeEach((to, from, next) => { const authToken = localStorage.getItem("authToken"); if (authToken) { - // console.log("Yes auth token"); - const url = `${config.studioServer.BASE_URL}hs/api/v2/auth/protected`; + let usage = true; + if (to.path === "/admin/subscription") { + usage = false; + } + const url = `${config.studioServer.BASE_URL}hs/api/v2/auth/protected?usage=${usage}`; fetch(url, { headers: { Authorization: `Bearer ${authToken}`, @@ -214,15 +217,29 @@ router.beforeEach((to, from, next) => { localStorage.removeItem("accessuser") } if ( - to.meta.admin && - !json.message.isSubscribed && - (to.path != "/admin/subscription" && to.path != "/admin/dashboard") + to.meta.admin ) { - eventBus.$emit("UpdateAdminNav", false); - next({ - path: "/admin/subscription", - params: { nextUrl: to.fullPath }, - }); + if (!json.message.isSubscribed && + (to.path != "/admin/subscription" && to.path != "/admin/dashboard")) { + eventBus.$emit("UpdateAdminNav", false); + next({ + path: "/admin/subscription", + params: { nextUrl: to.fullPath }, + }); + } else { + // if he is subscired + const usage = json.message.usage; + if (usage && usage.totalUsed >= usage.totalAvailable) { + next({ + path: "/admin/subscription", + params: { nextUrl: to.fullPath }, + }); + eventBus.$emit("UpdateAdminNav", false); + } else { + eventBus.$emit("UpdateAdminNav", true); + next(); + } + } } else { next(); } diff --git a/src/views/admin/Dashboard.vue b/src/views/admin/Dashboard.vue index 2267e3af..0349c2a4 100644 --- a/src/views/admin/Dashboard.vue +++ b/src/views/admin/Dashboard.vue @@ -88,76 +88,12 @@ export default { this.accessuser={ ...JSON.parse(accessUser) } - - this.fetchSubscription() - } + } }, methods: { - async fetchSubscription() { - try { - this.isLoading = true; - - // if (!this.user.id) throw new Error("No project owner found"); - - const url = `${this.$config.studioServer.BASE_URL}api/v1/subscription?usage=true`; - const headers = { - Authorization: `Bearer ${this.authToken}`, - AccessToken:`Bearer ${this.accessToken}` - }; - const resp = await fetch(url, { - headers, - method: "GET", - }); - - if (!resp.ok) { - return this.notifyErr(resp.statusText); - } - const json = await resp.json(); - this.subscriptions = json["subscriptions"]; - this.activeSubscriptions=this.subscriptions.filter((x) => (x.paymentData?true:x.isActive===true)) - this.paidSubscriptions=this.subscriptions.filter((x) => { - - return x.paymentData?true:(x.isPaid===true)}) - - const usage = json["usage"] - - if(usage && (usage.totalUsed >= usage.totalAvailable)){ - this.$router.push("/admin/subscription"); - eventBus.$emit('UpdateAdminNav', false); - - - - } - - if (usage && usage.totalAvailable > 0) { - eventBus.$emit('UpdateAdminNav', true); - } else { - eventBus.$emit('UpdateAdminNav', false); - } - // localStorage.setItem("subscriptions", JSON.stringify(json)); - // this.notifySuccess("No. of projects fetched " + this.projects.length); - } catch (e) { - this.notifyErr(e.message); - } finally { - this.isLoading = false; - } - }, gotosubpage: id => { this.$router.push(`${id}`); }, - logout(){ - // console.log("Dashboard:: Logout method...") - // localStorage.removeItem('authToken') - // localStorage.removeItem('user') - // localStorage.removeItem("credentials") - // localStorage.removeItem("userData") - - // if(this.$route.params.nextUrl != null){ - // this.$router.push(this.$route.params.nextUrl) - // }else{ - // this.$router.push('/login') - // } - }, }, mixins: [notificationMixins] diff --git a/src/views/admin/Events.vue b/src/views/admin/Events.vue index aa911305..681a6db9 100644 --- a/src/views/admin/Events.vue +++ b/src/views/admin/Events.vue @@ -557,7 +557,6 @@ export default { //const usrStr = localStorage.getItem("user"); //this.user = null; JSON.parse(usrStr); - this.fetchSubscription(); const usrStr = localStorage.getItem("user"); this.user = { @@ -582,54 +581,6 @@ export default { }, methods: { - async fetchSubscription() { - try { - this.isLoading = true; - - // if (!this.user.id) throw new Error("No project owner found"); - - const url = `${this.$config.studioServer.BASE_URL}api/v1/subscription?usage=true`; - const headers = { - Authorization: `Bearer ${this.authToken}`, - AccessToken: `Bearer ${this.accessToken}`, - }; - const resp = await fetch(url, { - headers, - method: "GET", - }); - - if (!resp.ok) { - return this.notifyErr(resp.statusText); - } - const json = await resp.json(); - this.subscriptions = json["subscriptions"]; - this.activeSubscriptions = this.subscriptions.filter((x) => - x.paymentData ? true : x.isActive === true - ); - this.paidSubscriptions = this.subscriptions.filter((x) => { - return x.paymentData ? true : x.isPaid === true; - }); - - const usage = json["usage"]; - - if (usage && usage.totalUsed >= usage.totalAvailable) { - this.$router.push("/admin/subscription"); - eventBus.$emit("UpdateAdminNav", false); - } - - if (usage && usage.totalAvailable > 0) { - eventBus.$emit("UpdateAdminNav", true); - } else { - eventBus.$emit("UpdateAdminNav", false); - } - // localStorage.setItem("subscriptions", JSON.stringify(json)); - // this.notifySuccess("No. of projects fetched " + this.projects.length); - } catch (e) { - this.notifyErr(e.message); - } finally { - this.isLoading = false; - } - }, async getTags() { const url = `${this.$config.studioServer.BASE_URL}api/v1/tag`; const headers = { diff --git a/src/views/admin/Participants.vue b/src/views/admin/Participants.vue index ba51f6c6..e736f5a1 100644 --- a/src/views/admin/Participants.vue +++ b/src/views/admin/Participants.vue @@ -407,7 +407,6 @@ computed:{ }, async mounted() { - this.fetchSubscription(); if (this.$route.query.projectId) { this.selectedProjectId = this.$route.query.projectId; this.selectedProject = this.$route.query.projectId; @@ -432,54 +431,6 @@ computed:{ }, methods: { - async fetchSubscription() { - try { - this.isLoading = true; - - // if (!this.user.id) throw new Error("No project owner found"); - - const url = `${this.$config.studioServer.BASE_URL}api/v1/subscription?usage=true`; - const headers = { - Authorization: `Bearer ${this.authToken}`, - AccessToken: `Bearer ${this.accessToken}`, - }; - const resp = await fetch(url, { - headers, - method: "GET", - }); - - if (!resp.ok) { - return this.notifyErr(resp.statusText); - } - const json = await resp.json(); - this.subscriptions = json["subscriptions"]; - this.activeSubscriptions = this.subscriptions.filter((x) => - x.paymentData ? true : x.isActive === true - ); - this.paidSubscriptions = this.subscriptions.filter((x) => { - return x.paymentData ? true : x.isPaid === true; - }); - - const usage = json["usage"]; - - if (usage && usage.totalUsed >= usage.totalAvailable) { - this.$router.push("/admin/subscription"); - eventBus.$emit("UpdateAdminNav", false); - } - - if (usage && usage.totalAvailable > 0) { - eventBus.$emit("UpdateAdminNav", true); - } else { - eventBus.$emit("UpdateAdminNav", false); - } - // localStorage.setItem("subscriptions", JSON.stringify(json)); - // this.notifySuccess("No. of projects fetched " + this.projects.length); - } catch (e) { - this.notifyErr(e.message); - } finally { - this.isLoading = false; - } - }, parseActionValue(action) { switch (action.type) { case "DISCORD_JOIN":