Created
February 21, 2018 15:55
-
-
Save Craga89/6234188c2e80143267611d780a2eadbc to your computer and use it in GitHub Desktop.
Add Toasters to AppContext
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 { USER } from 'globals'; | |
import PropTypes from 'prop-types'; | |
import uniqueId from 'lodash/uniqueId'; | |
import compose from 'recompact/compose'; | |
import withContext from 'recompact/withContext'; | |
import withReducer from 'recompact/withReducer'; | |
import injectCss from 'react/common/hocs/injectCss'; | |
import componentsCoreCssUrl from '@communicator/components.core/dist/communicator.components.core.css'; | |
if (process.env.NODE_ENV === 'development') { | |
window.perf = require('react/lib/ReactPerf'); | |
} | |
const AppContext = ({ | |
toasters, | |
children | |
}) => { | |
return ( | |
<div> | |
{children} | |
{toasters.map(({ id, ...toaster }) => | |
<Toaster key={id} {...toaster} /> | |
)} | |
</div> | |
); | |
}; | |
export default compose( | |
injectCss([ | |
componentsCoreCssUrl, | |
`${__webpack_public_path__}/${__OUTPUT_PATHS__.reactCss}` // eslint-disable-line camelcase | |
]), | |
withReducer( | |
'toasters', | |
'showToaster', | |
(state, action) => state.concat({ | |
type: action.type, | |
title: action.title, | |
text: action.text, | |
id: uniqueId('toaster') | |
}), | |
() => [] | |
), | |
withContext( | |
{ | |
user: PropTypes.object, | |
showToaster: PropTypes.func | |
}, | |
({ showToaster }) => ({ | |
showToaster, | |
user: USER | |
}) | |
) | |
) | |
(AppContext); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment