|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref, watch } from 'vue' |
| 3 | +import axios from 'axios' |
| 4 | +
|
| 5 | +/** |
| 6 | + * Configuration Constants |
| 7 | + */ |
| 8 | +const minLength: number = 3 |
| 9 | +const dataUrl: string = '/api/search' |
| 10 | +
|
| 11 | +/** |
| 12 | + * Reactive Variables |
| 13 | + */ |
| 14 | +const searchQuery = ref<string>('') |
| 15 | +const loading = ref<boolean>(false) |
| 16 | +const error = ref<any>(null) |
| 17 | +const data = ref<ResultData>({ |
| 18 | + count: 0, |
| 19 | + size: 0, |
| 20 | + page: 0, |
| 21 | + rows: [] |
| 22 | +}) |
| 23 | +
|
| 24 | +/** |
| 25 | + * Api fetch function |
| 26 | + */ |
| 27 | +async function fetch() { |
| 28 | + if (searchQuery.value.length < minLength) { |
| 29 | + return |
| 30 | + } |
| 31 | +
|
| 32 | + loading.value = true |
| 33 | + axios |
| 34 | + .get<ResultData>(dataUrl, { |
| 35 | + params: { |
| 36 | + q: searchQuery.value |
| 37 | + } |
| 38 | + }) |
| 39 | + .then((response) => { |
| 40 | + data.value = response.data |
| 41 | + }) |
| 42 | + .catch((err) => { |
| 43 | + error.value = err.response.data |
| 44 | + }) |
| 45 | + .finally(() => { |
| 46 | + loading.value = false |
| 47 | + }) |
| 48 | +} |
| 49 | +
|
| 50 | +/** |
| 51 | + * Watchers |
| 52 | + */ |
| 53 | +watch(searchQuery, async () => { |
| 54 | + fetch() |
| 55 | +}) |
| 56 | +
|
| 57 | +/** |
| 58 | + * Computed Properties |
| 59 | + */ |
| 60 | +const placeholder = ref<string>(`Type at least ${minLength} characters to search`) |
| 61 | +
|
| 62 | +/** |
| 63 | + * Interfaces |
| 64 | + */ |
| 65 | +interface ResultData { |
| 66 | + count: number |
| 67 | + size: number |
| 68 | + page: number |
| 69 | + rows: Result[] |
| 70 | +} |
| 71 | +
|
| 72 | +interface Result { |
| 73 | + title: string |
| 74 | + slug: string |
| 75 | + route: string |
| 76 | + snippet: string |
| 77 | + score: number |
| 78 | + version: string |
| 79 | +} |
| 80 | +</script> |
| 81 | + |
| 82 | +<template> |
| 83 | + <div class="uk-margin-small uk-inline uk-width-expand"> |
| 84 | + <span class="uk-form-icon" uk-icon="icon: search"></span> |
| 85 | + <input |
| 86 | + class="uk-input" |
| 87 | + type="text" |
| 88 | + placeholder="Search Documentation" |
| 89 | + aria-label="Search Documentation" |
| 90 | + uk-toggle="target: #search-modal" /> |
| 91 | + </div> |
| 92 | + |
| 93 | + <!-- This is the modal --> |
| 94 | + <div id="search-modal" uk-modal> |
| 95 | + <div class="uk-modal-dialog"> |
| 96 | + <button class="uk-modal-close-default" type="button" uk-close></button> |
| 97 | + <div class="uk-modal-header"> |
| 98 | + <h2 class="uk-modal-title">Search Documentation</h2> |
| 99 | + </div> |
| 100 | + |
| 101 | + <div class="uk-modal-body"> |
| 102 | + <div class="uk-margin-small uk-inline uk-width-expand"> |
| 103 | + <span class="uk-form-icon" uk-icon="icon: search"></span> |
| 104 | + <input |
| 105 | + class="uk-input" |
| 106 | + v-model="searchQuery" |
| 107 | + type="text" |
| 108 | + :placeholder="placeholder" |
| 109 | + aria-label="Search Documentation" |
| 110 | + autofocus |
| 111 | + tabindex="1" /> |
| 112 | + </div> |
| 113 | + |
| 114 | + <div class="uk-margin" uk-overflow-auto> |
| 115 | + <div v-if="loading" class="uk-text-center"> |
| 116 | + <div uk-spinner></div> |
| 117 | + </div> |
| 118 | + <div v-else-if="error" class="uk-alert-danger" uk-alert> |
| 119 | + <p>{{ error }}</p> |
| 120 | + </div> |
| 121 | + <div |
| 122 | + v-else-if="data.rows.length === 0 && searchQuery.length >= minLength" |
| 123 | + class="uk-text-center uk-text-muted"> |
| 124 | + <p>No results found</p> |
| 125 | + </div> |
| 126 | + <ul v-else-if="data.rows.length > 0" class="uk-list uk-list-divider"> |
| 127 | + <li v-for="row in data.rows" :key="row.route"> |
| 128 | + <a :href="row.route" class="uk-link-reset"> |
| 129 | + <h4 class="uk-margin-remove">{{ row.title }}</h4> |
| 130 | + <p class="uk-text-small" v-html="row.snippet"></p> |
| 131 | + </a> |
| 132 | + </li> |
| 133 | + </ul> |
| 134 | + </div> |
| 135 | + </div> |
| 136 | + |
| 137 | + <div class="uk-modal-footer uk-text-right"> |
| 138 | + <button class="uk-button uk-button-primary uk-modal-close" type="button"> |
| 139 | + Close |
| 140 | + </button> |
| 141 | + </div> |
| 142 | + </div> |
| 143 | + </div> |
| 144 | +</template> |
0 commit comments