1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/reducers/NotificationReducer.js

84 lines
2.3 KiB
JavaScript
Raw Normal View History

2018-02-20 09:30:36 +00:00
/* @flow */
2018-07-30 10:52:13 +00:00
2018-10-11 12:50:34 +00:00
import * as React from 'react';
import { LOCATION_CHANGE } from 'connected-react-router';
2018-08-14 13:11:52 +00:00
import * as NOTIFICATION from 'actions/constants/notification';
import { DEVICE } from 'trezor-connect';
2018-02-20 09:30:36 +00:00
2018-08-14 12:56:47 +00:00
import type { Action } from 'flowtype';
2018-04-23 10:20:15 +00:00
export type CallbackAction = {
label: React.Node,
2019-03-04 12:33:02 +00:00
callback: Function,
};
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
export type NotificationEntry = {
2019-03-04 12:33:02 +00:00
+key: string, // React.Key
+id: ?string,
+devicePath: ?string,
+variant: string,
2019-03-04 12:33:02 +00:00
+title: React.Node | string,
+message: ?(React.Node | string),
+cancelable: boolean,
+actions: Array<CallbackAction>,
};
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
export type State = Array<NotificationEntry>;
const initialState: State = [
2018-02-20 09:30:36 +00:00
// {
// id: undefined,
// type: "info",
// title: "Some static notification",
// message: "This one is not cancelable",
// cancelable: false,
// actions: []
// }
];
2018-04-23 10:20:15 +00:00
const addNotification = (state: State, payload: any): State => {
const newState: State = state.filter(e => !e.cancelable);
2018-02-20 09:30:36 +00:00
newState.push({
2018-10-10 07:31:26 +00:00
key: new Date().getTime().toString(),
2018-02-20 09:30:36 +00:00
id: payload.id,
devicePath: payload.devicePath,
variant: payload.variant,
2018-10-11 12:50:34 +00:00
title: payload.title,
message: payload.message,
2018-02-20 09:30:36 +00:00
cancelable: payload.cancelable,
2018-07-30 10:52:13 +00:00
actions: payload.actions,
2018-02-20 09:30:36 +00:00
});
// TODO: sort
return newState;
2018-07-30 10:52:13 +00:00
};
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
const closeNotification = (state: State, payload: any): State => {
2018-02-20 09:30:36 +00:00
if (payload && typeof payload.id === 'string') {
return state.filter(entry => entry.id !== payload.id);
2019-03-04 12:33:02 +00:00
}
if (payload && typeof payload.devicePath === 'string') {
return state.filter(entry => entry.devicePath !== payload.devicePath);
2018-02-20 09:30:36 +00:00
}
2018-07-30 10:52:13 +00:00
return state.filter(entry => !entry.cancelable);
};
2018-02-20 09:30:36 +00:00
2018-04-23 10:20:15 +00:00
export default function notification(state: State = initialState, action: Action): State {
2018-07-30 10:52:13 +00:00
switch (action.type) {
2018-09-06 15:04:28 +00:00
case DEVICE.DISCONNECT: {
const { path } = action.device; // Flow warning
2018-04-23 10:20:15 +00:00
return state.filter(entry => entry.devicePath !== path);
2018-09-06 15:04:28 +00:00
}
2018-04-23 10:20:15 +00:00
2018-07-30 10:52:13 +00:00
case NOTIFICATION.ADD:
2018-02-20 09:30:36 +00:00
return addNotification(state, action.payload);
2018-07-30 10:52:13 +00:00
case LOCATION_CHANGE:
case NOTIFICATION.CLOSE:
2018-02-20 09:30:36 +00:00
return closeNotification(state, action.payload);
2018-02-20 09:30:36 +00:00
default:
return state;
}
}