-
-
Notifications
You must be signed in to change notification settings - Fork 41
/
NotifyMe.js
222 lines (194 loc) · 8.1 KB
/
NotifyMe.js
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
import React, { useState, useEffect, useRef } from 'react';
import PropTypes from "prop-types";
import Overlay from 'react-bootstrap/Overlay';
import Popover from 'react-bootstrap/Popover';
import Button from 'react-bootstrap/Button';
import moment from 'moment';
import { reactLocalStorage } from 'reactjs-localstorage';
import { Bell, BookOpen, AlertTriangle } from 'react-feather';
import 'bootstrap/dist/css/bootstrap.min.css';
import './NotifyMe.scss';
const NotifyMe = props => {
moment.locale(navigator.languages[0].toLowerCase());
// State variabls
const [showCount, setShowCount] = useState(false);
const [messageCount, setMessageCount] = useState(0);
const [show, setShow] = useState(false);
const [target, setTarget] = useState(null);
const [raedIndex, setReadIndex] = useState(0);
// Useref for the overlay
const ref = useRef(null);
// Props passed to the component
const data = props.data;
const storageKey = props.storageKey || 'notification_timeline_storage_id';
const key = props.notific_key;
const notificationMsg = props.notific_value;
const sortedByKey = props.sortedByKey;
const heading = props.heading || 'Notifications';
const bellSize = props.size || 32;
const theme = props.theme || {color:'yellow',backgroundColor:'#282828'};
const multiLineSplitter = props.multiLineSplitter || '\n';
const showDate = props.showDate || false;
const Icon = props.icon || Bell;
useEffect(() => {
if (!sortedByKey) {
data.sort((a, b) => b[key] - a[key]);
}
// We read if any last read item id is in the local storage
let readItemLs = reactLocalStorage.getObject(storageKey);
let readMsgId = Object.keys(readItemLs).length > 0 ? readItemLs['id'] : '';
// if the id found, we check what is the index of that message in the array and query it. If not found,
// nothing has been read. Hence count should be same as all the message count.
let readIndex = (readMsgId === '') ? data.length : data.findIndex(elem => elem[key] === readMsgId);
// if the id is not found, it all flushed out and start again
readIndex = readIndex === -1 && data.length;
setReadIndex(readIndex);
// If there are messages and readIndex is pointing to at least one message, we will show the count bubble.
(data.length && readIndex) > 0 ? setShowCount(true) : setShowCount(false);
setMessageCount(readIndex);
}, [data]);
// Handle the click on the notification bell
const handleClick = (event) => {
setShow(!show);
setTarget(event.target);
}
// Calculate the day diff
const getDayDiff = timestamp => {
let a = moment();
let b = moment(timestamp);
let diff = a.diff(b, 'year');
if (diff === 0) {
diff = a.diff(b, 'month');
if (diff === 0) {
diff = a.diff(b, 'days');
if (diff === 0) {
diff = a.diff(b, 'hour');
if (diff === 0) {
diff = a.diff(b, 'minute');
if (diff === 0) {
diff = a.diff(b, 'second');
return `${diff} second(s) before`;
} else {
return `${diff} minute(s) before`;
}
} else {
return `${diff} hour(s) before`;
}
} else {
return `${diff} days(s) before`;
}
} else {
return `${diff} month(s) before`;
}
} else {
return `${diff} year(s) before`;
}
};
const getWhen = timestamp => {
let when = `${moment(timestamp).format('L')} ${moment(timestamp).format('LTS')}`;
return when;
}
// Get the notification message
const getContent = message => {
if (message.indexOf(multiLineSplitter) >= 0) {
let splitted = message.split(multiLineSplitter);
let ret = '<ul>';
for (let i = 0; i <= splitted.length - 1; i++) {
if (splitted[i] !== '') {
ret = ret + '<li>' + splitted[i] + '</li>';
}
}
ret = ret + '</ul>';
return {
__html: ret
};
}
return {
__html: `<ul><li>${message}</li></ul>`
};
};
// Hide the notification on clicking outside
const hide = () => {
setShow(false);
}
// Call the function when mark as read link is clicked
const markAsRead = () => {
setShowCount(false);
reactLocalStorage.setObject(storageKey, { 'id': data[0][key] });
setReadIndex(0);
}
return (
<>
<div className="notification-container">
<div className={`notification ${showCount ? 'notify show-count' : 'notify'}`}
style={{backgroundColor:theme.backgroundColor}}
data-count={messageCount}
onClick={event => handleClick(event)}>
<Icon color={theme.color} size={bellSize} />
</div>
</div>
<div ref={ref}>
<Overlay
show={show}
target={target}
placement="bottom"
container={ref.current}
containerPadding={20}
rootClose={true}
onHide={hide}
>
<Popover id="popover-contained">
<Popover.Title as="h3">{heading}</Popover.Title>
<Popover.Content style={{ padding: '3px 3px' }}>
{showCount && <div>
<Button variant="link" onClick={props.markAsReadFn || markAsRead}>
<BookOpen size={24} />
Mark all as read
</Button>
</div>
}
<ul className="notification-info-panel">
{
data.length > 0 ?
data.map((message, index) =>
<li
className={index < raedIndex ? 'notification-message unread' : 'notification-message'}
key={index}>
<div className="timestamp">
<span>{getDayDiff(message[key])}</span>
{showDate && <span>{' ('}{getWhen(message[key])}{')'}</span>}
</div>
<div className="content" dangerouslySetInnerHTML={getContent(message[notificationMsg])} />
</li>
) :
<>
<AlertTriangle color='#000000' size={32} />
<h5 className="nodata">No Notifications found!</h5>
</>
}
</ul>
</Popover.Content>
</Popover>
</Overlay>
</div>
</>
)
};
NotifyMe.propTypes = {
storageKey: PropTypes.string,
notific_key: PropTypes.string.isRequired,
data: PropTypes.array.isRequired,
notific_value: PropTypes.string.isRequired,
sortedByKey: PropTypes.bool,
theme: PropTypes.shape({
color: PropTypes.string,
backgroundColor: PropTypes.string
}),
size: PropTypes.string,
heading: PropTypes.string,
multiLineSplitter: PropTypes.string,
showDate: PropTypes.bool,
markAsReadFn: PropTypes.func,
icon: PropTypes.func
}
export default NotifyMe;