aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLibravatar Renard 2019-09-16 13:32:35 -0300
committerLibravatar Renard 2019-09-16 13:32:35 -0300
commitc7fcf05857716e187c064d7ba21f27962015b71d (patch)
tree35c6d3c5ee7aa0deb1ce7ae87a62cd2bbf7333ce /src
parent4a3e174029aedfedca652007c4e386ddccef5b3f (diff)
downloadbai-client-c7fcf05857716e187c064d7ba21f27962015b71d.tar.gz
bai-client-c7fcf05857716e187c064d7ba21f27962015b71d.tar.xz
bai-client-c7fcf05857716e187c064d7ba21f27962015b71d.zip
Autorefresh para Thread
Diffstat (limited to 'src')
-rw-r--r--src/Thread.js59
1 files changed, 56 insertions, 3 deletions
diff --git a/src/Thread.js b/src/Thread.js
index 022e360..f23c081 100644
--- a/src/Thread.js
+++ b/src/Thread.js
@@ -19,8 +19,12 @@ class Thread extends Component {
this.state = {
isLoading: true,
thread: [],
- error: null
+ error: null,
+ autoRefresh: null
};
+ this.lastTime = 0;
+ this.refreshCooldown = 15;
+ this.cooldownCounter = 0;
}
componentDidMount() {
@@ -39,10 +43,59 @@ class Thread extends Component {
if (resource["state"] === "error") {
this.setState({ error: resource });
}
- this.setState({ isLoading: false, thread: resource });
+ this.setState({
+ isLoading: false,
+ thread: resource,
+ autoRefresh: setInterval(() => this.updateReplies(), 1000)
+ });
+ this.lastTime = resource.time;
});
- /* .catch(console.error); */
}
+
+ componentWillUnmount() {
+ clearInterval(this.state.autoRefresh);
+ }
+
+ async updateReplies() {
+ this.cooldownCounter++;
+ if (this.cooldownCounter < this.refreshCooldown) {
+ return;
+ } else {
+ this.cooldownCounter = 0;
+ }
+
+ let apiURl = `https://bienvenidoainternet.org/cgi/api/thread?dir=${
+ this.props.dir
+ }&${this.props.id > 1000000 ? "ts" : "id"}=${this.props.id}&offset=${
+ this.state.thread.posts.length
+ }&time=${this.lastTime}`;
+
+ fetch(apiURl)
+ .then(response => {
+ return response.json();
+ })
+ .then(resource => {
+ if (resource.state === "success") {
+ if (resource.posts.length > 0) {
+ const newPosts = this.state.thread.posts.concat(resource.posts);
+ this.setState(({ thread }) => ({
+ thread: {
+ ...thread,
+ posts: newPosts
+ }
+ }));
+ this.refreshCooldown = 15;
+ } else {
+ this.refreshCooldown += 15;
+ if (this.refreshCooldown > 60) {
+ this.refreshCooldown = 60;
+ }
+ }
+ }
+ this.lastTime = resource.time;
+ });
+ }
+
render() {
const { isLoading, error } = this.state;