You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trezor-wallet/src/reducers/NotificationReducer.js

80 lines
2.1 KiB

6 years ago
/* @flow */
6 years ago
import { LOCATION_CHANGE } from 'react-router-redux';
import * as NOTIFICATION from 'actions/constants/notification';
import { DEVICE } from 'trezor-connect';
6 years ago
import type { Action } from 'flowtype';
6 years ago
export type CallbackAction = {
6 years ago
label: string;
callback: Function;
6 years ago
}
6 years ago
export type NotificationEntry = {
6 years ago
+id: ?string;
+devicePath: ?string;
6 years ago
+type: string;
+title: string;
+message: string;
+cancelable: boolean;
6 years ago
+actions: Array<CallbackAction>;
6 years ago
}
6 years ago
export type State = Array<NotificationEntry>;
const initialState: State = [
6 years ago
// {
// id: undefined,
// type: "info",
// title: "Some static notification",
// message: "This one is not cancelable",
// cancelable: false,
// actions: []
// }
];
6 years ago
const addNotification = (state: State, payload: any): State => {
const newState: State = state.filter(e => !e.cancelable);
6 years ago
newState.push({
id: payload.id,
devicePath: payload.devicePath,
6 years ago
type: payload.type,
title: payload.title.toString(),
message: payload.message.toString(),
cancelable: payload.cancelable,
actions: payload.actions,
6 years ago
});
// TODO: sort
return newState;
};
6 years ago
6 years ago
const closeNotification = (state: State, payload: any): State => {
6 years ago
if (payload && typeof payload.id === 'string') {
return state.filter(entry => entry.id !== payload.id);
} if (payload && typeof payload.devicePath === 'string') {
return state.filter(entry => entry.devicePath !== payload.devicePath);
6 years ago
}
return state.filter(entry => !entry.cancelable);
};
6 years ago
6 years ago
export default function notification(state: State = initialState, action: Action): State {
switch (action.type) {
case DEVICE.DISCONNECT:
6 years ago
const path: string = action.device.path; // Flow warning
return state.filter(entry => entry.devicePath !== path);
case NOTIFICATION.ADD:
6 years ago
return addNotification(state, action.payload);
case LOCATION_CHANGE:
case NOTIFICATION.CLOSE:
6 years ago
return closeNotification(state, action.payload);
6 years ago
default:
return state;
}
}