Last active
December 6, 2016 00:53
-
-
Save mattd/630bce9f95558d91ea052a8423c6772c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
const getDisplayName = (Component) => { | |
return Component.displayName || Component.name || 'Component' | |
}; | |
export const makeController = (willMount, willUnmount) => { | |
return (Component) => { | |
const noop = () => {}; | |
class Controller extends React.Component { | |
componentWillMount() { | |
willMount ? willMount(this.props) : noop(); | |
} | |
componentWillUnmount() { | |
willUnmount ? willUnmount(this.props) : noop(); | |
} | |
render() { | |
return ( | |
<Component {...this.props} /> | |
); | |
} | |
} | |
Controller.displayName = `Controller(${getDisplayName(Component)})`; | |
return Controller; | |
}; | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { connect } from 'react-redux'; | |
import { makeController } from '../controllers'; | |
import { getTermsPath } from '../services/terms'; | |
import { | |
subscribeToTerms, | |
unsubscribeFromTerms, | |
flushTerms | |
} from '../actions/creators/terms'; | |
import { Feature } from '../components/containers'; | |
const mapStateToProps = (state) => { | |
return { | |
db: state.db, | |
uid: state.profile.uid | |
}; | |
}; | |
const willMount = ({ | |
db, | |
uid, | |
dispatch | |
}) => { | |
if (!db[getTermsPath(uid)]) { | |
dispatch(subscribeToTerms(uid)); | |
} | |
}; | |
const willUnmount = ({ | |
uid, | |
dispatch | |
}) => { | |
if (uid) dispatch(unsubscribeFromTerms(uid)); | |
dispatch(flushTerms()); | |
}; | |
const Terms = () => { | |
return ( | |
<Feature title="Terms" /> | |
); | |
}; | |
export default connect(mapStateToProps)( | |
makeController(willMount, willUnmount)(Terms) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment