import Vue from 'vue'; import { debounce, isObject } from 'lodash'; import { mapState } from 'vuex'; import {AxiosError} from "axios"; export default Vue.component('navigation', { data: function () { return { loadingResults: false, searchActive: false, }; }, computed: { ...mapState('dashboard', ['servers']), searchTerm: { get: function (): string { return this.$store.getters['dashboard/getSearchTerm']; }, set: function (value: string): void { this.$store.dispatch('dashboard/setSearchTerm', value); } } }, created: function () { document.addEventListener('click', this.documentClick); }, beforeDestroy: function () { document.removeEventListener('click', this.documentClick); }, methods: { search: debounce(function (this: any): void { if (this.searchTerm.length >= 3) { this.loadingResults = true; this.gatherSearchResults(); } }, 500), gatherSearchResults: function (): void { this.$store.dispatch('dashboard/loadServers') .catch((err: AxiosError) => { console.error(err); const response = err.response; if (response && isObject(response.data.errors)) { response.data.errors.forEach((error: any) => { this.$flash.error(error.detail); }); } }) .then(() => { this.loadingResults = false; }); }, doLogout: function () { this.$store.commit('auth/logout'); window.location.assign(this.route('auth.logout')); }, documentClick: function (e: Event) { if (this.$refs.searchContainer) { if (this.$refs.searchContainer !== e.target && !(this.$refs.searchContainer as HTMLElement).contains(e.target as HTMLElement)) { this.searchActive = false; } } }, }, template: `
` })