230 lines
5.7 KiB
JavaScript
Raw Normal View History

/*
*
* HomePage
*
*/
import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
2018-02-22 12:08:11 +01:00
// import { createStructuredSelector } from 'reselect';
import { injectIntl } from 'react-intl';
import { bindActionCreators, compose } from 'redux';
2018-02-23 12:21:57 +01:00
import { isEmpty } from 'lodash';
// You can find these components in either
// ./node_modules/strapi-helper-plugin/lib/src
// or strapi/packages/strapi-helper-plugin/lib/src
import ContainerFluid from 'components/ContainerFluid';
import InputSearch from 'components/InputSearch';
// import InputSelect from 'components/InputSelect';
import PageFooter from 'components/PageFooter';
import PluginHeader from 'components/PluginHeader';
// Plugin's component
import EntriesNumber from 'components/EntriesNumber';
2018-02-16 16:53:01 +01:00
import List from 'components/List';
import PluginInputFile from 'components/PluginInputFile';
// Utils
2018-02-23 12:21:57 +01:00
import getQueryParameters from 'utils/getQueryParameters';
import injectReducer from 'utils/injectReducer';
import injectSaga from 'utils/injectSaga';
// Actions
import {
changeParams,
2018-02-19 15:14:32 +01:00
deleteData,
getData,
2018-02-19 15:14:32 +01:00
onDrop,
onSearch,
2018-02-23 12:21:57 +01:00
setParams,
} from './actions';
// Selectors
import selectHomePage from './selectors';
// Styles
2018-02-22 11:43:45 +01:00
import styles from './styles.scss';
import reducer from './reducer';
import saga from './saga';
export class HomePage extends React.Component {
2018-02-19 15:14:32 +01:00
getChildContext = () => (
{
deleteData: this.props.deleteData,
}
);
2018-02-23 12:21:57 +01:00
componentWillMount() {
if (!isEmpty(this.props.location.search)) {
const page = parseInt(this.getURLParams('page'), 10);
const limit = parseInt(this.getURLParams('limit'), 10);
const sort = this.getURLParams('sort');
this.props.setParams({ limit, page, sort });
}
}
componentDidMount() {
this.props.getData();
}
2018-02-19 15:14:32 +01:00
componentWillReceiveProps(nextProps) {
if (nextProps.deleteSuccess !== this.props.deleteSuccess) {
this.props.getData();
}
2018-02-23 12:21:57 +01:00
if (nextProps.location.search !== this.props.location.search) {
this.props.getData();
}
2018-02-19 15:14:32 +01:00
}
2018-02-23 12:21:57 +01:00
getURLParams = (type) => getQueryParameters(this.props.location.search, type);
changeSort = (name) => {
const { params: { limit, page } } = this.props;
const target = {
name: 'params.sort',
value: name,
};
const search = `page=${page}&limit=${limit}&sort=${name}`;
this.props.changeParams({ target });
this.props.history.push({
pathname: this.props.history.pathname,
search,
});
}
handleChangeParams = (e) => {
const { history, params } = this.props;
2018-02-20 19:20:31 +01:00
const search = e.target.name === 'params.limit' ?
`page=${params.page}&limit=${e.target.value}&sort=${params.sort}`
2018-02-22 12:08:11 +01:00
: `page=${e.target.value}&limit=${params.limit}&sort=${params.sort}`;
this.props.history.push({
pathname: history.pathname,
search,
});
this.props.changeParams(e);
}
2018-02-22 12:31:41 +01:00
renderInputSearch = () => (
<InputSearch
autoFocus
name="search"
onChange={this.props.onSearch}
placeholder="upload.HomePage.InputSearch.placeholder"
style={{ marginTop: '-10px' }}
value={this.props.search}
/>
2018-02-22 12:31:41 +01:00
)
render() {
return (
<ContainerFluid>
<div className={styles.homePageUpload}>
<PluginHeader
title={{
id: 'upload.HomePage.title',
}}
description={{
id: 'upload.HomePage.description',
}}
overrideRendering={this.renderInputSearch}
/>
</div>
2018-02-22 12:08:11 +01:00
<PluginInputFile
name="files"
onDrop={this.props.onDrop}
/>
<div className={styles.entriesWrapper}>
<div>
{/* NOTE: Prepare for bulk actions}
<InputSelect
name="bulkAction"
onChange={() => console.log('change')}
selectOptions={[{ value: 'select all'}]}
2018-02-16 16:53:01 +01:00
style={{ minWidth: '200px', height: '32px', marginTop: '-8px' }}
/>
*/}
</div>
<EntriesNumber number={this.props.entriesNumber} />
</div>
2018-02-19 13:10:35 +01:00
<List
data={this.props.uploadedFiles}
changeSort={this.changeSort}
sort={this.props.params.sort}
2018-02-19 13:10:35 +01:00
/>
<div className="col-md-12">
<PageFooter
2018-02-19 17:41:05 +01:00
count={this.props.entriesNumber}
onChangeParams={this.handleChangeParams}
params={this.props.params}
/>
</div>
</ContainerFluid>
);
}
}
2018-02-19 15:14:32 +01:00
HomePage.childContextTypes = {
deleteData: PropTypes.func.isRequired,
};
HomePage.contextTypes = {
router: PropTypes.object,
};
HomePage.defaultProps = {
params: {
limit: 10,
2018-02-20 19:20:31 +01:00
page: 1,
sort: 'updatedAt',
},
uploadedFiles: [],
};
HomePage.propTypes = {
changeParams: PropTypes.func.isRequired,
2018-02-22 12:08:11 +01:00
deleteData: PropTypes.func.isRequired,
deleteSuccess: PropTypes.bool.isRequired,
entriesNumber: PropTypes.number.isRequired,
getData: PropTypes.func.isRequired,
2018-02-22 12:08:11 +01:00
history: PropTypes.object.isRequired,
2018-02-23 12:21:57 +01:00
location: PropTypes.object.isRequired,
2018-02-16 14:17:24 +01:00
onDrop: PropTypes.func.isRequired,
onSearch: PropTypes.func.isRequired,
2018-02-22 12:08:11 +01:00
params: PropTypes.object,
search: PropTypes.string.isRequired,
2018-02-23 12:21:57 +01:00
setParams: PropTypes.func.isRequired,
2018-02-22 12:08:11 +01:00
uploadedFiles: PropTypes.arrayOf(PropTypes.object),
};
function mapDispatchToProps(dispatch) {
return bindActionCreators(
{
changeParams,
2018-02-19 15:14:32 +01:00
deleteData,
getData,
2018-02-19 15:14:32 +01:00
onDrop,
onSearch,
2018-02-23 12:21:57 +01:00
setParams,
},
dispatch,
);
}
const mapStateToProps = selectHomePage();
const withConnect = connect(mapStateToProps, mapDispatchToProps);
const withReducer = injectReducer({ key: 'homePage', reducer });
const withSaga = injectSaga({ key: 'homePage', saga });
export default compose(
withReducer,
withSaga,
withConnect,
)(injectIntl(HomePage));