From 3c6319f275f2e932c9e3c13af21e29a8e7d0fce3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20Molakvo=C3=A6=20=28skjnldsv=29?= Date: Tue, 15 Sep 2020 17:10:44 +0200 Subject: [PATCH] Properly use form role=search and unify reset button MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: John Molakvoæ (skjnldsv) --- core/src/services/UnifiedSearchService.js | 30 +++- core/src/views/UnifiedSearch.vue | 167 ++++++++++++++++------ 2 files changed, 145 insertions(+), 52 deletions(-) diff --git a/core/src/services/UnifiedSearchService.js b/core/src/services/UnifiedSearchService.js index c52cf5e46f..eb91f18f8c 100644 --- a/core/src/services/UnifiedSearchService.js +++ b/core/src/services/UnifiedSearchService.js @@ -28,6 +28,12 @@ export const minSearchLength = 2 export const regexFilterIn = /[^-]in:([a-z_-]+)/ig export const regexFilterNot = /-in:([a-z_-]+)/ig +/** + * Create a cancel token + * @returns {CancelTokenSource} + */ +const createCancelToken = () => axios.CancelToken.source() + /** * Get the list of available search providers * @@ -54,13 +60,20 @@ export async function getTypes() { /** * Get the list of available search providers * - * @param {string} type the type to search - * @param {string} query the search - * @param {int|string|undefined} cursor the offset for paginated searches - * @returns {Promise} + * @param {Object} options destructuring object + * @param {string} options.type the type to search + * @param {string} options.query the search + * @param {int|string|undefined} options.cursor the offset for paginated searches + * @returns {Object} {request: Promise, cancel: Promise} */ -export function search(type, query, cursor) { - return axios.get(generateOcsUrl('search', 2) + `providers/${type}/search`, { +export function search({ type, query, cursor }) { + /** + * Generate an axios cancel token + */ + const cancelToken = createCancelToken() + + const request = async() => axios.get(generateOcsUrl('search', 2) + `providers/${type}/search`, { + cancelToken: cancelToken.token, params: { term: query, cursor, @@ -68,4 +81,9 @@ export function search(type, query, cursor) { from: window.location.pathname.replace('/index.php', '') + window.location.search, }, }) + + return { + request, + cancel: cancelToken.cancel, + } } diff --git a/core/src/views/UnifiedSearch.vue b/core/src/views/UnifiedSearch.vue index b68051de90..099fa33650 100644 --- a/core/src/views/UnifiedSearch.vue +++ b/core/src/views/UnifiedSearch.vue @@ -31,16 +31,30 @@ - +
- + + import { emit } from '@nextcloud/event-bus' import { minSearchLength, getTypes, search, defaultLimit, regexFilterIn, regexFilterNot } from '../services/UnifiedSearchService' +import { showError } from '@nextcloud/dialogs' import ActionButton from '@nextcloud/vue/dist/Components/ActionButton' import Actions from '@nextcloud/vue/dist/Components/Actions' import debounce from 'debounce' @@ -116,7 +131,6 @@ import Magnify from 'vue-material-design-icons/Magnify' import HeaderMenu from '../components/HeaderMenu' import SearchResult from '../components/UnifiedSearch/SearchResult' import SearchResultPlaceholders from '../components/UnifiedSearch/SearchResultPlaceholders' -import { showError } from '@nextcloud/dialogs' export default { name: 'UnifiedSearch', @@ -135,10 +149,17 @@ export default { return { types: [], + // Cursors per types cursors: {}, + // Various search limits per types limits: {}, + // Loading types loading: {}, + // Reached search types reached: {}, + // Pending cancellable requests + requests: [], + // List of all results results: {}, query: '', @@ -296,10 +317,12 @@ export default { /** * Reset the search state */ - resetSearch() { + onReset() { emit('nextcloud:unified-search:reset') + this.logger.debug('Search reset') this.query = '' this.resetState() + this.focusInput() }, resetState() { this.cursors = {} @@ -308,6 +331,15 @@ export default { this.reached = {} this.results = {} this.focused = null + this.cancelPendingRequests() + }, + + /** + * Cancel any ongoing searches + */ + cancelPendingRequests() { + // Cancel all pending requests + this.requests.forEach(cancel => cancel()) }, /** @@ -320,18 +352,6 @@ export default { }) }, - /** - * Watch the search event on the input - * Used to detect the reset button press - * @param {Event} event the search event - */ - onSearch(event) { - // If value is empty, the reset button has been pressed - if (event.target.value === '') { - this.resetSearch() - } - }, - /** * If we have results already, open first one * If not, trigger the search again @@ -378,29 +398,34 @@ export default { // Reset search if the query changed this.resetState() - types.forEach(async type => { + Promise.all(types.map(async type => { try { + // Init cancellable request + const { request, cancel } = search({ type, query }) + this.requests.push(cancel) + + // Mark this type as loading and fetch results this.$set(this.loading, type, true) - const request = await search(type, query) + const { data } = await request() // Process results - if (request.data.ocs.data.entries.length > 0) { - this.$set(this.results, type, request.data.ocs.data.entries) + if (data.ocs.data.entries.length > 0) { + this.$set(this.results, type, data.ocs.data.entries) } else { this.$delete(this.results, type) } // Save cursor if any - if (request.data.ocs.data.cursor) { - this.$set(this.cursors, type, request.data.ocs.data.cursor) - } else if (!request.data.ocs.data.isPaginated) { - // If no cursor and no pagination, we save the default amount - // provided by server's initial state `defaultLimit` + if (data.ocs.data.cursor) { + this.$set(this.cursors, type, data.ocs.data.cursor) + } else if (!data.ocs.data.isPaginated) { + // If no cursor and no pagination, we save the default amount + // provided by server's initial state `defaultLimit` this.$set(this.limits, type, this.defaultLimit) } // Check if we reached end of pagination - if (request.data.ocs.data.entries.length < this.defaultLimit) { + if (data.ocs.data.entries.length < this.defaultLimit) { this.$set(this.reached, type, true) } @@ -409,13 +434,19 @@ export default { this.focused = 0 } } catch (error) { - this.logger.error(`Error searching for ${this.typesMap[type]}`, error) - showError(this.t('core', 'An error occurred while looking for {type}', { type: this.typesMap[type] })) + // If this is not a cancelled throw + if (error.response && error.response.status) { + this.logger.error(`Error searching for ${this.typesMap[type]}`, error) + showError(this.t('core', 'An error occurred while searching for {type}', { type: this.typesMap[type] })) + } this.$delete(this.results, type) } finally { this.$set(this.loading, type, false) } + })).then(() => { + // We finished all searches + this.loading = {} }) }, onInputDebounced: debounce(function(e) { @@ -434,7 +465,7 @@ export default { this.$set(this.loading, type, true) if (this.cursors[type]) { - const request = await search(type, this.query, this.cursors[type]) + const request = await search({ type, query: this.query, cursor: this.cursors[type] }) // Save cursor if any if (request.data.ocs.data.cursor) { @@ -582,6 +613,7 @@ export default {