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/components/Notification/index.js

86 lines
2.6 KiB

6 years ago
/* @flow */
6 years ago
import React from 'react';
import { H2 } from 'components/Heading';
6 years ago
import { bindActionCreators } from 'redux';
import { connect } from 'react-redux';
import * as NOTIFICATION from 'actions/constants/notification';
import * as NotificationActions from 'actions/NotificationActions';
import type { Action, State, Dispatch } from 'flowtype';
import Loader from 'components/Loader';
6 years ago
type Props = {
notifications: $ElementType<State, 'notifications'>,
close: (notif?: any) => Action
}
6 years ago
type NProps = {
key?: number;
className: string;
cancelable?: boolean;
title: string;
message?: string;
actions?: Array<any>;
close?: typeof NotificationActions.close,
loading?: boolean
}
6 years ago
export const Notification = (props: NProps): React$Element<string> => {
const className = `notification ${props.className}`;
const close: Function = typeof props.close === 'function' ? props.close : () => {}; // TODO: add default close action
const actionButtons = props.actions ? props.actions.map((a, i) => (
<button key={i} onClick={(event) => { close(); a.callback(); }} className="transparent">{ a.label }</button>
)) : null;
6 years ago
return (
<div className={className}>
6 years ago
{ props.cancelable ? (
<button
className="notification-close transparent"
onClick={event => close()}
/>
6 years ago
) : null }
<div className="notification-body">
<H2>{ props.title }</H2>
{ props.message ? (<p dangerouslySetInnerHTML={{ __html: props.message }} />) : null }
6 years ago
</div>
{ props.actions && props.actions.length > 0 ? (
<div className="notification-action">
{ actionButtons }
</div>
) : null }
{ props.loading ? (
<Loader
size={50}
/>
) : null }
6 years ago
</div>
);
};
6 years ago
export const NotificationGroup = (props: Props) => {
6 years ago
const { notifications, close } = props;
return notifications.map((n, i) => (
<Notification
key={i}
className={n.type}
title={n.title}
message={n.message}
cancelable={n.cancelable}
actions={n.actions}
close={close}
/>
));
};
6 years ago
export default connect(
(state: State) => ({
notifications: state.notifications,
}),
(dispatch: Dispatch) => ({
close: bindActionCreators(NotificationActions.close, dispatch),
}),
6 years ago
)(NotificationGroup);