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';
|
2018-12-18 13:33:01 +00:00
|
|
|
import { LOCATION_CHANGE } from 'connected-react-router';
|
2018-08-14 13:11:52 +00:00
|
|
|
import * as NOTIFICATION from 'actions/constants/notification';
|
2018-04-11 10:06:46 +00:00
|
|
|
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 = {
|
2019-03-04 12:33:02 +00:00
|
|
|
label: string,
|
|
|
|
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,
|
|
|
|
+type: string,
|
|
|
|
+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,
|
2018-04-11 10:06:46 +00:00
|
|
|
devicePath: payload.devicePath,
|
2018-02-20 09:30:36 +00:00
|
|
|
type: payload.type,
|
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') {
|
2018-04-11 10:06:46 +00:00
|
|
|
return state.filter(entry => entry.id !== payload.id);
|
2019-03-04 12:33:02 +00:00
|
|
|
}
|
|
|
|
if (payload && typeof payload.devicePath === 'string') {
|
2018-04-11 10:06:46 +00:00
|
|
|
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-04-11 10:06:46 +00:00
|
|
|
|
2018-02-20 09:30:36 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|