Begin transfering things to TS
This commit is contained in:
parent
81f5e49768
commit
3ad4422a94
17 changed files with 280 additions and 138 deletions
|
@ -1,12 +1,19 @@
|
|||
import Vue from 'vue';
|
||||
import Vuex from 'vuex';
|
||||
import auth from './modules/auth';
|
||||
import dashboard from './modules/dashboard';
|
||||
import server from './modules/server';
|
||||
import socket from './modules/socket';
|
||||
import auth, {AuthenticationState} from './modules/auth';
|
||||
import dashboard, {DashboardState} from './modules/dashboard';
|
||||
import server, {ServerState} from './modules/server';
|
||||
import socket, {SocketState} from './modules/socket';
|
||||
|
||||
Vue.use(Vuex);
|
||||
|
||||
export type ApplicationState = {
|
||||
socket: SocketState,
|
||||
server: ServerState,
|
||||
auth: AuthenticationState,
|
||||
dashboard: DashboardState,
|
||||
}
|
||||
|
||||
const store = new Vuex.Store({
|
||||
strict: process.env.NODE_ENV !== 'production',
|
||||
modules: {auth, dashboard, server, socket},
|
|
@ -1,20 +1,35 @@
|
|||
import User from './../../models/user';
|
||||
import User, {UserData} from '../../models/user';
|
||||
import {ActionContext} from "vuex";
|
||||
|
||||
const route = require('./../../../../../vendor/tightenco/ziggy/src/js/route').default;
|
||||
|
||||
type LoginAction = {
|
||||
type: 'login',
|
||||
user: string,
|
||||
password: string,
|
||||
}
|
||||
|
||||
type UpdateEmailAction = {
|
||||
type: 'updateEmail',
|
||||
email: string,
|
||||
password: string,
|
||||
}
|
||||
|
||||
export type AuthenticationState = {
|
||||
user: null | User,
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
// @ts-ignore
|
||||
user: typeof window.PterodactylUser === 'object' ? new User(window.PterodactylUser) : null,
|
||||
},
|
||||
getters: {
|
||||
/**
|
||||
* Return the currently authenticated user.
|
||||
*
|
||||
* @param state
|
||||
* @returns {User|null}
|
||||
*/
|
||||
getUser: function (state) {
|
||||
getUser: function (state: AuthenticationState): null | User {
|
||||
return state.user;
|
||||
},
|
||||
},
|
||||
|
@ -22,15 +37,16 @@ export default {
|
|||
actions: {
|
||||
/**
|
||||
* Log a user into the Panel.
|
||||
*
|
||||
* @param commit
|
||||
* @param {String} user
|
||||
* @param {String} password
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
login: ({commit}, {user, password}) => {
|
||||
login: ({commit}: ActionContext<AuthenticationState, any>, {user, password}: LoginAction): Promise<{
|
||||
complete: boolean,
|
||||
intended?: string,
|
||||
token?: string,
|
||||
}> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore
|
||||
window.axios.post(route('auth.login'), {user, password})
|
||||
// @ts-ignore
|
||||
.then(response => {
|
||||
commit('logout');
|
||||
|
||||
|
@ -59,15 +75,12 @@ export default {
|
|||
|
||||
/**
|
||||
* Update a user's email address on the Panel and store the updated result in Vuex.
|
||||
*
|
||||
* @param commit
|
||||
* @param {String} email
|
||||
* @param {String} password
|
||||
* @return {Promise<any>}
|
||||
*/
|
||||
updateEmail: function ({commit}, {email, password}) {
|
||||
updateEmail: function ({commit}: ActionContext<AuthenticationState, any>, {email, password}: UpdateEmailAction): Promise<void> {
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore
|
||||
window.axios.put(route('api.client.account.update-email'), {email, password})
|
||||
// @ts-ignore
|
||||
.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 login.
|
||||
|
@ -83,13 +96,15 @@ export default {
|
|||
},
|
||||
},
|
||||
mutations: {
|
||||
setEmail: function (state, email) {
|
||||
state.user.email = email;
|
||||
setEmail: function (state: AuthenticationState, email: string) {
|
||||
if (state.user) {
|
||||
state.user.email = email;
|
||||
}
|
||||
},
|
||||
login: function (state, data) {
|
||||
login: function (state: AuthenticationState, data: UserData) {
|
||||
state.user = new User(data);
|
||||
},
|
||||
logout: function (state) {
|
||||
logout: function (state: AuthenticationState) {
|
||||
state.user = null;
|
||||
},
|
||||
},
|
|
@ -1,6 +1,12 @@
|
|||
import Server from './../../models/server';
|
||||
import Server, {ServerData} from '../../models/server';
|
||||
import {ActionContext} from "vuex";
|
||||
const route = require('./../../../../../vendor/tightenco/ziggy/src/js/route').default;
|
||||
|
||||
export type DashboardState = {
|
||||
searchTerm: string,
|
||||
servers: Array<Server>,
|
||||
};
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
state: {
|
||||
|
@ -8,23 +14,21 @@ export default {
|
|||
searchTerm: '',
|
||||
},
|
||||
getters: {
|
||||
getSearchTerm: function (state) {
|
||||
getSearchTerm: function (state: DashboardState): string {
|
||||
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}) => {
|
||||
loadServers: ({commit, state}: ActionContext<DashboardState, any>): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore
|
||||
window.axios.get(route('api.client.index'), {
|
||||
params: { query: state.searchTerm },
|
||||
})
|
||||
// @ts-ignore
|
||||
.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.
|
||||
|
@ -35,7 +39,7 @@ export default {
|
|||
// Remove all of the existing servers.
|
||||
commit('clearServers');
|
||||
|
||||
response.data.data.forEach(obj => {
|
||||
response.data.data.forEach((obj: { attributes: ServerData }) => {
|
||||
commit('addServer', obj.attributes);
|
||||
});
|
||||
|
||||
|
@ -45,18 +49,20 @@ export default {
|
|||
});
|
||||
},
|
||||
|
||||
setSearchTerm: ({commit}, term) => {
|
||||
setSearchTerm: ({commit}: ActionContext<DashboardState, any>, term: string) => {
|
||||
commit('setSearchTerm', term);
|
||||
},
|
||||
},
|
||||
mutations: {
|
||||
addServer: function (state, data) {
|
||||
state.servers.push(new Server(data));
|
||||
addServer: function (state: DashboardState, data: ServerData) {
|
||||
state.servers.push(
|
||||
new Server(data)
|
||||
);
|
||||
},
|
||||
clearServers: function (state) {
|
||||
clearServers: function (state: DashboardState) {
|
||||
state.servers = [];
|
||||
},
|
||||
setSearchTerm: function (state, term) {
|
||||
setSearchTerm: function (state: DashboardState, term: string) {
|
||||
state.searchTerm = term;
|
||||
},
|
||||
},
|
|
@ -1,4 +1,18 @@
|
|||
// @ts-ignore
|
||||
import route from '../../../../../vendor/tightenco/ziggy/src/js/route';
|
||||
import {ActionContext} from "vuex";
|
||||
import {ServerData} from "../../models/server";
|
||||
|
||||
type ServerApplicationCredentials = {
|
||||
node: string,
|
||||
key: string,
|
||||
};
|
||||
|
||||
export type ServerState = {
|
||||
server: ServerData,
|
||||
credentials: ServerApplicationCredentials,
|
||||
console: Array<string>,
|
||||
};
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
|
@ -11,14 +25,13 @@ export default {
|
|||
},
|
||||
actions: {
|
||||
/**
|
||||
*
|
||||
* @param commit
|
||||
* @param {String} server
|
||||
* @returns {Promise<any>}
|
||||
* Fetches the active server from the API and stores it in vuex.
|
||||
*/
|
||||
getServer: ({commit}, {server}) => {
|
||||
getServer: ({commit}: ActionContext<ServerState, any>, {server}: { server: string }): Promise<void> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore
|
||||
window.axios.get(route('api.client.servers.view', { server }))
|
||||
// @ts-ignore
|
||||
.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 login.
|
||||
|
@ -39,14 +52,12 @@ export default {
|
|||
/**
|
||||
* Get authentication credentials that the client should use when connecting to the daemon to
|
||||
* retrieve server information.
|
||||
*
|
||||
* @param commit
|
||||
* @param {String} server
|
||||
* @returns {Promise<any>}
|
||||
*/
|
||||
getCredentials: ({commit}, {server}) => {
|
||||
getCredentials: ({commit}: ActionContext<ServerState, any>, {server}: { server: string }) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
// @ts-ignore
|
||||
window.axios.get(route('server.credentials', {server}))
|
||||
// @ts-ignore
|
||||
.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 login.
|
||||
|
@ -65,13 +76,13 @@ export default {
|
|||
},
|
||||
},
|
||||
mutations: {
|
||||
SERVER_DATA: function (state, data) {
|
||||
SERVER_DATA: function (state: ServerState, data: ServerData) {
|
||||
state.server = data;
|
||||
},
|
||||
SERVER_CREDENTIALS: function (state, credentials) {
|
||||
SERVER_CREDENTIALS: function (state: ServerState, credentials: ServerApplicationCredentials) {
|
||||
state.credentials = credentials;
|
||||
},
|
||||
CONSOLE_DATA: function (state, data) {
|
||||
CONSOLE_DATA: function (state: ServerState, data: string) {
|
||||
state.console.push(data);
|
||||
},
|
||||
},
|
|
@ -1,4 +1,10 @@
|
|||
import Status from './../../helpers/statuses';
|
||||
import Status from '../../helpers/statuses';
|
||||
|
||||
export type SocketState = {
|
||||
connected: boolean,
|
||||
connectionError: boolean | Error,
|
||||
status: number,
|
||||
}
|
||||
|
||||
export default {
|
||||
namespaced: true,
|
||||
|
@ -7,29 +13,27 @@ export default {
|
|||
connectionError: false,
|
||||
status: Status.STATUS_OFF,
|
||||
},
|
||||
actions: {
|
||||
},
|
||||
mutations: {
|
||||
SOCKET_CONNECT: (state) => {
|
||||
SOCKET_CONNECT: (state: SocketState) => {
|
||||
state.connected = true;
|
||||
state.connectionError = false;
|
||||
},
|
||||
|
||||
SOCKET_ERROR: (state, err) => {
|
||||
SOCKET_ERROR: (state: SocketState, err : Error) => {
|
||||
state.connected = false;
|
||||
state.connectionError = err;
|
||||
},
|
||||
|
||||
SOCKET_CONNECT_ERROR: (state, err) => {
|
||||
SOCKET_CONNECT_ERROR: (state: SocketState, err : Error) => {
|
||||
state.connected = false;
|
||||
state.connectionError = err;
|
||||
},
|
||||
|
||||
'SOCKET_INITIAL STATUS': (state, data) => {
|
||||
'SOCKET_INITIAL STATUS': (state: SocketState, data: { status: number }) => {
|
||||
state.status = data.status;
|
||||
},
|
||||
|
||||
SOCKET_STATUS: (state, data) => {
|
||||
SOCKET_STATUS: (state: SocketState, data: { status: number }) => {
|
||||
state.status = data.status;
|
||||
}
|
||||
},
|
Loading…
Add table
Add a link
Reference in a new issue