extract the search box into its own component

This commit is contained in:
j3d1 2024-11-05 23:36:05 +01:00
parent 6e38ff7ac7
commit 55cef1128e
3 changed files with 59 additions and 14 deletions

View file

@ -29,16 +29,7 @@
</router-link> </router-link>
</li> </li>
</ul> </ul>
<form class="form-inline mt-1 my-lg-auto my-xl-auto w-100 d-inline mr-1" v-if="hasPermissions"> <SearchBox v-if="hasPermissions" class="mt-1 my-lg-auto my-xl-auto w-100 d-inline mr-1"/>
<input
class="form-control w-100"
type="search"
placeholder="Search"
aria-label="Search"
@input="searchEventItems($event.target.value)"
disabled
>
</form>
<div class="custom-control-inline mr-1" v-if="hasPermissions"> <div class="custom-control-inline mr-1" v-if="hasPermissions">
<div class="btn-group btn-group-toggle mr-1" v-if="isItemView()"> <div class="btn-group btn-group-toggle mr-1" v-if="isItemView()">
<button :class="['btn', 'btn-info', { active: layout === 'cards' }]" @click="setLayout('cards')"> <button :class="['btn', 'btn-info', { active: layout === 'cards' }]" @click="setLayout('cards')">
@ -103,9 +94,13 @@
<script> <script>
import {mapState, mapActions, mapMutations, mapGetters} from 'vuex'; import {mapState, mapActions, mapMutations, mapGetters} from 'vuex';
import SearchBox from "@/components/inputs/SearchBox.vue";
export default { export default {
name: 'Navbar', name: 'Navbar',
components: {
SearchBox
},
data: () => ({ data: () => ({
views: [ views: [
{'title': 'items', 'path': 'items'}, {'title': 'items', 'path': 'items'},
@ -122,7 +117,7 @@ export default {
...mapGetters(['getEventSlug', 'getActiveView', "checkPermission", "hasPermissions", "layout", "route"]), ...mapGetters(['getEventSlug', 'getActiveView', "checkPermission", "hasPermissions", "layout", "route"]),
}, },
methods: { methods: {
...mapActions(['changeEvent', 'changeView', 'searchEventItems']), ...mapActions(['changeEvent', 'changeView']),
...mapMutations(['logout']), ...mapMutations(['logout']),
navigateTo(link) { navigateTo(link) {
if (this.route.path !== link) if (this.route.path !== link)

View file

@ -0,0 +1,43 @@
<template>
<input
class="form-control w-100"
type="search"
placeholder="Search"
aria-label="Search"
v-model="search_query"
@keyup.enter="dispatchSearch"
>
</template>
<script>
import {mapActions, mapGetters} from "vuex";
export default {
name: 'SearchBox',
data() {
return {
search_query: ''
}
},
computed: {
...mapGetters(['getActiveView'])
},
methods: {
...mapActions(['searchEventItems', 'searchEventTickets']),
isItemView() {
return this.getActiveView === 'items' || this.getActiveView === 'item';
},
isTicketView() {
return this.getActiveView === 'tickets' || this.getActiveView === 'ticket';
},
dispatchSearch() {
if (this.isItemView()) {
this.searchEventItems(this.search_query);
} else if (this.isTicketView()) {
this.searchEventTickets(this.search_query);
}
}
}
};
</script>

View file

@ -23,6 +23,7 @@ const store = createStore({
lastEvent: '37C3', lastEvent: '37C3',
lastUsed: {}, lastUsed: {},
searchQuery: '',
remember: false, remember: false,
user: { user: {
username: null, username: null,
@ -355,10 +356,9 @@ const store = createStore({
} }
}, },
async searchEventItems({commit, getters, state}, query) { async searchEventItems({commit, getters, state}, query) {
const foo = utf8.encode(query); const encoded_query = base64.encode(utf8.encode(query));
const bar = base64.encode(foo);
const {data, success} = await http.get(`/2/${getters.getEventSlug}/items/${bar}/`, state.user.token); const {data, success} = await http.get(`/2/${getters.getEventSlug}/items/${encoded_query}/`, state.user.token);
if (data && success) if (data && success)
commit('replaceLoadedItems', data); commit('replaceLoadedItems', data);
}, },
@ -407,6 +407,13 @@ const store = createStore({
if (data && success) if (data && success)
commit('replaceTickets', data); commit('replaceTickets', data);
}, },
async searchEventTickets({commit, getters, state}, query) {
const encoded_query = base64.encode(utf8.encode(query));
const {data, success} = await http.get(`/2/${getters.getEventSlug}/tickets/${encoded_query}/`, state.user.token);
if (data && success)
commit('replaceTickets', data);
},
async sendMail({commit, dispatch, state}, {id, message}) { async sendMail({commit, dispatch, state}, {id, message}) {
const {data, success} = await http.post(`/2/tickets/${id}/reply/`, {message}, state.user.token); const {data, success} = await http.post(`/2/tickets/${id}/reply/`, {message}, state.user.token);
if (data && success) { if (data && success) {