Unverified Commit e31e280e authored by Daniel's avatar Daniel Committed by GitHub

Added auto-select resource after searching (#292)

parent 0e262bb2
......@@ -32,7 +32,7 @@ export interface SearchReducerState {
};
/* ACTIONS */
export function searchAll(term: string, resource: ResourceType, pageIndex: number): SearchAllRequest {
export function searchAll(term: string, resource?: ResourceType, pageIndex?: number): SearchAllRequest {
return {
payload: {
resource,
......
......@@ -30,11 +30,12 @@ import {
searchResourceFailure,
searchResourceSuccess, setPageIndex, setResource,
} from './reducer';
import { getPageIndex, getSearchState } from './utils';
import { autoSelectResource, getPageIndex, getSearchState } from './utils';
import { updateSearchUrl } from 'utils/navigation-utils';
export function* searchAllWorker(action: SearchAllRequest): SagaIterator {
const { resource, pageIndex, term } = action.payload;
let { resource } = action.payload;
const { pageIndex, term } = action.payload;
const tableIndex = resource === ResourceType.table ? pageIndex : 0;
const userIndex = resource === ResourceType.user ? pageIndex : 0;
const dashboardIndex = resource === ResourceType.dashboard ? pageIndex : 0;
......@@ -53,7 +54,12 @@ export function* searchAllWorker(action: SearchAllRequest): SagaIterator {
dashboards: dashboardResponse.dashboards || initialState.dashboards,
isLoading: false,
};
const index = getPageIndex(searchAllResponse, resource);
if (resource === undefined) {
resource = autoSelectResource(searchAllResponse);
searchAllResponse.selectedTab = resource;
}
const index = getPageIndex(searchAllResponse);
yield put(searchAllSuccess(searchAllResponse));
updateSearchUrl({ term, resource, index, }, true);
......@@ -80,7 +86,7 @@ export function* searchResourceWatcher(): SagaIterator {
export function* submitSearchWorker(action: SubmitSearchRequest): SagaIterator {
const { searchTerm } = action.payload;
yield put(searchAll(searchTerm, ResourceType.table, 0));
yield put(searchAll(searchTerm));
updateSearchUrl({ term: searchTerm });
};
export function* submitSearchWatcher(): SagaIterator {
......@@ -142,7 +148,7 @@ export function* loadPreviousSearchWorker(action: LoadPreviousSearchRequest): Sa
updateSearchUrl({
term: state.search_term,
resource: state.selectedTab,
index: getPageIndex(state, state.selectedTab),
index: getPageIndex(state),
});
};
export function* loadPreviousSearchWatcher(): SagaIterator {
......
import { testSaga } from 'redux-saga-test-plan';
import { ResourceType } from 'interfaces';
import { DEFAULT_RESOURCE_TYPE, ResourceType } from 'interfaces';
import * as API from '../api/v0';
......@@ -331,7 +331,7 @@ describe('search ducks', () => {
const term = 'test';
updateSearchUrlSpy.mockClear();
testSaga(submitSearchWorker, submitSearch(term))
.next().put(searchAll(term, ResourceType.table, 0))
.next().put(searchAll(term))
.next().isDone();
expect(updateSearchUrlSpy).toHaveBeenCalledWith({ term });
......@@ -536,5 +536,51 @@ describe('search ducks', () => {
expect(SearchUtils.getPageIndex(mockState, 'not valid input')).toEqual(0);
});
});
describe('autoSelectResource', () => {
const emptyMockState = {
...searchState,
dashboards: {
...searchState.dashboards,
total_results: 0,
},
tables: {
...searchState.tables,
total_results: 0,
},
users: {
...searchState.users,
total_results: 0,
}
};
it('returns the DEFAULT_RESOURCE_TYPE when search results are empty', () => {
expect(SearchUtils.autoSelectResource(emptyMockState)).toEqual(DEFAULT_RESOURCE_TYPE);
});
it('prefers `table` over `user` and `dashboard`', () => {
const mockState = { ...emptyMockState };
mockState.tables.total_results = 10;
mockState.users.total_results = 10;
mockState.dashboards.total_results = 10;
expect(SearchUtils.autoSelectResource(mockState)).toEqual(ResourceType.table);
});
it('prefers `user` over `dashboard`', () => {
const mockState = { ...emptyMockState };
mockState.tables.total_results = 0;
mockState.users.total_results = 10;
mockState.dashboards.total_results = 10;
expect(SearchUtils.autoSelectResource(mockState)).toEqual(ResourceType.user);
});
it('returns `dashboard` if there are dashboards but no other results', () => {
const mockState = { ...emptyMockState };
mockState.tables.total_results = 0;
mockState.users.total_results = 0;
mockState.dashboards.total_results = 10;
expect(SearchUtils.autoSelectResource(mockState)).toEqual(ResourceType.dashboard);
});
});
});
});
import { GlobalState } from 'ducks/rootReducer';
import { SearchReducerState } from 'ducks/search/reducer';
import { ResourceType } from 'interfaces/Resources';
import { DEFAULT_RESOURCE_TYPE, ResourceType } from 'interfaces/Resources';
export const getSearchState = (state: GlobalState): SearchReducerState => state.search;
......@@ -16,3 +16,17 @@ export const getPageIndex = (state: SearchReducerState, resource?: ResourceType)
};
return 0;
};
export const autoSelectResource = (state: SearchReducerState) => {
if (state.tables && state.tables.total_results > 0) {
return ResourceType.table;
}
if (state.users && state.users.total_results > 0) {
return ResourceType.user
}
if (state.dashboards && state.dashboards.total_results > 0) {
return ResourceType.dashboard
}
return DEFAULT_RESOURCE_TYPE;
};
......@@ -6,6 +6,8 @@ export enum ResourceType {
dashboard = "dashboard",
};
export const DEFAULT_RESOURCE_TYPE = ResourceType.table;
export interface Resource {
type: ResourceType;
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment