misc_pterodactyl-panel/resources/assets/scripts/store/modules/dashboard.ts

67 lines
2.3 KiB
TypeScript
Raw Normal View History

2018-12-16 23:29:44 +00:00
import Server, {ServerData} from '../../models/server';
import {ActionContext} from "vuex";
2018-12-17 02:57:34 +00:00
import {DashboardState} from "../types";
2019-02-10 05:15:45 +00:00
2018-07-16 00:53:40 +00:00
const route = require('./../../../../../vendor/tightenco/ziggy/src/js/route').default;
export default {
namespaced: true,
state: {
servers: [],
searchTerm: '',
},
getters: {
2018-12-16 23:29:44 +00:00
getSearchTerm: function (state: DashboardState): string {
2018-07-16 00:53:40 +00:00
return state.searchTerm;
2018-12-16 23:29:44 +00:00
},
2018-07-16 00:53:40 +00:00
},
actions: {
/**
* Retrieve all of the servers for a user matching the query.
*/
2018-12-16 23:29:44 +00:00
loadServers: ({commit, state}: ActionContext<DashboardState, any>): Promise<void> => {
2018-07-16 00:53:40 +00:00
return new Promise((resolve, reject) => {
2018-12-16 23:29:44 +00:00
// @ts-ignore
2018-07-16 00:53:40 +00:00
window.axios.get(route('api.client.index'), {
2019-02-10 05:15:45 +00:00
params: {query: state.searchTerm},
2018-07-16 00:53:40 +00:00
})
2019-02-10 05:15:45 +00:00
// @ts-ignore
2018-07-16 00:53:40 +00:00
.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');
2018-12-16 23:29:44 +00:00
response.data.data.forEach((obj: { attributes: ServerData }) => {
2018-07-16 00:53:40 +00:00
commit('addServer', obj.attributes);
});
resolve();
})
.catch(reject);
});
},
2018-12-16 23:29:44 +00:00
setSearchTerm: ({commit}: ActionContext<DashboardState, any>, term: string) => {
2018-07-16 00:53:40 +00:00
commit('setSearchTerm', term);
},
},
mutations: {
2018-12-16 23:29:44 +00:00
addServer: function (state: DashboardState, data: ServerData) {
state.servers.push(
new Server(data)
);
2018-07-16 00:53:40 +00:00
},
2018-12-16 23:29:44 +00:00
clearServers: function (state: DashboardState) {
2018-07-16 00:53:40 +00:00
state.servers = [];
},
2018-12-16 23:29:44 +00:00
setSearchTerm: function (state: DashboardState, term: string) {
2018-07-16 00:53:40 +00:00
state.searchTerm = term;
},
},
};