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

Delay loading unnecessary manifests until they are required for rendering e.g. in the add window

parent df205124
No related branches found
No related tags found
No related merge requests found
...@@ -45,7 +45,7 @@ describe('manifest actions', () => { ...@@ -45,7 +45,7 @@ describe('manifest actions', () => {
it('dispatches the REQUEST_MANIFEST action', () => { it('dispatches the REQUEST_MANIFEST action', () => {
store.dispatch(actions.fetchManifest('https://purl.stanford.edu/sn904cj3429/iiif/manifest')); store.dispatch(actions.fetchManifest('https://purl.stanford.edu/sn904cj3429/iiif/manifest'));
expect(store.getActions()).toEqual([ expect(store.getActions()).toEqual([
{ manifestId: 'https://purl.stanford.edu/sn904cj3429/iiif/manifest', type: 'REQUEST_MANIFEST' }, { manifestId: 'https://purl.stanford.edu/sn904cj3429/iiif/manifest', type: 'REQUEST_MANIFEST', properties: { isFetching: true } },
]); ]);
}); });
it('dispatches the REQUEST_MANIFEST and then RECEIVE_MANIFEST', () => { it('dispatches the REQUEST_MANIFEST and then RECEIVE_MANIFEST', () => {
......
...@@ -30,6 +30,12 @@ describe('ManifestListItem', () => { ...@@ -30,6 +30,12 @@ describe('ManifestListItem', () => {
expect(wrapper.find('.mirador-manifest-list-item').length).toBe(1); expect(wrapper.find('.mirador-manifest-list-item').length).toBe(1);
expect(wrapper.find('ReactPlaceholder').length).toBe(1); expect(wrapper.find('ReactPlaceholder').length).toBe(1);
}); });
it('renders an error message if fetching the manifest failed', () => {
const wrapper = createWrapper({ error: 'This is an error message' });
expect(wrapper.find('WithStyles(Paper)').length).toBe(1);
expect(wrapper.find('WithStyles(Paper)').children().text()).toEqual('This is an error message');
});
it('updates and adds window when button clicked', () => { it('updates and adds window when button clicked', () => {
const addWindow = jest.fn(); const addWindow = jest.fn();
const wrapper = createWrapper({ addWindow }); const wrapper = createWrapper({ addWindow });
......
...@@ -9,7 +9,6 @@ describe('manifests reducer', () => { ...@@ -9,7 +9,6 @@ describe('manifests reducer', () => {
})).toEqual({ })).toEqual({
abc123: { abc123: {
id: 'abc123', id: 'abc123',
isFetching: true,
}, },
}); });
}); });
......
...@@ -25,10 +25,30 @@ const handleOpenButtonClick = (event, manifest, addWindow) => { ...@@ -25,10 +25,30 @@ const handleOpenButtonClick = (event, manifest, addWindow) => {
/** */ /** */
class ManifestListItem extends React.Component { class ManifestListItem extends React.Component {
/** */
componentDidMount() {
const {
fetchManifest, manifestId, ready, isFetching, error,
} = this.props;
if (!ready && !error && !isFetching) fetchManifest(manifestId);
}
/** */ /** */
render() { render() {
const { const {
manifestId, ready, title, thumbnail, logo, addWindow, handleClose, size, classes, provider, t, manifestId,
ready,
title,
thumbnail,
logo,
addWindow,
handleClose,
size,
classes,
provider,
t,
error,
} = this.props; } = this.props;
const placeholder = ( const placeholder = (
...@@ -48,6 +68,14 @@ class ManifestListItem extends React.Component { ...@@ -48,6 +68,14 @@ class ManifestListItem extends React.Component {
</Grid> </Grid>
); );
if (error) {
return (
<Paper elevation={1} className={classes.root} data-manifestid={manifestId}>
{error}
</Paper>
);
}
return ( return (
<Paper elevation={1} className={classes.root} data-manifestid={manifestId}> <Paper elevation={1} className={classes.root} data-manifestid={manifestId}>
<ReactPlaceholder <ReactPlaceholder
...@@ -113,6 +141,9 @@ ManifestListItem.propTypes = { ...@@ -113,6 +141,9 @@ ManifestListItem.propTypes = {
classes: PropTypes.object, // eslint-disable-line react/forbid-prop-types classes: PropTypes.object, // eslint-disable-line react/forbid-prop-types
provider: PropTypes.string, provider: PropTypes.string,
t: PropTypes.func, t: PropTypes.func,
fetchManifest: PropTypes.func.isRequired,
error: PropTypes.string,
isFetching: PropTypes.bool,
}; };
ManifestListItem.defaultProps = { ManifestListItem.defaultProps = {
...@@ -125,6 +156,8 @@ ManifestListItem.defaultProps = { ...@@ -125,6 +156,8 @@ ManifestListItem.defaultProps = {
size: 0, size: 0,
provider: null, provider: null,
t: key => key, t: key => key,
error: null,
isFetching: false,
}; };
/** */ /** */
......
...@@ -13,6 +13,8 @@ const mapStateToProps = (state, { manifestId }) => { ...@@ -13,6 +13,8 @@ const mapStateToProps = (state, { manifestId }) => {
return { return {
ready: !!manifest.manifestation, ready: !!manifest.manifestation,
error: manifest.error,
isFetching: manifest.isFetching,
title: getManifestTitle(manifest), title: getManifestTitle(manifest),
logo: getManifestLogo(manifest), logo: getManifestLogo(manifest),
thumbnail: getManifestThumbnail(manifest), thumbnail: getManifestThumbnail(manifest),
...@@ -26,7 +28,7 @@ const mapStateToProps = (state, { manifestId }) => { ...@@ -26,7 +28,7 @@ const mapStateToProps = (state, { manifestId }) => {
* @memberof ManifestListItem * @memberof ManifestListItem
* @private * @private
*/ */
const mapDispatchToProps = { addWindow: actions.addWindow }; const mapDispatchToProps = { addWindow: actions.addWindow, fetchManifest: actions.fetchManifest };
const enhance = compose( const enhance = compose(
connect(mapStateToProps, mapDispatchToProps), connect(mapStateToProps, mapDispatchToProps),
......
...@@ -66,7 +66,7 @@ class MiradorViewer { ...@@ -66,7 +66,7 @@ class MiradorViewer {
Object.keys(mergedConfig.manifests || {}).forEach((manifestId) => { Object.keys(mergedConfig.manifests || {}).forEach((manifestId) => {
this.store.dispatch( this.store.dispatch(
actions.fetchManifest(manifestId, mergedConfig.manifests[manifestId]), actions.requestManifest(manifestId, mergedConfig.manifests[manifestId]),
); );
}); });
} }
......
...@@ -53,7 +53,8 @@ export function receiveManifestFailure(manifestId, error) { ...@@ -53,7 +53,8 @@ export function receiveManifestFailure(manifestId, error) {
*/ */
export function fetchManifest(manifestId, properties) { export function fetchManifest(manifestId, properties) {
return ((dispatch) => { return ((dispatch) => {
dispatch(requestManifest(manifestId, properties)); dispatch(requestManifest(manifestId, { ...properties, isFetching: true }));
return fetch(manifestId) return fetch(manifestId)
.then(response => response.json()) .then(response => response.json())
.then(json => dispatch(receiveManifest(manifestId, json))) .then(json => dispatch(receiveManifest(manifestId, json)))
......
...@@ -13,7 +13,6 @@ export const manifestsReducer = (state = {}, action) => { ...@@ -13,7 +13,6 @@ export const manifestsReducer = (state = {}, action) => {
...state[action.manifestId], ...state[action.manifestId],
...action.properties, ...action.properties,
id: action.manifestId, id: action.manifestId,
isFetching: true,
}, },
}; };
case ActionTypes.RECEIVE_MANIFEST: case ActionTypes.RECEIVE_MANIFEST:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment