forked from benmvp/react-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (49 loc) · 1.64 KB
/
index.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
import * as api from '../api';
const setUnread = (emails, emailId, unread) => (
api.setUnread(emailId, unread)
.then(({success}) => {
if (success) {
return emails.map((email) => (
email.id === emailId
? {...email, unread}
: email
));
}
throw new Error(`Unable to set email ID# ${emailId} unread state to ${unread}.`);
})
.catch((ex) => console.error(ex))
);
export const getEmails = () => (
api.getEmails()
.catch((ex) => console.error(ex))
);
export const markRead = (emails, emailId) => setUnread(emails, emailId, false);
export const markUnread = (emails, emailId) => setUnread(emails, emailId, true);
export const deleteEmail = (emails, emailId) => (
api.deleteEmail(emailId)
.then(({success}) => {
if (success) {
return emails.filter((email) => email.id !== emailId);
}
throw new Error(`Unable to delete email ID# ${emailId}.`);
})
.catch((ex) => console.error(ex))
);
export const addEmail = (emails, newEmail) => (
api.addEmail(newEmail)
.then(({success}) => {
if (success) {
return [
{
...newEmail,
id: Date.now(),
date: `${new Date()}`,
unread: true
},
...emails
];
}
throw new Error('Unable to send email!');
})
.catch((ex) => console.error(ex))
);