+ const currentView = getState().searchBar.currentView;
+ const searchValuePresent = searchValue.length > 0;
+
+ if (currentView === SearchView.ADVANCED) {
+ dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
+ } else if (searchValuePresent) {
+ dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.AUTOCOMPLETE));
+ dispatch(searchBarActions.SET_SELECTED_ITEM(searchValue));
+ } else {
+ dispatch(searchBarActions.SET_CURRENT_VIEW(SearchView.BASIC));
+ dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
+ dispatch(searchBarActions.SELECT_FIRST_ITEM());
+ }
+ };
+
+export const submitData = (event: React.FormEvent<HTMLFormElement>) =>
+ (dispatch: Dispatch, getState: () => RootState) => {
+ event.preventDefault();
+ const searchValue = getState().searchBar.searchValue;
+ dispatch<any>(saveRecentQuery(searchValue));
+ dispatch<any>(loadRecentQueries());
+ dispatch(searchBarActions.CLOSE_SEARCH_VIEW());
+ if (RESOURCE_UUID_REGEX.exec(searchValue) || COLLECTION_PDH_REGEX.exec(searchValue)) {
+ dispatch<any>(navigateTo(searchValue));
+ } else {
+ dispatch(searchBarActions.SET_SEARCH_VALUE(searchValue));
+ dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
+ dispatch(searchResultsPanelActions.CLEAR());
+ dispatch(navigateToSearchResults(searchValue));
+ }
+ };
+
+let cancelTokens: any[] = [];
+const searchGroups = (searchValue: string, limit: number, useCancel = false) =>
+ async (dispatch: Dispatch, getState: () => RootState, services: ServiceRepository) => {
+ const currentView = getState().searchBar.currentView;
+
+ if (cancelTokens.length > 0 && useCancel) {
+ cancelTokens.forEach(cancelToken => (cancelToken as any).cancel('New search request triggered.'));
+ cancelTokens = [];
+ }
+
+ setTimeout(async () => {
+ if (searchValue || currentView === SearchView.ADVANCED) {
+ const { cluster: clusterId } = getAdvancedDataFromQuery(searchValue);
+ const sessions = getSearchSessions(clusterId, getState().auth.sessions);
+ const lists: ListResults<GroupContentsResource>[] = await Promise.all(sessions.map((session, index) => {
+ cancelTokens.push(axios.CancelToken.source());
+ const filters = queryToFilters(searchValue, session.apiRevision);
+ return services.groupsService.contents('', {
+ filters,
+ limit,
+ recursive: true
+ }, session, cancelTokens[index].token);
+ }));
+
+ cancelTokens = [];
+
+ const items = lists.reduce((items, list) => items.concat(list.items), [] as GroupContentsResource[]);
+
+ if (lists.filter(list => !!(list as any).items).length !== lists.length) {
+ dispatch(searchBarActions.SET_SEARCH_RESULTS([]));
+ } else {
+ dispatch(searchBarActions.SET_SEARCH_RESULTS(items));
+ }
+ }
+ }, 10);
+ };
+
+const buildQueryFromKeyMap = (data: any, keyMap: string[][]) => {
+ let value = data.searchValue;
+
+ const addRem = (field: string, key: string) => {
+ const v = data[key];
+ // Remove previous search expression.
+ if (data.hasOwnProperty(key)) {
+ let pattern: string;
+ if (v === false) {
+ pattern = `${field.replace(':', '\\:\\s*')}\\s*`;
+ } else if (key.startsWith('prop-')) {
+ // On properties, only remove key:value duplicates, allowing
+ // multiple properties with the same key.
+ const oldValue = key.slice(5).split(':')[1];
+ pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*${oldValue}\\s*`;
+ } else {
+ pattern = `${field.replace(':', '\\:\\s*')}\\:\\s*[\\w|\\#|\\-|\\/]*\\s*`;
+ }
+ value = value.replace(new RegExp(pattern), '');
+ }
+ // Re-add it with the current search value.
+ if (v) {
+ const nv = v === true
+ ? `${field}`
+ : `${field}:${v}`;
+ // Always append to the end to keep user-entered text at the start.
+ value = value + ' ' + nv;