add some eslint-config - works fine for js now, reindent main

This commit is contained in:
Anton Schubert 2017-08-30 13:28:19 +02:00
parent 3ceb1a2c1a
commit 311e911523
4 changed files with 205 additions and 188 deletions

View File

@ -1,8 +1,23 @@
{ {
"parser": "babel-eslint", "parserOptions": {
"rules": { "parser": "babel-eslint",
"ecmaVersion": 6,
"sourceType": "module",
"impliedStrict": true
},
"env": {
"browser": true,
"es6": true
}, },
"plugins": [ "plugins": [
"html" "html",
] ],
extends: [
"eslint:recommended",
"plugin:vue/recommended"
],
"rules": {
"indent": ["error", 4],
"no-console": 0
},
} }

View File

@ -21,6 +21,7 @@
"eslint": "^4.5.0", "eslint": "^4.5.0",
"eslint-loader": "^1.9.0", "eslint-loader": "^1.9.0",
"eslint-plugin-html": "^3.2.0", "eslint-plugin-html": "^3.2.0",
"eslint-plugin-vue": "^3.12.0",
"less": "^2.7.2", "less": "^2.7.2",
"less-loader": "^4.0.5", "less-loader": "^4.0.5",
"vue-hot-reload-api": "^2.1.0", "vue-hot-reload-api": "^2.1.0",

View File

