Skip to content
Snippets Groups Projects
Commit 7af7208f authored by Chris Beer's avatar Chris Beer
Browse files

Poke the startIndex through to the search components that need it; part of #2684

parent d4024a81
No related tags found
No related merge requests found
...@@ -20,6 +20,7 @@ function createWrapper(props) { ...@@ -20,6 +20,7 @@ function createWrapper(props) {
match: 'moose', match: 'moose',
}, },
]} ]}
startIndex={10}
{...props} {...props}
/>, />,
); );
...@@ -30,7 +31,7 @@ describe('SearchResults', () => { ...@@ -30,7 +31,7 @@ describe('SearchResults', () => {
const selectContentSearchAnnotation = jest.fn(); const selectContentSearchAnnotation = jest.fn();
const wrapper = createWrapper({ selectContentSearchAnnotation }); const wrapper = createWrapper({ selectContentSearchAnnotation });
expect(wrapper.find('Connect(WithStyles(WithPlugins(SearchHit)))').length).toEqual(1); expect(wrapper.find('Connect(WithStyles(WithPlugins(SearchHit)))').length).toEqual(1);
expect(wrapper.find('Connect(WithStyles(WithPlugins(SearchHit)))').prop('index')).toEqual(0); expect(wrapper.find('Connect(WithStyles(WithPlugins(SearchHit)))').prop('index')).toEqual(10);
}); });
it('can focus on a single item', () => { it('can focus on a single item', () => {
......
...@@ -5,6 +5,7 @@ import { ...@@ -5,6 +5,7 @@ import {
getSelectedContentSearchAnnotationIds, getSelectedContentSearchAnnotationIds,
getSelectedContentSearchAnnotations, getSelectedContentSearchAnnotations,
getSearchAnnotationsForCompanionWindow, getSearchAnnotationsForCompanionWindow,
getSearchForCompanionWindow,
} from '../../../src/state/selectors'; } from '../../../src/state/selectors';
describe('getSearchResultsForWindow', () => { describe('getSearchResultsForWindow', () => {
...@@ -36,6 +37,36 @@ describe('getSearchResultsForWindow', () => { ...@@ -36,6 +37,36 @@ describe('getSearchResultsForWindow', () => {
}); });
}); });
describe('getSearchForCompanionWindow', () => {
const companionWindowId = 'cwid';
it('returns the search data for a completed search', () => {
const state = {
searches: {
a: {
[companionWindowId]: {
json: { hits: [1, 2, 3] },
},
},
b: {
[companionWindowId]: {
isFetching: true,
json: { },
},
},
},
};
expect(
getSearchForCompanionWindow(state, { companionWindowId, windowId: 'a' }),
).toEqual({ hits: [1, 2, 3] });
expect(
getSearchForCompanionWindow(state, { companionWindowId, windowId: 'b' }),
).toEqual({});
expect(
getSearchForCompanionWindow({}, { companionWindowId, windowId: 'a' }),
).toEqual({});
});
});
describe('getSearchHitsForCompanionWindow', () => { describe('getSearchHitsForCompanionWindow', () => {
const companionWindowId = 'cwid'; const companionWindowId = 'cwid';
it('returns flattened hits for a manifest', () => { it('returns flattened hits for a manifest', () => {
......
...@@ -42,9 +42,9 @@ export class SearchPanelNavigation extends Component { ...@@ -42,9 +42,9 @@ export class SearchPanelNavigation extends Component {
*/ */
render() { render() {
const { const {
searchHits, selectedContentSearchAnnotation, classes, t, searchHits, selectedContentSearchAnnotation, classes, t, startIndex,
} = this.props; } = this.props;
const currentHitIndex = searchHits const currentHitIndex = startIndex + searchHits
.findIndex(val => val.annotations.includes(selectedContentSearchAnnotation[0])); .findIndex(val => val.annotations.includes(selectedContentSearchAnnotation[0]));
return ( return (
<> <>
...@@ -79,11 +79,13 @@ SearchPanelNavigation.propTypes = { ...@@ -79,11 +79,13 @@ SearchPanelNavigation.propTypes = {
}).isRequired, }).isRequired,
selectContentSearchAnnotation: PropTypes.func.isRequired, selectContentSearchAnnotation: PropTypes.func.isRequired,
selectedContentSearchAnnotation: PropTypes.arrayOf(PropTypes.string).isRequired, selectedContentSearchAnnotation: PropTypes.arrayOf(PropTypes.string).isRequired,
startIndex: PropTypes.number,
t: PropTypes.func, t: PropTypes.func,
windowId: PropTypes.string.isRequired, windowId: PropTypes.string.isRequired,
}; };
SearchPanelNavigation.defaultProps = { SearchPanelNavigation.defaultProps = {
classes: {}, classes: {},
searchHits: [], searchHits: [],
startIndex: 0,
t: key => key, t: key => key,
}; };
...@@ -30,6 +30,7 @@ export class SearchResults extends Component { ...@@ -30,6 +30,7 @@ export class SearchResults extends Component {
const { const {
classes, classes,
searchHits, searchHits,
startIndex,
t, t,
windowId, windowId,
} = this.props; } = this.props;
...@@ -53,7 +54,7 @@ export class SearchResults extends Component { ...@@ -53,7 +54,7 @@ export class SearchResults extends Component {
key={hit.annotations[0]} key={hit.annotations[0]}
focused={focused} focused={focused}
hit={hit} hit={hit}
index={index} index={startIndex + index}
windowId={windowId} windowId={windowId}
showDetails={this.toggleFocus} showDetails={this.toggleFocus}
/> />
...@@ -68,11 +69,13 @@ export class SearchResults extends Component { ...@@ -68,11 +69,13 @@ export class SearchResults extends Component {
SearchResults.propTypes = { SearchResults.propTypes = {
classes: PropTypes.objectOf(PropTypes.string), classes: PropTypes.objectOf(PropTypes.string),
searchHits: PropTypes.arrayOf(PropTypes.object).isRequired, searchHits: PropTypes.arrayOf(PropTypes.object).isRequired,
startIndex: PropTypes.number,
t: PropTypes.func, t: PropTypes.func,
windowId: PropTypes.string.isRequired, // eslint-disable-line react/no-unused-prop-types windowId: PropTypes.string.isRequired, // eslint-disable-line react/no-unused-prop-types
}; };
SearchResults.defaultProps = { SearchResults.defaultProps = {
classes: {}, classes: {},
startIndex: 0,
t: k => k, t: k => k,
}; };
...@@ -8,6 +8,7 @@ import * as actions from '../state/actions'; ...@@ -8,6 +8,7 @@ import * as actions from '../state/actions';
import { import {
getSearchHitsForCompanionWindow, getSearchHitsForCompanionWindow,
getSelectedContentSearchAnnotationIds, getSelectedContentSearchAnnotationIds,
getSearchForCompanionWindow,
} from '../state/selectors'; } from '../state/selectors';
/** /**
...@@ -18,6 +19,7 @@ import { ...@@ -18,6 +19,7 @@ import {
const mapStateToProps = (state, { companionWindowId, windowId }) => ({ const mapStateToProps = (state, { companionWindowId, windowId }) => ({
searchHits: getSearchHitsForCompanionWindow(state, { companionWindowId, windowId }), searchHits: getSearchHitsForCompanionWindow(state, { companionWindowId, windowId }),
selectedContentSearchAnnotation: getSelectedContentSearchAnnotationIds(state, { windowId }), selectedContentSearchAnnotation: getSelectedContentSearchAnnotationIds(state, { windowId }),
startIndex: getSearchForCompanionWindow(state, { companionWindowId, windowId }).startIndex,
}); });
/** /**
......
...@@ -8,6 +8,7 @@ import * as actions from '../state/actions'; ...@@ -8,6 +8,7 @@ import * as actions from '../state/actions';
import { import {
getSearchHitsForCompanionWindow, getSearchHitsForCompanionWindow,
getSelectedContentSearchAnnotationIds, getSelectedContentSearchAnnotationIds,
getSearchForCompanionWindow,
} from '../state/selectors'; } from '../state/selectors';
/** /**
...@@ -18,6 +19,7 @@ import { ...@@ -18,6 +19,7 @@ import {
const mapStateToProps = (state, { companionWindowId, windowId }) => ({ const mapStateToProps = (state, { companionWindowId, windowId }) => ({
searchHits: getSearchHitsForCompanionWindow(state, { companionWindowId, windowId }), searchHits: getSearchHitsForCompanionWindow(state, { companionWindowId, windowId }),
selectedContentSearchAnnotation: getSelectedContentSearchAnnotationIds(state, { windowId }), selectedContentSearchAnnotation: getSelectedContentSearchAnnotationIds(state, { windowId }),
startIndex: getSearchForCompanionWindow(state, { companionWindowId, windowId }).startIndex,
}); });
const mapDispatchToProps = { const mapDispatchToProps = {
......
...@@ -25,20 +25,30 @@ export const getSearchResultsForCompanionWindow = createSelector( ...@@ -25,20 +25,30 @@ export const getSearchResultsForCompanionWindow = createSelector(
}, },
); );
export const getSearchHitsForCompanionWindow = createSelector( export const getSearchForCompanionWindow = createSelector(
[ [
getSearchResultsForCompanionWindow, getSearchResultsForCompanionWindow,
], ],
(result) => { (result) => {
if (!result || !result.json || result.isFetching || !result.json.hits) return []; if (!result || !result.json || result.isFetching) return {};
return result.json.hits; return result.json;
},
);
export const getSearchHitsForCompanionWindow = createSelector(
[
getSearchForCompanionWindow,
],
(result) => {
if (!result.hits) return [];
return result.hits;
}, },
); );
/** convert search results to an annotation */ /** convert search results to an annotation */
const searchResultsToAnnotation = (result) => { const searchResultsToAnnotation = (json) => {
if (!result || !result.json || result.isFetching || !result.json.resources) return undefined; if (!json.resources) return undefined;
const anno = new Annotation(result.json); const anno = new Annotation(json);
return { return {
id: anno.id, id: anno.id,
resources: anno.resources, resources: anno.resources,
...@@ -47,7 +57,7 @@ const searchResultsToAnnotation = (result) => { ...@@ -47,7 +57,7 @@ const searchResultsToAnnotation = (result) => {
export const getSearchAnnotationsForCompanionWindow = createSelector( export const getSearchAnnotationsForCompanionWindow = createSelector(
[ [
getSearchResultsForCompanionWindow, getSearchForCompanionWindow,
], ],
result => searchResultsToAnnotation(result), result => searchResultsToAnnotation(result),
); );
...@@ -58,7 +68,9 @@ export const getSearchAnnotationsForWindow = createSelector( ...@@ -58,7 +68,9 @@ export const getSearchAnnotationsForWindow = createSelector(
], ],
(results) => { (results) => {
if (!results) return []; if (!results) return [];
const arr = Object.values(results).map(result => searchResultsToAnnotation(result)); const arr = Object.values(results).map(result => (
result && result.json && !result.isFetching && searchResultsToAnnotation(result.json)
));
return arr.filter(e => e); return arr.filter(e => e);
}, },
); );
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment