Skip to content

Instantly share code, notes, and snippets.

@Craga89
Created February 21, 2018 15:55
Show Gist options
  • Save Craga89/6234188c2e80143267611d780a2eadbc to your computer and use it in GitHub Desktop.
Save Craga89/6234188c2e80143267611d780a2eadbc to your computer and use it in GitHub Desktop.
Add Toasters to AppContext
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