@ -13,212 +13,212 @@ Vue.use(Vuex);
const router = new VueRouter(); const router = new VueRouter();
const store = new Vuex.Store({ const store = new Vuex.Store({
state: { state: {
api: {}, api: {},
navigation: [], navigation: [],
persist: { persist: {
login: {}, login: {},
authToken: '', authToken: '',
jwt: {}, jwt: {},
credentials: {} credentials: {}
} }
},
mutations: {
setAPI(state, payload) {
state.api = payload;
}, },
setNavigation(state, payload) { mutations: {
for (var i = 0; i < payload.length; i++) { setAPI(state, payload) {
state.navigation.push(payload[i]); // = payload; state.api = payload;
} },
}, setNavigation(state, payload) {
setCredentials(state, payload) { for (var i = 0; i < payload.length; i++) {
state.persist.credentials = { state.navigation.push(payload[i]); // = payload;
username: payload.username,
password: payload.password
};
objectToPersist(state.persist, 'persistantStore');
},
setLogin(state, payload) {
state.persist.login = payload.User;
state.persist.authToken = payload.AuthToken;
state.persist.jwt = JwtDecode(payload.AuthToken);
objectToPersist(state.persist, 'persistantStore');
},
clearLogin(state) {
state.persist.login = {};
state.persist.authToken = '';
state.persist.jwt = {};
state.persist.credentials = {};
objectToPersist(state.persist, 'persistantStore');
}
},
actions: {
apiRequest(context, payload) {
let doRequest = () => {
return new Promise((resolve, reject) => {
Axios({
method: payload.method ? payload.method : 'get',
baseURL: context.state.api.baseURL,
url: payload.endpoint,
params: payload.params,
data: payload.data,
headers: {
'X-Auth-Token': context.state.persist.authToken
} }
}) },
.then(response => { setCredentials(state, payload) {
console.log(response); state.persist.credentials = {
resolve(response.data); username: payload.username,
}) password: payload.password
.catch(error => { };
console.dir(error); objectToPersist(state.persist, 'persistantStore');
reject(error); },
}) setLogin(state, payload) {
}); state.persist.login = payload.User;
}; state.persist.authToken = payload.AuthToken;
state.persist.jwt = JwtDecode(payload.AuthToken);
if (payload.endpoint != 'login') { objectToPersist(state.persist, 'persistantStore');
// no jwt check for login call },
clearLogin(state) {
// empty username = not logged in state.persist.login = {};
if (!context.state.persist.credentials.username) { state.persist.authToken = '';
return new Promise((resolve, reject) => { state.persist.jwt = {};
// show login page state.persist.credentials = {};
router.push('/login'); objectToPersist(state.persist, 'persistantStore');
reject(['not logged in']);
});
} }
let now = Math.round(Date.now() / 1000);
if (context.state.persist.jwt.exp < (now + 300)) {
// too old jwt, logout
return new Promise((resolve, reject) => {
context.commit('clearLogin');
router.push('/login');
reject(['jwt too old, logout']);
});
}
if ((context.state.persist.jwt.exp - 60) < now) {
// jwt near expire, get new one
console.log("getting new jwt");
return context.dispatch('login', context.state.persist.credentials)
.then(() => {
console.log("LOOOGIIIINNN");
return doRequest();
})
.catch(error => {
throw error;
})
}
}
return doRequest();
}, },
login(context, payload) { actions: {
return new Promise((resolve, reject) => { apiRequest(context, payload) {
context.dispatch('apiRequest', { let doRequest = () => {
method: 'post', return new Promise((resolve, reject) => {
endpoint: 'login', Axios({
data: payload method: payload.method ? payload.method : 'get',
}) baseURL: context.state.api.baseURL,
.then(data => { url: payload.endpoint,
context.commit('setCredentials', payload); params: payload.params,
context.commit('setLogin', data); data: payload.data,
resolve(data.User); headers: {
}) 'X-Auth-Token': context.state.persist.authToken
.catch(error => { }
if (error.response && error.response.data && error.response.data.error) { })
reject(error.response.data.error); .then(response => {
} else { console.log(response);
reject([]); resolve(response.data);
})
.catch(error => {
console.dir(error);
reject(error);
})
});
};
if (payload.endpoint != 'login') {
// no jwt check for login call
// empty username = not logged in
if (!context.state.persist.credentials.username) {
return new Promise((resolve, reject) => {
// show login page
router.push('/login');
reject(['not logged in']);
});
}
let now = Math.round(Date.now() / 1000);
if (context.state.persist.jwt.exp < (now + 300)) {
// too old jwt, logout
return new Promise((resolve, reject) => {
context.commit('clearLogin');
router.push('/login');
reject(['jwt too old, logout']);
});
}
if ((context.state.persist.jwt.exp - 60) < now) {
// jwt near expire, get new one
console.log("getting new jwt");
return context.dispatch('login', context.state.persist.credentials)
.then(() => {
console.log("LOOOGIIIINNN");
return doRequest();
})
.catch(error => {
throw error;
})
}
} }
});
}); return doRequest();
},
login(context, payload) {
return new Promise((resolve, reject) => {
context.dispatch('apiRequest', {
method: 'post',
endpoint: 'login',
data: payload
})
.then(data => {
context.commit('setCredentials', payload);
context.commit('setLogin', data);
resolve(data.User);
})
.catch(error => {
if (error.response && error.response.data && error.response.data.error) {
reject(error.response.data.error);
} else {
reject([]);
}
});
});
}
} }
}
}); });
function objectToPersist(obj, key) { function objectToPersist(obj, key) {
try { try {
const serialized = JSON.stringify(obj); const serialized = JSON.stringify(obj);
localStorage.setItem(key ? key : 'persistant', serialized); localStorage.setItem(key ? key : 'persistant', serialized);
} catch (error) { } catch (error) {
console.error(error); console.error(error);
} }
}; }
function persistToObject(key) { function persistToObject(key) {
try { try {
const serialized = localStorage.getItem(key ? key : 'persistant'); const serialized = localStorage.getItem(key ? key : 'persistant');
if (serialized === null) { if (serialized === null) {
return undefined; return undefined;
}
return JSON.parse(serialized);
} catch (error) {
console.log(error);
return undefined;
} }
return JSON.parse(serialized);
} catch (error) {
console.log(error);
return undefined;
}
} }
// get store persist part from localStorage // get store persist part from localStorage
let persist = persistToObject('persistantStore'); let persist = persistToObject('persistantStore');
if (persist) { if (persist) {
store.state.persist = persist; store.state.persist = persist;
} }
// load init // load init
Axios.get('conf/init.json') Axios.get('conf/init.json')
.then(results => { .then(results => {
// set navigation // set navigation
if (!Array.isArray(results.data.navigation)) { if (!Array.isArray(results.data.navigation)) {
alert('invalid data in init.json'); alert('invalid data in init.json');
return; return;
} }
const navigation = results.data.navigation.map(({name, to, icon, show}) => {return {name, to, icon, show}}); const navigation = results.data.navigation.map(({name, to, icon, show}) => {return {name, to, icon, show}});
store.commit("setNavigation", navigation); store.commit("setNavigation", navigation);
// set api config in store // set api config in store
store.commit("setAPI", results.data.api); store.commit("setAPI", results.data.api);
// add routes // add routes
let routes = []; let routes = [];
let rIdx = 0; let rIdx = 0;
results.data.navigation.forEach(({name, to, content, data}) => routes.push({ results.data.navigation.forEach(({name, to, content, data}) => routes.push({
name: name, name: name,
path: to, path: to,
meta: { meta: {
title: name title: name
}, },
component: Vue.component(name + rIdx++, { component: Vue.component(name + rIdx++, {
template: '<div id="' + name + rIdx + '">' + content + '</div>', template: '<div id="' + name + rIdx + '">' + content + '</div>',
components: Views, components: Views,
data: function(data) { data: function(data) {
if (typeof data != 'object') { if (typeof data != 'object') {
return () => { return {}; }; return () => { return {}; };
} }
return () => { return data; }; return () => { return data; };
}(data) }(data)
}) })
})); }));
router.addRoutes(routes); router.addRoutes(routes);
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
console.log(to); console.log(to);
document.title = (to.meta && to.meta.title) ? results.data.title + ': ' + to.meta.title : results.data.title; document.title = (to.meta && to.meta.title) ? results.data.title + ': ' + to.meta.title : results.data.title;
next(); next();
}); });
// load app, when init finishs // load app, when init finishs
new Vue({ new Vue({
el: '#vue-app', el: '#vue-app',
render: h => h(App), render: h => h(App),
router, router,
store store
}); });
}) })
.catch(error => { .catch(error => {
alert('error loading: ' + error.message); alert('error loading: ' + error.message);
}); });

View File

@ -47,6 +47,7 @@ module.exports = {
}, },
module: { module: {
loaders: [{ loaders: [{
enforce: "pre",
test: /\.(vue|js)$/, test: /\.(vue|js)$/,
loader: 'eslint-loader', loader: 'eslint-loader',
exclude: /node_modules/ exclude: /node_modules/