Skip to content

Commit 201c8a7

Browse files
committed
Make search work correctly when clicking on results
1 parent 31092df commit 201c8a7

File tree

2 files changed

+36
-17
lines changed

2 files changed

+36
-17
lines changed

resources/assets/scripts/components/core/Navigation.vue

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@
55
Pterodactyl
66
</router-link>
77
</div>
8-
<div class="search-box flex-none" v-if="$route.name !== 'dashboard'">
8+
<div class="search-box flex-none" v-if="$route.name !== 'dashboard'" ref="searchContainer">
99
<input type="text" class="search-input" id="searchInput" placeholder="Search..."
10-
:class="{ 'has-search-results': (servers.length > 0 || loadingResults) && searchActive }"
10+
:class="{ 'has-search-results': ((servers.length > 0 && searchTerm.length >= 3) || loadingResults) && searchActive }"
1111
v-on:focus="searchActive = true"
12-
v-on:blur="searchActive = false"
1312
v-on:input="search"
1413
v-model="searchTerm"
1514
/>
16-
<div class="search-results select-none" :class="{ 'hidden': (servers.length === 0 && !loadingResults) || !searchActive }">
15+
<div class="search-results select-none" :class="{ 'hidden': (servers.length === 0 && !loadingResults) || !searchActive || searchTerm.length < 3 }">
1716
<div v-if="loadingResults">
1817
<a href="#">
1918
<div class="flex items-center">
@@ -26,12 +25,8 @@
2625
</div>
2726
</a>
2827
</div>
29-
<div v-else>
30-
<router-link
31-
v-for="server in servers"
32-
:key="server.identifier"
33-
:to="{ name: 'server', params: { id: server.identifier } }"
34-
>
28+
<div v-else v-for="server in servers" :key="server.identifier">
29+
<router-link :to="{ name: 'server', params: { id: server.identifier }}" v-on:click.native="searchActive = false">
3530
<div class="flex items-center">
3631
<div class="flex-1">
3732
<span class="font-bold text-grey-darkest">{{ server.name }}</span><br />
@@ -100,20 +95,24 @@
10095
}
10196
},
10297
98+
created: function () {
99+
document.addEventListener('click', this.documentClick);
100+
},
101+
102+
beforeDestroy: function () {
103+
document.removeEventListener('click', this.documentClick);
104+
},
105+
103106
methods: {
104107
search: debounce(function () {
105-
if (this.searchTerm.length > 3) {
108+
if (this.searchTerm.length >= 3) {
106109
this.loadingResults = true;
107110
this.gatherSearchResults(this.searchTerm);
108111
}
109112
}, 500),
110113
111114
gatherSearchResults: function () {
112115
this.$store.dispatch('dashboard/loadServers')
113-
.then(() => {
114-
if (this.servers.length === 0) {
115-
}
116-
})
117116
.catch(err => {
118117
console.error(err);
119118
const response = err.response;
@@ -132,6 +131,14 @@
132131
this.$store.commit('auth/logout');
133132
return window.location = this.route('auth.logout');
134133
},
134+
135+
documentClick: function (e) {
136+
if (this.$refs.searchContainer) {
137+
if (this.$refs.searchContainer !== e.target && !this.$refs.searchContainer.contains(e.target)) {
138+
this.searchActive = false;
139+
}
140+
}
141+
},
135142
}
136143
};
137144
</script>

resources/assets/scripts/components/server/Server.vue

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</div>
1919
</div>
2020
<div class="mt-6 sidenav mr-6 bg-white border rounded">
21-
<router-link :to="{ name: 'server', params: { id: this.$route.params.id } }">
21+
<router-link :to="{ name: 'server', params: { id: $route.params.id } }">
2222
<terminal-icon class="h-4"></terminal-icon> Console
2323
</router-link>
2424
<router-link :to="{ name: 'server-files' }">
@@ -51,7 +51,7 @@
5151
</div>
5252
</div>
5353
-->
54-
<router-view></router-view>
54+
<router-view :key="server.identifier"></router-view>
5555
</div>
5656
</div>
5757
</div>
@@ -87,6 +87,18 @@
8787
...mapState('socket', ['connected', 'connectionError']),
8888
},
8989
90+
// Watch for route changes that occur with different server parameters. This occurs when a user
91+
// uses the search bar. Because of the way vue-router works, it won't re-mount the server component
92+
// so we will end up seeing the wrong server data if we don't perform this watch.
93+
watch: {
94+
'$route': function (toRoute, fromRoute) {
95+
if (toRoute.params.id !== fromRoute.params.id) {
96+
this.loadingServerData = true;
97+
this.loadServer();
98+
}
99+
}
100+
},
101+
90102
data: function () {
91103
return {
92104
loadingServerData: true,

0 commit comments

Comments
 (0)