2017-08-24 11:16:07 +02:00
|
|
|
import Vue from 'vue';
|
|
|
|
import Vuex from 'vuex';
|
|
|
|
import VueRouter from 'vue-router';
|
2017-08-24 13:23:03 +02:00
|
|
|
import Axios from 'axios';
|
|
|
|
|
|
|
|
import App from './app.vue';
|
2017-08-24 11:16:07 +02:00
|
|
|
import Dashboard from './components/dashboard.vue';
|
2017-08-24 15:55:44 +02:00
|
|
|
import LoginForm from './components/forms/login.vue';
|
2017-08-24 11:16:07 +02:00
|
|
|
import Userlist from './components/userlist.vue';
|
|
|
|
|
|
|
|
Vue.use(VueRouter);
|
2017-08-24 13:23:03 +02:00
|
|
|
Vue.use(Vuex);
|
2017-08-24 11:16:07 +02:00
|
|
|
|
|
|
|
const store = new Vuex.Store({
|
|
|
|
state: {
|
2017-08-24 15:55:44 +02:00
|
|
|
api: {},
|
|
|
|
navigation: [],
|
|
|
|
login: {},
|
|
|
|
authToken: '',
|
|
|
|
credentials: {}
|
2017-08-24 11:16:07 +02:00
|
|
|
},
|
|
|
|
mutations: {
|
2017-08-24 15:55:44 +02:00
|
|
|
setAPI(state, payload) {
|
|
|
|
state.api = payload;
|
|
|
|
},
|
|
|
|
setNavigation(state, payload) {
|
|
|
|
for (var i=0; i<payload.length; i++) {
|
|
|
|
state.navigation.push(payload[i]); // = payload;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setCredentials(state, payload) {
|
|
|
|
state.credentials = {
|
|
|
|
username: payload.username,
|
|
|
|
password: payload.password
|
|
|
|
}
|
|
|
|
},
|
|
|
|
setLogin(state, payload) {
|
|
|
|
state.login = payload.User;
|
|
|
|
state.authToken = payload.AuthToken;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
actions: {
|
|
|
|
apiRequest(context, payload) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
|
|
|
|
Axios({
|
|
|
|
method: payload.method ? payload.method : 'get',
|
2017-08-24 19:28:53 +02:00
|
|
|
baseURL: context.state.api.baseURL,
|
|
|
|
url: payload.endpoint,
|
|
|
|
params: payload.params,
|
2017-08-24 15:55:44 +02:00
|
|
|
data: payload.data,
|
|
|
|
headers: {
|
|
|
|
'X-Auth-Token': context.state.authToken
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
console.log(response);
|
|
|
|
resolve(response.data);
|
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
console.dir(error);
|
|
|
|
reject(error);
|
|
|
|
})
|
|
|
|
});
|
|
|
|
},
|
|
|
|
login(context, payload) {
|
|
|
|
context.commit('setCredentials', payload);
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
context.dispatch('apiRequest', {
|
|
|
|
method: 'post',
|
|
|
|
endpoint: 'login',
|
|
|
|
data: payload
|
|
|
|
})
|
|
|
|
.then(data => {
|
|
|
|
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([]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
2017-08-24 11:16:07 +02:00
|
|
|
}
|
2017-08-24 15:55:44 +02:00
|
|
|
});
|
|
|
|
|
2017-08-24 11:16:07 +02:00
|
|
|
|
|
|
|
const routes = [
|
2017-08-24 15:55:44 +02:00
|
|
|
{
|
|
|
|
path: '/login',
|
|
|
|
component: LoginForm
|
|
|
|
},
|
2017-08-24 11:16:07 +02:00
|
|
|
{
|
|
|
|
path: '/',
|
|
|
|
component: Dashboard
|
|
|
|
},
|
|
|
|
{
|
|
|
|
path: '/userlist',
|
|
|
|
component: Userlist
|
|
|
|
}
|
|
|
|
];
|
|
|
|
|
|
|
|
const router = new VueRouter({
|
|
|
|
routes
|
|
|
|
});
|
|
|
|
|
2017-08-24 15:55:44 +02:00
|
|
|
// load init
|
|
|
|
Axios.get('conf/init.json')
|
|
|
|
.then(results => {
|
|
|
|
// set navigation
|
|
|
|
if ( !Array.isArray(results.data.navigation) ) {
|
|
|
|
alert('invalid data in: ' + this.src);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
store.commit("setNavigation", results.data.navigation);
|
|
|
|
|
|
|
|
// set api config in store
|
|
|
|
store.commit("setAPI", results.data.api);
|
|
|
|
|
|
|
|
// load app, when init finishs
|
|
|
|
new Vue({
|
|
|
|
el: '#vue-app',
|
|
|
|
components: {
|
|
|
|
App
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
navItems: store.state.navigation
|
|
|
|
},
|
|
|
|
router,
|
|
|
|
store
|
2017-08-24 13:23:03 +02:00
|
|
|
});
|
2017-08-24 15:55:44 +02:00
|
|
|
})
|
|
|
|
.catch(error => {
|
|
|
|
alert('error loading: ' + error.message);
|
|
|
|
});
|