Newer
Older
import React from "react";
import { Spinner, Alert, Row } from "react-bootstrap";
import AuthContext from "./AuthContext";
constructor(props) {
super(props);
this.state = {
if (!this.props.oneShot) {
this.timerID = setInterval(
() => this.fetch(),
10000 // every 10 seconds
);
}
compomentWillUnmount() {
clearInterval(this.timerID);
}
fetch() {
if (this.state.error) {
this.setState({ error: null });
headers: { "x-authentication": this.context.token },
})
.then((response) => {
if (!response.ok) {
throw new Error(
response.status.toString() + " " + response.statusText
);
}
return response.json();
})
.then((data) => {
this.props.onFetch(data);
})
.catch((e) => this.setState({ error: e.message, isLoading: false }));
}
render() {
if (this.state.isLoading) {
return (
<Row className="justify-content-md-center">
<Spinner animation="border" />
</Row>
);
}
if (this.state.error != null) {
console.log(this.state.error);
return (
<Alert variant="danger">
Ha ocurrido un error cargando datos: {this.state.error}
</Alert>
);