Created
September 14, 2020 09:56
-
-
Save maxime-rainville/c09c7c47f48b64fbacb890ca9f7257fa to your computer and use it in GitHub Desktop.
Auto create Test Toast on page load
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, { useEffect } from 'react'; | |
import Toasts from 'components/Toasts/Toasts'; | |
import { connect } from 'react-redux'; | |
import { bindActionCreators } from 'redux'; | |
import * as toastsActions from 'state/toasts/ToastsActions'; | |
// Just here for testing | |
const types = ['info', 'success', 'warning', 'error']; | |
// Just here for testing | |
const magicToast = (display, idx) => ({ | |
text: 'I just spawn more of myself.', | |
type: types[idx % types.length], | |
actions: [ | |
{ label: 'Same type', onClick: () => display(magicToast(display, idx)) }, | |
{ label: 'Next type', onClick: () => display(magicToast(display, idx + 1)) } | |
] | |
}); | |
/** | |
* Wires the Toasts component into the redux store. | |
*/ | |
const ToastsContainer = ({ toasts, actions: { success, display, dismiss, pause, resume } }) => { | |
// Just here for testing | |
useEffect(() => { | |
success('The Toast container has been initialised.'); | |
display(magicToast(display, 0)); | |
}, []); | |
return (<Toasts | |
toasts={toasts} | |
onDismiss={dismiss} | |
onPause={pause} | |
onResume={resume} | |
/>); | |
}; | |
const mapStateToProps = ({ toasts }) => ({ toasts }); | |
const mapDispatchToProps = (dispatch) => ({ | |
actions: bindActionCreators(toastsActions, dispatch) | |
}); | |
export default connect(mapStateToProps, mapDispatchToProps)(ToastsContainer); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment