forked from benmvp/react-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
233 lines (199 loc) · 7.83 KB
/
App.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
223
224
225
226
227
228
229
230
231
232
233
import React from 'react';
import find from 'lodash/find';
import isEqual from 'lodash/isEqual';
import * as api from '../api';
import EmailList from '../components/EmailList';
import EmailView from '../components/EmailView';
import EmailForm from '../components/EmailForm';
import './App.scss';
const EmailViewWrapper = ({selectedEmail, onClose, onDelete, onMarkUnread, onMarkRead}) => {
let component = null;
if (selectedEmail) {
component = (
<article className="app__view">
<EmailView
email={selectedEmail}
onClose={onClose}
onDelete={onDelete}
onMarkUnread={onMarkUnread}
onMarkRead={onMarkRead}
/>
</article>
);
}
return component;
};
const EmailFormWrapper = ({showForm, onSubmit, onCancel}) => {
let component = null;
if (showForm) {
component = (
<div className="app__form-modal">
<div className="app__form">
<EmailForm onSubmit={onSubmit} onCancel={onCancel} />
</div>
</div>
);
}
return component;
};
export default class EmailApp extends React.Component {
static propTypes = {
pollInterval: React.PropTypes.number
}
static defaultProps = {
pollInterval: 2000
}
state = {
emails: [],
selectedEmailId: -1,
showForm: false
}
componentDidMount() {
// Retrieve emails from server once we know DOM exists
this._getUpdateEmails();
// Set up long-polling to continuously get new data
this._pollId = setInterval(
() => this._getUpdateEmails(),
this.props.pollInterval
);
}
componentWillUnmount() {
// Need to remember to clearInterval when the component gets
// removed from the DOM, otherwise the interval will keep going
// forever and leak memory
clearInterval(this._pollId);
}
_getUpdateEmails() {
return api.getEmails()
.then((emails) => {
// Because `emails` is a different reference from `this.state.emails`,
// the component will unnecessarily re-render even though the contents
// are the same. The virtual DOM will prevent the actual DOM from updating
// but it still actually has to run its diffing algorithm. So instead
// making this quick check here, saves unnecessary extra work
if (!isEqual(emails, this.state.emails)) {
this.setState({emails});
}
})
.catch((ex) => console.error(ex));
}
_setUnread(emailId, unread=true) {
api.setUnread(emailId, unread)
// optimistic updating (see _handleFormSubmit for more info)
.then(({success}) => {
if (success) {
let emails = this.state.emails.map((email) => (
email.id === emailId
? {...email, unread}
: email
));
this.setState({emails});
}
else {
throw new Error(`Unable to set email ID# ${emailId} unread state to ${unread}.`);
}
})
.catch((ex) => console.error(ex));
}
_handleItemSelect(emailId) {
// update state (so that the EmailView will show)
this.setState({selectedEmailId: emailId});
if (this.state.selectedEmailId !== emailId) {
// also mark the email as read
this._setUnread(emailId, false);
}
}
_handleEmailViewClose() {
// We close the email view by resetting the selected email
this.setState({selectedEmailId: -1});
}
_handleFormSubmit(newEmail) {
api.addEmail(newEmail)
.then(({success}) => {
if (success) {
// if the email was successfully updated, we have to make
// a request to get the new list of emails, but we'll have
// to wait for the response of that request, so let's add to
// our state immediately and then later when the response
// comes back, the server-side list will update. This is mainly
// here to demonstrate immutable updating of data structures
// Create a full email info by spreading in `id`, `date` & `unread`
// Then spread to front of emails state (since it's the newest)
let emails = [
{
...newEmail,
id: Date.now(),
date: `${new Date()}`,
unread: true
},
...this.state.emails
];
// Set state with new updated emails list
this.setState({emails, showForm: false});
}
else {
throw new Error('Unable to send email!');
}
})
.catch((ex) => console.error(ex));
}
_handleItemDelete(emailId) {
api.deleteEmail(emailId)
// optimistic updating (see _handleFormSubmit for more info)
.then(({success}) => {
if (success) {
let emails = this.state.emails.filter((email) => email.id !== emailId);
// Also reset `selectedEmailId` since we're deleting it
this.setState({emails, selectedEmailId: -1});
}
else {
throw new Error(`Unable to delete email ID# ${emailId}.`);
}
})
.catch((ex) => console.error(ex));
}
_handleItemMarkUnread(emailId) {
this._setUnread(emailId);
}
_handleItemMarkRead(emailId) {
this._setUnread(emailId, false);
}
_handleShowForm() {
// Show email form overlay by setting state to true
this.setState({showForm: true});
}
_handleHideForm() {
// Hide email form overlay by setting state to false
this.setState({showForm: false});
}
render() {
let {emails, selectedEmailId, showForm} = this.state;
let selectedEmail = find(emails, (email) => email.id === selectedEmailId);
return (
<main className="app">
<div className="app__page">
<section className="app__list">
<EmailList
emails={emails}
onItemSelect={this._handleItemSelect.bind(this)}
onItemDelete={this._handleItemDelete.bind(this)}
onItemMarkUnread={this._handleItemMarkUnread.bind(this)}
selectedEmailId={selectedEmailId}
/>
</section>
<EmailViewWrapper selectedEmail={selectedEmail}
onClose={this._handleEmailViewClose.bind(this)}
onDelete={this._handleItemDelete.bind(this, selectedEmailId)}
onMarkUnread={this._handleItemMarkUnread.bind(this, selectedEmailId)}
onMarkRead={this._handleItemMarkRead.bind(this, selectedEmailId)}
/>
<button className="app__new-email" onClick={this._handleShowForm.bind(this)}>+</button>
<EmailFormWrapper showForm={showForm}
onSubmit={this._handleFormSubmit.bind(this)}
onCancel={this._handleHideForm.bind(this)}
/>
</div>
</main>
);
}
}