Fix dashboard to track server state

This commit is contained in:
Dane Everitt 2018-07-15 17:53:40 -07:00
parent 8b3713e3ff
commit 7f5485d648
No known key found for this signature in database
GPG key ID: EEA66103B3D71F53
5 changed files with 95 additions and 31 deletions

View file

@ -7,7 +7,7 @@
<input type="text" <input type="text"
:placeholder="$t('dashboard.index.search')" :placeholder="$t('dashboard.index.search')"
@input="onChange" @input="onChange"
v-model="search" v-model="searchTerm"
ref="search" ref="search"
/> />
</div> </div>
@ -34,6 +34,8 @@
import Flash from '../Flash'; import Flash from '../Flash';
import ServerBox from './ServerBox'; import ServerBox from './ServerBox';
import Navigation from '../core/Navigation'; import Navigation from '../core/Navigation';
import isObject from 'lodash/isObject';
import {mapState} from 'vuex';
export default { export default {
name: 'dashboard', name: 'dashboard',
@ -42,17 +44,18 @@
return { return {
backgroundedAt: new Date(), backgroundedAt: new Date(),
documentVisible: true, documentVisible: true,
loading: true, loading: false,
search: '',
servers: [],
} }
}, },
/** /**
* Start loading the servers before the DOM $.el is created. * Start loading the servers before the DOM $.el is created. If we already have servers
* stored in vuex shows those and don't fire another API call just to load them again.
*/ */
created: function () { created: function () {
if (this.servers.length === 0) {
this.loadServers(); this.loadServers();
}
document.addEventListener('visibilitychange', () => { document.addEventListener('visibilitychange', () => {
this.documentVisible = document.visibilityState === 'visible'; this.documentVisible = document.visibilityState === 'visible';
@ -68,28 +71,29 @@
this._iterateServerResourceUse(); this._iterateServerResourceUse();
}, },
computed: {
...mapState('dashboard', ['servers']),
searchTerm: {
get: function () {
return this.$store.getters['dashboard/getSearchTerm'];
},
set: function (value) {
this.$store.dispatch('dashboard/setSearchTerm', value);
}
}
},
methods: { methods: {
/** /**
* Load the user's servers and render them onto the dashboard. * Load the user's servers and render them onto the dashboard.
*
* @param {string} query
*/ */
loadServers: function (query = '') { loadServers: function () {
this.loading = true; this.loading = true;
window.axios.get(this.route('api.client.index'), { this.$store.dispatch('dashboard/loadServers')
params: { query },
})
.finally(() => { .finally(() => {
this.clearFlashes(); this.clearFlashes();
}) })
.then(response => { .then(() => {
this.servers = [];
response.data.data.forEach(obj => {
const s = new Server(obj.attributes);
this.servers.push(s);
this.getResourceUse(s);
});
if (this.servers.length === 0) { if (this.servers.length === 0) {
this.info(this.$t('dashboard.index.no_matches')); this.info(this.$t('dashboard.index.no_matches'));
} }
@ -97,7 +101,7 @@
.catch(err => { .catch(err => {
console.error(err); console.error(err);
const response = err.response; const response = err.response;
if (response.data && _.isObject(response.data.errors)) { if (response.data && isObject(response.data.errors)) {
response.data.errors.forEach(error => { response.data.errors.forEach(error => {
this.error(error.detail); this.error(error.detail);
}); });
@ -113,7 +117,7 @@
* at the fastest. * at the fastest.
*/ */
onChange: debounce(function () { onChange: debounce(function () {
this.loadServers(this.$data.search); this.loadServers();
}, 500), }, 500),
/** /**

View file

@ -1,6 +1,6 @@
<template> <template>
<div class="server-box animate fadein"> <div class="server-box animate fadein">
<router-link :to="{ name: 'server', params: { serverID: server.identifier }}" class="content"> <router-link :to="{ name: 'server', params: { id: server.identifier }}" class="content">
<div class="float-right"> <div class="float-right">
<div class="indicator" :class="status"></div> <div class="indicator" :class="status"></div>
</div> </div>

View file

@ -46,7 +46,6 @@ const router = new VueRouter({
// have no JWT or the JWT is expired and wouldn't be accepted by the Panel. // have no JWT or the JWT is expired and wouldn't be accepted by the Panel.
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
if (to.path === route('auth.logout')) { if (to.path === route('auth.logout')) {
console.log('logging out');
return window.location = route('auth.logout'); return window.location = route('auth.logout');
} }
@ -54,13 +53,11 @@ router.beforeEach((to, from, next) => {
// Check that if we're accessing a non-auth route that a user exists on the page. // Check that if we're accessing a non-auth route that a user exists on the page.
if (!to.path.startsWith('/auth') && !(user instanceof User)) { if (!to.path.startsWith('/auth') && !(user instanceof User)) {
console.log('logging out 2');
store.commit('auth/logout'); store.commit('auth/logout');
return window.location = route('auth.logout'); return window.location = route('auth.logout');
} }
// Continue on through the pipeline. // Continue on through the pipeline.
console.log('continuing');
return next(); return next();
}); });

View file

@ -1,22 +1,22 @@
import Vue from 'vue'; import Vue from 'vue';
import Vuex from 'vuex'; import Vuex from 'vuex';
import server from "./modules/server"; import auth from './modules/auth';
import auth from "./modules/auth"; import dashboard from './modules/dashboard';
Vue.use(Vuex); Vue.use(Vuex);
const store = new Vuex.Store({ const store = new Vuex.Store({
strict: process.env.NODE_ENV !== 'production', strict: process.env.NODE_ENV !== 'production',
modules: { auth }, modules: { auth, dashboard },
}); });
if (module.hot) { if (module.hot) {
module.hot.accept(['./modules/auth'], () => { module.hot.accept(['./modules/auth'], () => {
const newAuthModule = require('./modules/auth').default; const newAuthModule = require('./modules/auth').default;
// const newServerModule = require('./modules/server').default; const newDashboardModule = require('./modules/dashboard').default;
store.hotUpdate({ store.hotUpdate({
modules: { newAuthModule }, modules: { newAuthModule, newDashboardModule },
}); });
}); });
} }

View file

@ -0,0 +1,63 @@
import Server from './../../models/server';
const route = require('./../../../../../vendor/tightenco/ziggy/src/js/route').default;
export default {
namespaced: true,
state: {
servers: [],
searchTerm: '',
},
getters: {
getSearchTerm: function (state) {
return state.searchTerm;
}
},
actions: {
/**
* Retrieve all of the servers for a user matching the query.
*
* @param commit
* @param {String} query
* @returns {Promise<any>}
*/
loadServers: ({commit, state}) => {
return new Promise((resolve, reject) => {
window.axios.get(route('api.client.index'), {
params: { query: state.searchTerm },
})
.then(response => {
// If there is a 302 redirect or some other odd behavior (basically, response that isnt
// in JSON format) throw an error and don't try to continue with the request processing.
if (!(response.data instanceof Object)) {
return reject(new Error('An error was encountered while processing this request.'));
}
// Remove all of the existing servers.
commit('clearServers');
response.data.data.forEach(obj => {
commit('addServer', obj.attributes);
});
resolve();
})
.catch(reject);
});
},
setSearchTerm: ({commit}, term) => {
commit('setSearchTerm', term);
},
},
mutations: {
addServer: function (state, data) {
state.servers.push(new Server(data));
},
clearServers: function (state) {
state.servers = [];
},
setSearchTerm: function (state, term) {
state.searchTerm = term;
},
},
};