Update notifier and notifications reducer

This commit is contained in:
Germán Martínez 2019-09-26 15:14:24 +02:00
parent 486122d004
commit b51aecafad
5 changed files with 16 additions and 19 deletions

View File

@ -35,24 +35,24 @@ class Notifier extends Component {
componentDidUpdate() {
const { notifications = [], enqueueSnackbar, removeSnackbar } = this.props
notifications.map(({ key, message, options = {} }) => {
notifications.forEach((notification) => {
// Do nothing if snackbar is already displayed
if (this.displayed.includes(key)) {
if (this.displayed.includes(notification.key)) {
return
}
// Display snackbar using notistack
enqueueSnackbar(message, {
...options,
enqueueSnackbar(notification.message, {
...notification.options,
onClose: (event, reason, key) => {
if (options.onClose) {
options.onClose(event, reason, key)
if (notification.options.onClose) {
notification.options.onClose(event, reason, key)
}
// Dispatch action to remove snackbar from redux store
removeSnackbar(key)
},
})
// Keep track of snackbars that we've displayed
this.storeDisplayed(key)
this.storeDisplayed(notification.key)
})
}

View File

@ -6,9 +6,7 @@ import { type NotificationProps } from '~/logic/notifications/store/models/notif
export const ENQUEUE_SNACKBAR = 'ENQUEUE_SNACKBAR'
const addSnackbar = createAction<string, *, *>(ENQUEUE_SNACKBAR, (notification: NotificationProps): ActionReturn => ({
notification,
}))
const addSnackbar = createAction<string, *>(ENQUEUE_SNACKBAR)
const enqueueSnackbar = (notification: NotificationProps) => (
dispatch: ReduxDispatch<GlobalState>,
@ -16,7 +14,7 @@ const enqueueSnackbar = (notification: NotificationProps) => (
) => {
const newNotification = {
...notification,
key: new Date().getTime() + Math.random(),
key: new Date().getTime(),
}
dispatch(addSnackbar(newNotification))
}

View File

@ -3,12 +3,14 @@ import { Record } from 'immutable'
import type { RecordFactory, RecordOf } from 'immutable'
export type NotificationProps = {
key?: number,
message: string,
options: Object,
dismissed: boolean,
}
export const makeNotification: RecordFactory<NotificationProps> = Record({
key: 0,
message: '',
options: {},
dismissed: false,

View File

@ -13,23 +13,20 @@ export type NotificationReducerState = Map<string, *>
export default handleActions<NotificationReducerState, *>(
{
[ENQUEUE_SNACKBAR]: (state: NotificationReducerState, action: ActionType<Function>): NotificationReducerState => {
const { notification }: { notification: NotificationProps } = action.payload
const notification: NotificationProps = action.payload
if (state.hasIn(['notifications', notification.options.key])) {
return state.updateIn(['notifications', notification.options.key], (prev) => prev.merge(notification))
}
return state.setIn(['notifications', notification.options.key], makeNotification(notification))
return state.set(notification.key, makeNotification(notification))
},
[CLOSE_SNACKBAR]: (state: NotificationReducerState, action: ActionType<Function>): NotificationReducerState => {
const { notification }: { notification: NotificationProps } = action.payload
notification.dismissed = true
return state.updateIn(['notifications', notification.key], (prev) => prev.merge(notification))
return state.update(notification.key, (prev) => prev.merge(notification))
},
[REMOVE_SNACKBAR]: (state: NotificationReducerState, action: ActionType<Function>): NotificationReducerState => {
const key = action.payload
return state.deleteIn(['notification', key])
return state.delete(key)
},
},
Map({

View File

@ -47,7 +47,7 @@ const handleProviderNotification = (dispatch: ReduxDispatch<*>, provider: Provid
// you SHOULD pass your own `key` in the options. `key` can be any sequence
// of number or characters, but it has to be unique to a given snackbar.
//dispatch(enqueueSnackbarAction(NOTIFICATIONS.WALLET_CONNECTED_MSG))
// dispatch(enqueueSnackbarAction(NOTIFICATIONS.WALLET_CONNECTED_MSG))
enqueueSnackbar(NOTIFICATIONS.WALLET_CONNECTED_MSG.message, NOTIFICATIONS.WALLET_CONNECTED_MSG.options)
} else {
enqueueSnackbar(NOTIFICATIONS.UNLOCK_WALLET_MSG.message, NOTIFICATIONS.UNLOCK_WALLET_MSG.options)