|
| 1 | +import Vue from 'vue'; |
| 2 | +import { debounce, isObject } from 'lodash'; |
| 3 | +import { mapState } from 'vuex'; |
| 4 | +import Flash from "./../Flash"; |
| 5 | +import Navigation from "./../core/Navigation"; |
| 6 | +import {AxiosError} from "axios"; |
| 7 | +import ServerBox from "./ServerBox"; |
| 8 | + |
| 9 | +type DataStructure = { |
| 10 | + backgroundedAt: Date, |
| 11 | + documentVisible: boolean, |
| 12 | + loading: boolean, |
| 13 | + servers?: Array<any>, |
| 14 | + searchTerm?: string, |
| 15 | +} |
| 16 | + |
| 17 | +export default Vue.component('dashboard', { |
| 18 | + components: { |
| 19 | + ServerBox, |
| 20 | + Navigation, |
| 21 | + Flash |
| 22 | + }, |
| 23 | + |
| 24 | + data: function (): DataStructure { |
| 25 | + return { |
| 26 | + backgroundedAt: new Date(), |
| 27 | + documentVisible: true, |
| 28 | + loading: false, |
| 29 | + } |
| 30 | + }, |
| 31 | + |
| 32 | + /** |
| 33 | + * Start loading the servers before the DOM $.el is created. If we already have servers |
| 34 | + * stored in vuex shows those and don't fire another API call just to load them again. |
| 35 | + */ |
| 36 | + created: function () { |
| 37 | + if (!this.servers || this.servers.length === 0) { |
| 38 | + this.loadServers(); |
| 39 | + } |
| 40 | + }, |
| 41 | + |
| 42 | + /** |
| 43 | + * Once the page is mounted set a function to run every 10 seconds that will |
| 44 | + * iterate through the visible servers and fetch their resource usage. |
| 45 | + */ |
| 46 | + mounted: function () { |
| 47 | + (this.$refs.search as HTMLElement).focus(); |
| 48 | + }, |
| 49 | + |
| 50 | + computed: { |
| 51 | + ...mapState('dashboard', ['servers']), |
| 52 | + searchTerm: { |
| 53 | + get: function (): string { |
| 54 | + return this.$store.getters['dashboard/getSearchTerm']; |
| 55 | + }, |
| 56 | + set: function (value: string): void { |
| 57 | + this.$store.dispatch('dashboard/setSearchTerm', value); |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + |
| 62 | + methods: { |
| 63 | + /** |
| 64 | + * Load the user's servers and render them onto the dashboard. |
| 65 | + */ |
| 66 | + loadServers: function () { |
| 67 | + this.loading = true; |
| 68 | + this.$flash.clear(); |
| 69 | + |
| 70 | + this.$store.dispatch('dashboard/loadServers') |
| 71 | + .then(() => { |
| 72 | + if (!this.servers || this.servers.length === 0) { |
| 73 | + this.$flash.info(this.$t('dashboard.index.no_matches')); |
| 74 | + } |
| 75 | + }) |
| 76 | + .catch((err: AxiosError) => { |
| 77 | + console.error(err); |
| 78 | + const response = err.response; |
| 79 | + if (response && isObject(response.data.errors)) { |
| 80 | + response.data.errors.forEach((error: any) => { |
| 81 | + this.$flash.error(error.detail); |
| 82 | + }); |
| 83 | + } |
| 84 | + }) |
| 85 | + .then(() => this.loading = false); |
| 86 | + }, |
| 87 | + |
| 88 | + /** |
| 89 | + * Handle a search for servers but only call the search function every 500ms |
| 90 | + * at the fastest. |
| 91 | + */ |
| 92 | + onChange: debounce(function (this: any): void { |
| 93 | + this.loadServers(); |
| 94 | + }, 500), |
| 95 | + }, |
| 96 | + |
| 97 | + template: ` |
| 98 | + <div> |
| 99 | + <navigation/> |
| 100 | + <div class="container"> |
| 101 | + <flash container="mt-4"/> |
| 102 | + <div class="server-search animate fadein"> |
| 103 | + <input type="text" |
| 104 | + :placeholder="$t('dashboard.index.search')" |
| 105 | + @input="onChange" |
| 106 | + v-model="searchTerm" |
| 107 | + ref="search" |
| 108 | + /> |
| 109 | + </div> |
| 110 | + <div v-if="this.loading" class="my-4 animate fadein"> |
| 111 | + <div class="text-center h-16"> |
| 112 | + <span class="spinner spinner-xl spinner-thick blue"></span> |
| 113 | + </div> |
| 114 | + </div> |
| 115 | + <transition-group class="flex flex-wrap justify-center sm:justify-start" v-else> |
| 116 | + <server-box |
| 117 | + v-for="(server, index) in servers" |
| 118 | + :key="index" |
| 119 | + :server="server" |
| 120 | + /> |
| 121 | + </transition-group> |
| 122 | + </div> |
| 123 | + </div> |
| 124 | + ` |
| 125 | +}); |
0 commit comments