|
|
@@ -0,0 +1,98 @@
|
|
|
+import { connect } from 'react-redux';
|
|
|
+import React, { Component } from 'react';
|
|
|
+import PropTypes from 'prop-types';
|
|
|
+import uniqid from 'uniqid';
|
|
|
+
|
|
|
+import { getDocs } from 'selectors/crud';
|
|
|
+
|
|
|
+import {
|
|
|
+ docCreated,
|
|
|
+ docRead,
|
|
|
+ docUpdated,
|
|
|
+ docDeleted
|
|
|
+} from 'actions/crud';
|
|
|
+
|
|
|
+import CrudDocument from 'components/CrudDocument';
|
|
|
+
|
|
|
+const mapStateToProps = (state, { route }) => ({
|
|
|
+ items: getDocs(state, route)
|
|
|
+});
|
|
|
+
|
|
|
+const mapDispatchToProps = dispatch => ({
|
|
|
+ onCreate: (route, fields) => {
|
|
|
+ const pendingId = uniqid();
|
|
|
+
|
|
|
+ dispatch(docCreated(route, pendingId, fields));
|
|
|
+ },
|
|
|
+ onRead: route => dispatch(docRead(route)),
|
|
|
+ onUpdate: (route, id, fields) => dispatch(docUpdated(route, id, fields)),
|
|
|
+ onDelete: (route, id) => dispatch(docDeleted(route, id))
|
|
|
+});
|
|
|
+
|
|
|
+export default
|
|
|
+@connect(mapStateToProps, mapDispatchToProps)
|
|
|
+class CrudList extends Component {
|
|
|
+ static propTypes = {
|
|
|
+ title: PropTypes.string.isRequired,
|
|
|
+ route: PropTypes.string.isRequired,
|
|
|
+ docFields: PropTypes.object.isRequired,
|
|
|
+ items: PropTypes.array.isRequired,
|
|
|
+ onCreate: PropTypes.func.isRequired,
|
|
|
+ onRead: PropTypes.func.isRequired,
|
|
|
+ onUpdate: PropTypes.func.isRequired,
|
|
|
+ onDelete: PropTypes.func.isRequired
|
|
|
+ };
|
|
|
+
|
|
|
+ onRefresh = () => {
|
|
|
+ this.props.onRead(this.props.route);
|
|
|
+ };
|
|
|
+
|
|
|
+ onUpdate = (id, fields) => {
|
|
|
+ this.props.onUpdate(this.props.route, id, fields);
|
|
|
+ };
|
|
|
+
|
|
|
+ onDelete = id => {
|
|
|
+ this.props.onDelete(this.props.route, id);
|
|
|
+ };
|
|
|
+
|
|
|
+ componentDidMount() {
|
|
|
+ this.onRefresh();
|
|
|
+ }
|
|
|
+
|
|
|
+ render() {
|
|
|
+ const {
|
|
|
+ title,
|
|
|
+ items,
|
|
|
+ docFields
|
|
|
+ } = this.props;
|
|
|
+
|
|
|
+ const docsList = items.map(({ id, pending, ...fields }) => (
|
|
|
+ <CrudDocument key={id}
|
|
|
+ id={id}
|
|
|
+ pending={pending}
|
|
|
+ fields={fields}
|
|
|
+ docFields={docFields}
|
|
|
+ onUpdate={this.onUpdate}
|
|
|
+ onDelete={this.onDelete}
|
|
|
+ />
|
|
|
+ ));
|
|
|
+
|
|
|
+ return (
|
|
|
+ <div className="crud-list-wrapper">
|
|
|
+ <div className="head">
|
|
|
+ <h3 className="title">{title}</h3>
|
|
|
+ <div className="meta">
|
|
|
+ <button className="button-refresh"
|
|
|
+ onClick={this.onRefresh}>
|
|
|
+ {'Refresh'}
|
|
|
+ </button>
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ <div className="body">
|
|
|
+ {docsList}
|
|
|
+ </div>
|
|
|
+ </div>
|
|
|
+ );
|
|
|
+ }
|
|
|
+}
|
|
|
+
|