diff options
Diffstat (limited to 'app/assets/javascripts/runner/runner_search_utils.js')
-rw-r--r-- | app/assets/javascripts/runner/runner_search_utils.js | 88 |
1 files changed, 80 insertions, 8 deletions
diff --git a/app/assets/javascripts/runner/runner_search_utils.js b/app/assets/javascripts/runner/runner_search_utils.js index 0a817ea0acf..b88023720e8 100644 --- a/app/assets/javascripts/runner/runner_search_utils.js +++ b/app/assets/javascripts/runner/runner_search_utils.js @@ -18,6 +18,50 @@ import { RUNNER_PAGE_SIZE, } from './constants'; +/** + * The filters and sorting of the runners are built around + * an object called "search" that contains the current state + * of search in the UI. For example: + * + * ``` + * const search = { + * // The current tab + * runnerType: 'INSTANCE_TYPE', + * + * // Filters in the search bar + * filters: [ + * { type: 'status', value: { data: 'ACTIVE', operator: '=' } }, + * { type: 'filtered-search-term', value: { data: '' } }, + * ], + * + * // Current sorting value + * sort: 'CREATED_DESC', + * + * // Pagination information + * pagination: { page: 1 }, + * }; + * ``` + * + * An object in this format can be used to generate URLs + * with the search parameters or by runner components + * a input using a v-model. + * + * @module runner_search_utils + */ + +/** + * Validates a search value + * @param {Object} search + * @returns {boolean} True if the value follows the search format. + */ +export const searchValidator = ({ runnerType, filters, sort }) => { + return ( + (runnerType === null || typeof runnerType === 'string') && + Array.isArray(filters) && + typeof sort === 'string' + ); +}; + const getPaginationFromParams = (params) => { const page = parseInt(params[PARAM_KEY_PAGE], 10); const after = params[PARAM_KEY_AFTER]; @@ -35,13 +79,20 @@ const getPaginationFromParams = (params) => { }; }; +/** + * Takes a URL query and transforms it into a "search" object + * @param {String?} query + * @returns {Object} A search object + */ export const fromUrlQueryToSearch = (query = window.location.search) => { const params = queryToObject(query, { gatherArrays: true }); + const runnerType = params[PARAM_KEY_RUNNER_TYPE]?.[0] || null; return { + runnerType, filters: prepareTokens( urlQueryToFilter(query, { - filterNamesAllowList: [PARAM_KEY_STATUS, PARAM_KEY_RUNNER_TYPE, PARAM_KEY_TAG], + filterNamesAllowList: [PARAM_KEY_STATUS, PARAM_KEY_TAG], filteredSearchTermKey: PARAM_KEY_SEARCH, }), ), @@ -50,8 +101,15 @@ export const fromUrlQueryToSearch = (query = window.location.search) => { }; }; +/** + * Takes a "search" object and transforms it into a URL. + * + * @param {Object} search + * @param {String} url + * @returns {String} New URL for the page + */ export const fromSearchToUrl = ( - { filters = [], sort = null, pagination = {} }, + { runnerType = null, filters = [], sort = null, pagination = {} }, url = window.location.href, ) => { const filterParams = { @@ -65,6 +123,10 @@ export const fromSearchToUrl = ( }), }; + if (runnerType) { + filterParams[PARAM_KEY_RUNNER_TYPE] = [runnerType]; + } + if (!filterParams[PARAM_KEY_SEARCH]) { filterParams[PARAM_KEY_SEARCH] = null; } @@ -82,21 +144,31 @@ export const fromSearchToUrl = ( return setUrlParams({ ...filterParams, ...otherParams }, url, false, true, true); }; -export const fromSearchToVariables = ({ filters = [], sort = null, pagination = {} } = {}) => { +/** + * Takes a "search" object and transforms it into variables for runner a GraphQL query. + * + * @param {Object} search + * @returns {Object} Hash of filter values + */ +export const fromSearchToVariables = ({ + runnerType = null, + filters = [], + sort = null, + pagination = {}, +} = {}) => { const variables = {}; const queryObj = filterToQueryObject(processFilters(filters), { filteredSearchTermKey: PARAM_KEY_SEARCH, }); - variables.search = queryObj[PARAM_KEY_SEARCH]; - - // TODO Get more than one value when GraphQL API supports OR for "status" or "runner_type" [variables.status] = queryObj[PARAM_KEY_STATUS] || []; - [variables.type] = queryObj[PARAM_KEY_RUNNER_TYPE] || []; - + variables.search = queryObj[PARAM_KEY_SEARCH]; variables.tagList = queryObj[PARAM_KEY_TAG]; + if (runnerType) { + variables.type = runnerType; + } if (sort) { variables.sort = sort; } |