Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add toast notifications capability (#469)
Add toast notifications capability
- Loading branch information
1 parent
4e4f24b
commit 94a354c
Showing
6 changed files
with
150 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
81 changes: 81 additions & 0 deletions
81
client/src/components/toastNotificationList/toastNotificatinsList.js
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
import { connect } from 'react-redux'; | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { | ||
ToastNotificationList, | ||
TimedToastNotification | ||
} from 'patternfly-react'; | ||
|
||
import { bindMethods } from '../../common/helpers'; | ||
import Store from '../../redux/store'; | ||
import { toastNotificationTypes } from '../../redux/constants'; | ||
|
||
class ToastNotificationsList extends React.Component { | ||
constructor() { | ||
super(); | ||
|
||
bindMethods(this, ['onHover', 'onLeave', 'onDismiss']); | ||
} | ||
|
||
onHover() { | ||
Store.dispatch({ type: toastNotificationTypes.TOAST_PAUSE }); | ||
} | ||
|
||
onLeave() { | ||
Store.dispatch({ type: toastNotificationTypes.TOAST_RESUME }); | ||
} | ||
|
||
onDismiss(toast) { | ||
Store.dispatch({ | ||
type: toastNotificationTypes.TOAST_REMOVE, | ||
toast: toast | ||
}); | ||
} | ||
|
||
render() { | ||
const { toasts, paused } = this.props; | ||
|
||
return ( | ||
<ToastNotificationList> | ||
{toasts && | ||
toasts.map((toast, index) => { | ||
if (!toast.removed) { | ||
return ( | ||
<TimedToastNotification | ||
key={index} | ||
toastIndex={index} | ||
type={toast.alertType} | ||
paused={paused} | ||
onDismiss={e => this.onDismiss(toast)} | ||
onMouseEnter={this.onHover} | ||
onMouseLeave={this.onLeave} | ||
> | ||
<span> | ||
<strong>{toast.header}</strong> | ||
{toast.message} | ||
</span> | ||
</TimedToastNotification> | ||
); | ||
} else { | ||
return null; | ||
} | ||
})} | ||
</ToastNotificationList> | ||
); | ||
} | ||
} | ||
|
||
ToastNotificationsList.propTypes = { | ||
toasts: PropTypes.array, | ||
paused: PropTypes.bool | ||
}; | ||
|
||
function mapStateToProps(state, ownProps) { | ||
return { | ||
toasts: state.toastNotifications.toasts, | ||
paused: state.toastNotifications.paused | ||
}; | ||
} | ||
|
||
export default connect(mapStateToProps)(ToastNotificationsList); |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
const TOAST_ADD = 'TOAST_ADD'; | ||
const TOAST_REMOVE = 'TOAST_REMOVE'; | ||
const TOAST_PAUSE = 'TOAST_PAUSE'; | ||
const TOAST_RESUME = 'TOAST_RESUME'; | ||
|
||
export { TOAST_ADD, TOAST_REMOVE, TOAST_PAUSE, TOAST_RESUME }; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { toastNotificationTypes } from '../constants'; | ||
|
||
const initialState = { | ||
toasts: [], | ||
paused: false | ||
}; | ||
|
||
export default function toastNotificationsReducer( | ||
state = initialState, | ||
action | ||
) { | ||
switch (action.type) { | ||
case toastNotificationTypes.TOAST_ADD: | ||
let newToast = { | ||
header: action.header, | ||
message: action.message, | ||
alertType: action.alertType, | ||
persistent: action.persistent | ||
}; | ||
return Object.assign({}, state, { | ||
toasts: [...state.toasts, newToast], | ||
displayedToasts: state.displayedToasts + 1 | ||
}); | ||
|
||
case toastNotificationTypes.TOAST_REMOVE: | ||
let index = state.toasts.indexOf(action.toast); | ||
action.toast.removed = true; | ||
|
||
let displayedToast = state.toasts.find(toast => { | ||
return !toast.removed; | ||
}); | ||
|
||
if (!displayedToast) { | ||
return Object.assign({}, state, { toasts: [] }); | ||
} | ||
|
||
return Object.assign({}, state, { | ||
toasts: [ | ||
...state.toasts.slice(0, index), | ||
action.toast, | ||
...state.toasts.slice(index + 1) | ||
] | ||
}); | ||
|
||
case toastNotificationTypes.TOAST_PAUSE: | ||
return Object.assign({}, state, { paused: true }); | ||
|
||
case toastNotificationTypes.TOAST_RESUME: | ||
return Object.assign({}, state, { paused: false }); | ||
|
||
default: | ||
return state; | ||
} | ||
} |