File size: 2,608 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
import { isEqual } from 'lodash';
import PropTypes from 'prop-types';
import { Component } from 'react';
import { connect } from 'react-redux';
import passToChildren from 'calypso/lib/react-pass-to-children';
import { setQuery } from 'calypso/state/media/actions';
import { fetchNextMediaPage } from 'calypso/state/media/thunks';
import getGooglePhotosPickerSession from 'calypso/state/selectors/get-google-photos-picker-session';
import getMediaSortedByDate from 'calypso/state/selectors/get-media-sorted-by-date';
import hasNextMediaPage from 'calypso/state/selectors/has-next-media-page';
import utils from './utils';
export class MediaListData extends Component {
static displayName = 'MediaListData';
static propTypes = {
siteId: PropTypes.number.isRequired,
source: PropTypes.string,
postId: PropTypes.number,
filter: PropTypes.string,
search: PropTypes.string,
};
componentDidMount() {
this.props.setQuery( this.props.siteId, this.getQuery() );
}
componentDidUpdate( prevProps ) {
const nextQuery = this.getQuery();
if (
prevProps.siteId !== this.props.siteId ||
! isEqual( nextQuery, this.getQuery( prevProps ) )
) {
this.props.setQuery( this.props.siteId, nextQuery );
}
}
getQuery = ( props ) => {
const query = {};
props = props || this.props;
if ( props.search ) {
query.search = props.search;
}
if ( props.filter && ! props.source ) {
if ( props.filter === 'this-post' ) {
if ( props.postId ) {
query.post_ID = props.postId;
}
} else {
query.mime_type = utils.getMimeBaseTypeFromFilter( props.filter );
}
}
if ( props.source ) {
query.source = props.source;
query.path = 'recent';
if ( props.source === 'google_photos' ) {
if ( props.googlePhotosPickerSession ) {
query.session_id = props.googlePhotosPickerSession.id;
} else {
// Add any query params specific to Google Photos
return utils.getGoogleQuery( query, props );
}
}
}
return query;
};
fetchData = () => {
this.props.fetchNextMediaPage( this.props.siteId );
};
render() {
return passToChildren( this, {
mediaHasNextPage: this.props.hasNextPage,
mediaOnFetchNextPage: this.fetchData,
} );
}
}
MediaListData.defaultProps = {
setQuery: () => {},
};
const mapStateToProps = ( state, ownProps ) => ( {
media: getMediaSortedByDate( state, ownProps.siteId ),
hasNextPage: hasNextMediaPage( state, ownProps.siteId ),
googlePhotosPickerSession: getGooglePhotosPickerSession( state ),
} );
export default connect( mapStateToProps, { fetchNextMediaPage, setQuery } )( MediaListData );
|