Skip to content

Commit 7f5485d

Browse files
committed
Fix dashboard to track server state
1 parent 8b3713e commit 7f5485d

File tree

5 files changed

+95
-31
lines changed

5 files changed

+95
-31
lines changed

resources/assets/scripts/components/dashboard/Dashboard.vue

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<input type="text"
88
:placeholder="$t('dashboard.index.search')"
99
@input="onChange"
10-
v-model="search"
10+
v-model="searchTerm"
1111
ref="search"
1212
/>
1313
</div>
@@ -34,6 +34,8 @@
3434
import Flash from '../Flash';
3535
import ServerBox from './ServerBox';
3636
import Navigation from '../core/Navigation';
37+
import isObject from 'lodash/isObject';
38+
import {mapState} from 'vuex';
3739
3840
export default {
3941
name: 'dashboard',
@@ -42,17 +44,18 @@
4244
return {
4345
backgroundedAt: new Date(),
4446
documentVisible: true,
45-
loading: true,
46-
search: '',
47-
servers: [],
47+
loading: false,
4848
}
4949
},
5050
5151
/**
52-
* Start loading the servers before the DOM $.el is created.
52+
* Start loading the servers before the DOM $.el is created. If we already have servers
53+
* stored in vuex shows those and don't fire another API call just to load them again.
5354
*/
5455
created: function () {
55-
this.loadServers();
56+
if (this.servers.length === 0) {
57+
this.loadServers();
58+
}
5659
5760
document.addEventListener('visibilitychange', () => {
5861
this.documentVisible = document.visibilityState === 'visible';
@@ -68,36 +71,37 @@
6871
this._iterateServerResourceUse();
6972
},
7073
74+
computed: {
75+
...mapState('dashboard', ['servers']),
76+
searchTerm: {
77+
get: function () {
78+
return this.$store.getters['dashboard/getSearchTerm'];
79+
},
80+
set: function (value) {
81+
this.$store.dispatch('dashboard/setSearchTerm', value);
82+
}
83+
}
84+
},
85+
7186
methods: {
7287
/**
7388
* Load the user's servers and render them onto the dashboard.
74-
*
75-
* @param {string} query
7689
*/
77-
loadServers: function (query = '') {
90+
loadServers: function () {
7891
this.loading = true;
79-
window.axios.get(this.route('api.client.index'), {
80-
params: { query },
81-
})
92+
this.$store.dispatch('dashboard/loadServers')
8293
.finally(() => {
8394
this.clearFlashes();
8495
})
85-
.then(response => {
86-
this.servers = [];
87-
response.data.data.forEach(obj => {
88-
const s = new Server(obj.attributes);
89-
this.servers.push(s);
90-
this.getResourceUse(s);
91-
});
92-
96+
.then(() => {
9397
if (this.servers.length === 0) {
9498
this.info(this.$t('dashboard.index.no_matches'));
9599
}
96100
})
97101
.catch(err => {
98102
console.error(err);
99103
const response = err.response;
100-
if (response.data && _.isObject(response.data.errors)) {
104+
if (response.data && isObject(response.data.errors)) {
101105
response.data.errors.forEach(error => {
102106
this.error(error.detail);
103107
});
@@ -113,7 +117,7 @@
113117
* at the fastest.
114118
*/
115119
onChange: debounce(function () {
116-
this.loadServers(this.$data.search);
120+
this.loadServers();
117121
}, 500),
118122
119123
/**

resources/assets/scripts/components/dashboard/ServerBox.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<template>
22
<div class="server-box animate fadein">
3-
<router-link :to="{ name: 'server', params: { serverID: server.identifier }}" class="content">
3+
<router-link :to="{ name: 'server', params: { id: server.identifier }}" class="content">
44
<div class="float-right">
55
<div class="indicator" :class="status"></div>
66
</div>

resources/assets/scripts/router.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,21 +46,18 @@ const router = new VueRouter({
4646
// have no JWT or the JWT is expired and wouldn't be accepted by the Panel.
4747
router.beforeEach((to, from, next) => {
4848
if (to.path === route('auth.logout')) {
49-
console.log('logging out');
5049
return window.location = route('auth.logout');
5150
}
5251

5352
const user = store.getters['auth/getUser'];
5453

5554
// Check that if we're accessing a non-auth route that a user exists on the page.
5655
if (!to.path.startsWith('/auth') && !(user instanceof User)) {
57-
console.log('logging out 2');
5856
store.commit('auth/logout');
5957
return window.location = route('auth.logout');
6058
}
6159

6260
// Continue on through the pipeline.
63-
console.log('continuing');
6461
return next();
6562
});
6663

resources/assets/scripts/store/index.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
import Vue from 'vue';
22
import Vuex from 'vuex';
3-
import server from "./modules/server";
4-
import auth from "./modules/auth";
3+
import auth from './modules/auth';
4+
import dashboard from './modules/dashboard';
55

66
Vue.use(Vuex);
77

88
const store = new Vuex.Store({
99
strict: process.env.NODE_ENV !== 'production',
10-
modules: { auth },
10+
modules: { auth, dashboard },
1111
});
1212

1313
if (module.hot) {
1414
module.hot.accept(['./modules/auth'], () => {
1515
const newAuthModule = require('./modules/auth').default;
16-
// const newServerModule = require('./modules/server').default;
16+
const newDashboardModule = require('./modules/dashboard').default;
1717

1818
store.hotUpdate({
19-
modules: { newAuthModule },
19+
modules: { newAuthModule, newDashboardModule },
2020
});
2121
});
2222
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import Server from './../../models/server';
2+
const route = require('./../../../../../vendor/tightenco/ziggy/src/js/route').default;
3+
4+
export default {
5+
namespaced: true,
6+
state: {
7+
servers: [],
8+
searchTerm: '',
9+
},
10+
getters: {
11+
getSearchTerm: function (state) {
12+
return state.searchTerm;
13+
}
14+
},
15+
actions: {
16+
/**
17+
* Retrieve all of the servers for a user matching the query.
18+
*
19+
* @param commit
20+
* @param {String} query
21+
* @returns {Promise<any>}
22+
*/
23+
loadServers: ({commit, state}) => {
24+
return new Promise((resolve, reject) => {
25+
window.axios.get(route('api.client.index'), {
26+
params: { query: state.searchTerm },
27+
})
28+
.then(response => {
29+
// If there is a 302 redirect or some other odd behavior (basically, response that isnt
30+
// in JSON format) throw an error and don't try to continue with the request processing.
31+
if (!(response.data instanceof Object)) {
32+
return reject(new Error('An error was encountered while processing this request.'));
33+
}
34+
35+
// Remove all of the existing servers.
36+
commit('clearServers');
37+
38+
response.data.data.forEach(obj => {
39+
commit('addServer', obj.attributes);
40+
});
41+
42+
resolve();
43+
})
44+
.catch(reject);
45+
});
46+
},
47+
48+
setSearchTerm: ({commit}, term) => {
49+
commit('setSearchTerm', term);
50+
},
51+
},
52+
mutations: {
53+
addServer: function (state, data) {
54+
state.servers.push(new Server(data));
55+
},
56+
clearServers: function (state) {
57+
state.servers = [];
58+
},
59+
setSearchTerm: function (state, term) {
60+
state.searchTerm = term;
61+
},
62+
},
63+
};

0 commit comments

Comments
 (0)