This repository was archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathclient.js
110 lines (97 loc) · 2.96 KB
/
client.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
import io from 'socket.io-client';
import feathers from 'feathers/client';
import socketio from 'feathers-socketio/client';
import React from 'react';
import ReactDOM from 'react-dom';
import rx from 'feathers-reactive';
import RxJS from 'rxjs';
const socket = io();
const app = feathers()
.configure(socketio(socket))
.configure(rx(RxJS));
const submissionService = app.service('submissions');
class SubmissionList extends React.Component {
componentDidMount() {
submissionService.find({ query: {
$sort: {
votes: -1
}
}})
.subscribe(submissions => this.setState({ submissions }));
}
upvote(submission) {
submissionService.patch(submission.id, {
votes: submission.votes + 1
});
}
downvote(submission) {
submissionService.patch(submission.id, {
votes: submission.votes - 1
});
}
render() {
if(this.state && this.state.submissions) {
return <ul className="list-group">
{this.state.submissions.data.map(submission =>
<li className="submission list-group-item" key={submission.id}>
<h2>
<small className="text-center pull-left">
<span className="glyphicon glyphicon-chevron-up"
onClick={this.upvote.bind(this, submission)}>
</span>
<br />
<strong>{submission.votes}</strong><br/>
<span className="glyphicon glyphicon-chevron-down"
onClick={this.downvote.bind(this, submission)}>
</span>
</small>
<a href={submission.url}>{submission.title}</a>
</h2>
</li>
)}
</ul>;
}
return <div>Loading...</div>
}
}
class SubmissionForm extends React.Component {
constructor(props) {
super(props);
this.state = {
title: '',
link: ''
};
}
updateProperty(prop, ev) {
this.setState({ [prop]: ev.target.value });
}
submit(ev) {
ev.preventDefault();
submissionService.create({
title: this.state.title,
link: this.state.link
}).then(() => this.setState({
title: '',
link: ''
}));
}
render() {
return <form onSubmit={this.submit.bind(this)}>
<div className="form-group">
<label for="inputTitle">Title</label>
<input type="text" className="form-control" id="inputTitle" placeholder="Title" onChange={this.updateProperty.bind(this, 'title')} value={this.state.title} />
</div>
<div className="form-group">
<label for="inputTitle">Link</label>
<input type="text" className="form-control" id="inputLink" placeholder="Link" onChange={this.updateProperty.bind(this, 'link')} value={this.state.link} />
</div>
<div className="form-group">
<button className="btn btn-primary btn-block" type="submit">Submit</button>
</div>
</form>;
}
}
ReactDOM.render(<div>
<SubmissionList />
<SubmissionForm />
</div>, document.getElementById('app'));