-
Notifications
You must be signed in to change notification settings - Fork 295
/
AlertsOverlay.tsx
130 lines (118 loc) · 3.51 KB
/
AlertsOverlay.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// @flow
import React, { Component } from 'react';
import moment from 'moment';
import { observer } from 'mobx-react';
import { get } from 'lodash';
import ReactMarkdown from 'react-markdown';
import { ButtonSkin } from 'react-polymorph/lib/skins/simple/ButtonSkin';
import News from '../../domains/News';
import DialogCloseButton from '../widgets/DialogCloseButton';
import closeCrossThin from '../../assets/images/close-cross-thin.inline.svg';
import ButtonLink from '../widgets/ButtonLink';
import styles from './AlertsOverlay.scss';
type State = {
showOverlay: boolean,
};
type Props = {
alerts: Array<News.News>,
onCloseOpenAlert: Function,
onMarkNewsAsRead: Function,
onOpenExternalLink: Function,
onProceedNewsAction: Function,
allAlertsCount: number,
hideCounter?: boolean,
currentDateFormat: string,
};
@observer
export default class AlertsOverlay extends Component<Props, State> {
constructor(props: Props) {
super(props);
this.state = {
showOverlay: true,
};
}
localizedDateFormat: 'MM/DD/YYYY';
componentDidMount() {
this.localizedDateFormat = moment.localeData().longDateFormat('L');
}
contentClickHandler(event: SyntheticMouseEvent<HTMLElement>) {
const linkUrl = get(event, ['target', 'href']);
if (linkUrl) {
event.preventDefault();
this.props.onOpenExternalLink(linkUrl);
}
}
onClose = () => {
const { alerts, onMarkNewsAsRead, onCloseOpenAlert } = this.props;
if (alerts.length <= 1) {
onMarkNewsAsRead([alerts[0].id]);
onCloseOpenAlert();
this.setState({ showOverlay: false });
return;
}
onMarkNewsAsRead([alerts[0].id]);
};
onProceedNewsAction = (event: SyntheticMouseEvent<HTMLElement>) => {
const { onProceedNewsAction, alerts } = this.props;
onProceedNewsAction(alerts[0], event);
};
renderAction = (action: Object) => {
if (action && (action.url || action.event)) {
return (
<ButtonLink
className={styles.actionBtn}
onClick={this.onProceedNewsAction}
skin={ButtonSkin}
label={action.label}
linkProps={{
className: styles.externalLink,
hasIconBefore: false,
hasIconAfter: action.url && true,
}}
/>
);
}
return null;
};
renderCounter = (alerts: Array<News.News>) => {
const { allAlertsCount, hideCounter } = this.props;
if (!hideCounter && allAlertsCount > 1) {
return (
<span className={styles.counter}>
{allAlertsCount - alerts.length + 1} / {allAlertsCount}
</span>
);
}
return null;
};
render() {
const { showOverlay } = this.state;
const { alerts, currentDateFormat } = this.props;
const [alert] = alerts;
const { content, date, action, title } = alert;
return (
showOverlay && (
<div className={styles.component}>
<DialogCloseButton
className={styles.closeButton}
icon={closeCrossThin}
onClose={this.onClose}
/>
{this.renderCounter(alerts)}
<h1 className={styles.title}>{title}</h1>
<span className={styles.date}>
{moment(date).format(currentDateFormat)}
</span>
<div
className={styles.content}
role="presentation"
onClick={this.contentClickHandler.bind(this)}
>
<ReactMarkdown escapeHtml={false} source={content} />
</div>
{this.renderAction(action)}
</div>
)
);
}
}