-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.jsx
71 lines (64 loc) · 1.76 KB
/
index.jsx
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
import { h, Component } from 'preact';
import { connect } from 'preact-redux';
import { createStructuredSelector } from 'reselect';
import { connectivity, user } from '../../redux/modules';
import { callNames } from '../../api';
import { asJS } from '../../helper/immutableHelper';
import NotesForm from './NotesForm';
import Note from './Note';
import pureComponent from '../../helper/componentHelper';
@pureComponent
class Notes extends Component {
constructor(props) {
super(props);
this.state = {
noteText: ''
};
this.onInputChange = this.onInputChange.bind(this);
this.onSubmitNote = this.onSubmitNote.bind(this);
}
onInputChange({ target: { value } }) {
this.setState({
noteText: value
});
}
onSubmitNote({ requestApiCallAction, userName, notes }) {
return () => {
const message = this.state.noteText.trim();
if (message.length) {
requestApiCallAction(
callNames.STORE_NOTE,
{
userName,
message,
noteId: notes.size
},
user.actions.STORE_USER_NOTE
);
this.setState({
noteText: ''
});
}
};
}
render(props) {
return (
<div>
<NotesForm
value={this.state.noteText}
onInput={this.onInputChange}
onSubmitNote={this.onSubmitNote(props)}
/>
{asJS(props.notes.map((note, i) => <Note note={note} key={i} />))}
</div>
);
}
}
const mapStateToProps = createStructuredSelector({
notes: user.selectors.getOrderedUserNotes,
userName: user.selectors.getUsername
});
const mapDispatchToProps = {
requestApiCallAction: connectivity.actions.requestApiCall,
};
export default connect(mapStateToProps, mapDispatchToProps)(Notes);