Skip to content
This repository was archived by the owner on Jan 20, 2021. It is now read-only.

Commit ab44b60

Browse files
committed
store: implement API caching for logged in user
Signed-off-by: Rohit Yadav <rohit.yadav@shapeblue.com>
1 parent fc9f618 commit ab44b60

File tree

4 files changed

+31
-19
lines changed

4 files changed

+31
-19
lines changed

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/permission.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import 'nprogress/nprogress.css' // progress bar style
2525
import message from 'ant-design-vue/es/message'
2626
import notification from 'ant-design-vue/es/notification'
2727
import { setDocumentTitle, domTitle } from '@/utils/domUtil'
28-
import { ACCESS_TOKEN } from '@/store/mutation-types'
28+
import { ACCESS_TOKEN, APIS } from '@/store/mutation-types'
2929

3030
NProgress.configure({ showSpinner: false }) // NProgress Configuration
3131

@@ -42,7 +42,10 @@ router.beforeEach((to, from, next) => {
4242
NProgress.done()
4343
} else {
4444
if (Object.keys(store.getters.apis).length === 0) {
45-
message.loading('Discovering features...', 5)
45+
const cachedApis = Vue.ls.get(APIS, {})
46+
if (Object.keys(cachedApis).length === 0) {
47+
message.loading('Loading...', 4)
48+
}
4649
store
4750
.dispatch('GetInfo')
4851
.then(apis => {

src/store/modules/user.js

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import Cookies from 'js-cookie'
1919
import Vue from 'vue'
2020
import md5 from 'md5'
2121
import { login, logout, api } from '@/api'
22-
import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, ASYNC_JOB_IDS } from '@/store/mutation-types'
22+
import { ACCESS_TOKEN, CURRENT_PROJECT, DEFAULT_THEME, APIS, ASYNC_JOB_IDS } from '@/store/mutation-types'
2323

2424
const user = {
2525
state: {
@@ -54,6 +54,7 @@ const user = {
5454
},
5555
SET_APIS: (state, apis) => {
5656
state.apis = apis
57+
Vue.ls.set(APIS, apis)
5758
},
5859
SET_FEATURES: (state, features) => {
5960
state.features = features
@@ -114,22 +115,29 @@ const user = {
114115

115116
GetInfo ({ commit }) {
116117
return new Promise((resolve, reject) => {
117-
api('listApis').then(response => {
118-
const apis = {}
119-
const apiList = response.listapisresponse.api
120-
for (var idx = 0; idx < apiList.length; idx++) {
121-
const api = apiList[idx]
122-
const apiName = api.name
123-
apis[apiName] = {
124-
params: api.params,
125-
response: api.response
118+
const cachedApis = Vue.ls.get(APIS, {})
119+
if (Object.keys(cachedApis).length > 0) {
120+
console.log('Login detected, using cached APIs')
121+
commit('SET_APIS', cachedApis)
122+
resolve(cachedApis)
123+
} else {
124+
api('listApis').then(response => {
125+
const apis = {}
126+
const apiList = response.listapisresponse.api
127+
for (var idx = 0; idx < apiList.length; idx++) {
128+
const api = apiList[idx]
129+
const apiName = api.name
130+
apis[apiName] = {
131+
params: api.params,
132+
response: api.response
133+
}
126134
}
127-
}
128-
commit('SET_APIS', apis)
129-
resolve(apis)
130-
}).catch(error => {
131-
reject(error)
132-
})
135+
commit('SET_APIS', apis)
136+
resolve(apis)
137+
}).catch(error => {
138+
reject(error)
139+
})
140+
}
133141

134142
api('listUsers').then(response => {
135143
const result = response.listusersresponse.user[0]

src/store/mutation-types.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ export const DEFAULT_FIXED_SIDEMENU = 'DEFAULT_FIXED_SIDEMENU'
2727
export const DEFAULT_FIXED_HEADER_HIDDEN = 'DEFAULT_FIXED_HEADER_HIDDEN'
2828
export const DEFAULT_CONTENT_WIDTH_TYPE = 'DEFAULT_CONTENT_WIDTH_TYPE'
2929
export const DEFAULT_MULTI_TAB = 'DEFAULT_MULTI_TAB'
30+
export const APIS = 'APIS'
3031
export const ASYNC_JOB_IDS = 'ASYNC_JOB_IDS'
3132

3233
export const CONTENT_WIDTH_TYPE = {

0 commit comments

Comments
 (0)