-
Notifications
You must be signed in to change notification settings - Fork 8
/
NoteForm.js
256 lines (243 loc) · 6.53 KB
/
NoteForm.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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
import {h, Component} from 'preact';
import QuickActions from './QuickActions';
import Message from '../Message';
import ChangeViewTabs from './ChangeViewTabs';
import FormInputs from './FormInputs';
import Footer from '../Footer';
import {getDraft, deleteDraft} from '../../util/draft';
import {clone} from '../../util/utils';
import micropub from '../../util/micropub';
import {
NEW_NOTE,
PAGE_REPLY,
ITEM_REPLY,
MESSAGE_SUCCESS,
MESSAGE_ERROR,
} from '../../constants';
import {getSettings} from '../../util/settings';
export default class NoteForm extends Component {
constructor(props) {
super(props);
const selectedEntry = localStorage.getItem('selectedEntry');
const syndicateOptions = JSON.parse(localStorage.getItem('syndicateTo'));
const settings = getSettings();
const draft = getDraft();
this.state = {
postType: this.getPostType(settings),
url: this.getEntryUrl(),
userDomain: localStorage.getItem('domain'),
entry: draft,
hasSelectedEntry: !!selectedEntry,
isDisabled: false,
isLoading: false,
settings: settings,
syndicateOptions,
};
}
getPostType(settings) {
const selectedEntry = localStorage.getItem('selectedEntry');
if (
location.search.indexOf('reply=true') === -1 &&
!settings.defaultToCurrentPage
) {
return NEW_NOTE;
}
if (selectedEntry) {
return ITEM_REPLY;
} else {
return PAGE_REPLY;
}
}
getEntryUrl() {
const selectedEntry = localStorage.getItem('selectedEntry');
if (selectedEntry) {
return selectedEntry;
} else {
return localStorage.getItem('pageUrl');
}
}
render() {
const {
postType,
url,
isDisabled,
isLoading,
settings,
userDomain,
entry,
syndicateOptions,
hasSelectedEntry,
errorMessage,
} = this.state;
const {handleSettings, handleLogout} = this.props;
return (
<div>
<ChangeViewTabs
postType={postType}
onChange={this.changeView}
hasSelectedEntry={hasSelectedEntry}
/>
<QuickActions
postType={postType}
url={url}
onLike={this.handleLike}
onRepost={this.handleRepost}
onReacji={this.handleReacji}
isDisabled={isLoading}
settings={settings}
/>
<div className="container">
<FormInputs
postType={postType}
entry={entry}
settings={settings}
syndicateOptions={syndicateOptions}
updateEntry={this.updateEntry}
onSubmit={this.handleSubmit}
isDisabled={isDisabled}
isLoading={isLoading}
ref={el => (this.form = el)}
/>
{errorMessage ? (
<Message type={MESSAGE_ERROR}>{errorMessage}</Message>
) : null}
</div>
<Footer
domain={userDomain}
onSettings={handleSettings}
onLogout={handleLogout}
/>
</div>
);
}
handleLike = () => {
if (!this.state.url) {
return;
}
this.postEntry({
h: 'entry',
'like-of': this.state.url,
})
.then(location => {
const type = this.state.postType === ITEM_REPLY ? 'Item' : 'Page';
this.flashSuccessMessage(`${type} liked successfully`, location);
})
.catch(err => {
console.error(err);
this.flashErrorMessage('Error posting like');
});
};
handleRepost = () => {
if (!this.state.url) {
return;
}
this.postEntry({
h: 'entry',
'repost-of': this.state.url,
})
.then(location => {
const type = this.state.postType === ITEM_REPLY ? 'Item' : 'Page';
this.flashSuccessMessage(`${type} reposted successfully`, location);
})
.catch(err => {
console.error(err);
this.flashErrorMessage('Error reposting');
});
};
handleReacji = emoji => {
if (!this.state.url) {
return;
}
this.postEntry({
h: 'entry',
content: emoji,
'in-reply-to': this.state.url,
})
.then(location => {
const type = this.state.postType === ITEM_REPLY ? 'Item' : 'Page';
this.flashSuccessMessage(`${type} reacted to successfully`, location);
})
.catch(err => {
console.error(err);
this.flashErrorMessage('Error reacting');
});
};
updateEntry = newEntry => {
this.setState({entry: newEntry});
};
flashSuccessMessage(message, location) {
this.props.userFeedback(message, MESSAGE_SUCCESS, location);
if (this.state.settings.closeAfterPosting) {
setTimeout(() => {
window.close();
}, 3000);
}
}
flashErrorMessage(message) {
this.setState({
errorMessage: message,
isDisabled: false,
isLoading: false,
});
setTimeout(() => {
if (this.state.errorMessage === message) {
this.setState({errorMessage: false});
}
}, 4000);
}
handleSubmit = entry => {
if (this.state.postType !== NEW_NOTE) {
entry['in-reply-to'] = this.state.url;
}
this.postEntry(entry)
.then(location => {
const type = this.state.postType === NEW_NOTE ? 'Note' : 'Reply';
deleteDraft();
this.flashSuccessMessage(`${type} posted successfully`, location);
})
.catch(err => {
console.error(err);
if (err.status >= 400 && err.status < 500) {
this.flashErrorMessage(
'Error authenticating to micropub endpoint. Try logging out and back in.'
);
} else {
this.flashErrorMessage('Error posting Note');
}
});
};
postEntry(entry) {
this.setState({
isDisabled: true,
isLoading: true,
});
const aliasedEntry = clone(entry);
const slugName = this.state.settings.slug;
const syndicateName = this.state.settings.syndicateTo;
if (slugName) {
aliasedEntry[slugName] = aliasedEntry['mp-slug'];
delete aliasedEntry['mp-slug'];
}
if (syndicateName) {
aliasedEntry[syndicateName] = aliasedEntry['mp-syndicate-to'];
delete aliasedEntry['mp-syndicate-to'];
}
return micropub.create(aliasedEntry, 'form');
}
changeView = postType => {
let url;
switch (postType) {
case NEW_NOTE:
url = null;
break;
case PAGE_REPLY:
url = localStorage.getItem('pageUrl');
break;
case ITEM_REPLY:
url = localStorage.getItem('selectedEntry');
break;
}
this.setState({url, postType});
this.form.focus();
};
}