Spaces:
Sleeping
Sleeping
File size: 2,032 Bytes
540b600 |
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 |
import bindAll from 'lodash.bindall';
import PropTypes from 'prop-types';
import React from 'react';
import {connect} from 'react-redux';
import {setRestore} from '../reducers/restore-deletion';
/**
* DeletionRestorer component passes a restoreDeletion function to its child.
* It expects this child to be a function with the signature
* function (restoreDeletion, props) {}
* The component can then be used to attach deletion restoring functionality
* to any other component:
*
* <DeletionRestorer>{(restoreDeletion, props) => (
* <MyCoolComponent
* onClick={restoreDeletion}
* {...props}
* />
* )}</DeletionRestorer>
*/
class DeletionRestorer extends React.Component {
constructor (props) {
super(props);
bindAll(this, [
'restoreDeletion'
]);
}
restoreDeletion () {
if (typeof this.props.restore === 'function') {
this.props.restore();
this.props.dispatchUpdateRestore({restoreFun: null, deletedItem: ''});
}
}
render () {
const {
/* eslint-disable no-unused-vars */
children,
dispatchUpdateRestore,
/* eslint-enable no-unused-vars */
...props
} = this.props;
const restorable = typeof this.props.restore === 'function';
return this.props.children(this.restoreDeletion, {
...props,
restorable
});
}
}
DeletionRestorer.propTypes = {
children: PropTypes.func,
deletedItem: PropTypes.string,
dispatchUpdateRestore: PropTypes.func,
restore: PropTypes.func
};
const mapStateToProps = state => ({
deletedItem: state.scratchGui.restoreDeletion.deletedItem,
restore: state.scratchGui.restoreDeletion.restoreFun
});
const mapDispatchToProps = dispatch => ({
dispatchUpdateRestore: updatedState => {
dispatch(setRestore(updatedState));
}
});
export default connect(
mapStateToProps,
mapDispatchToProps
)(DeletionRestorer);
|