import Vue from 'vue'; import Navigation from '@/components/core/Navigation'; import ProgressBar from './components/ProgressBar'; import { mapState } from 'vuex'; import * as io from 'socket.io-client'; import { Socketio } from "@/mixins/socketio"; import Icon from "@/components/core/Icon"; import PowerButtons from "@/components/server/components/PowerButtons"; export default Vue.component('server', { components: { ProgressBar, PowerButtons, Navigation, Icon }, computed: { ...mapState('server', ['server', 'credentials']), ...mapState('socket', ['connected', 'connectionError']), }, mixins: [ Socketio ], // Watch for route changes that occur with different server parameters. This occurs when a user // uses the search bar. Because of the way vue-router works, it won't re-mount the server component // so we will end up seeing the wrong server data if we don't perform this watch. watch: { '$route': function (toRoute, fromRoute) { if (toRoute.params.id !== fromRoute.params.id) { this.loadingServerData = true; this.loadServer(); } } }, data: function () { return { loadingServerData: true, }; }, mounted: function () { this.loadServer(); }, beforeDestroy: function () { this.removeSocket(); }, methods: { /** * Load the core server information needed for these pages to be functional. */ loadServer: function () { Promise.all([ this.$store.dispatch('server/getServer', {server: this.$route.params.id}), this.$store.dispatch('server/getCredentials', {server: this.$route.params.id}) ]) .then(() => { // Configure the socket.io implementation. This is a really ghetto way of handling things // but all of these plugins assume you have some constant connection, which we don't. const socket = io(`${this.credentials.node}/v1/ws/${this.server.uuid}`, { query: `token=${this.credentials.key}`, }); this.$socket().connect(socket); this.loadingServerData = false; }) .catch(err => { console.error('There was an error performing Server::loadServer', { err }); }); }, }, template: `