2019-08-06 10:09:10 +02:00
|
|
|
import React from 'react';
|
2019-08-06 14:04:27 +02:00
|
|
|
import ModalBox from './ModalBox.js';
|
|
|
|
import { Button, Spinner } from 'react-bootstrap';
|
2019-08-06 10:09:10 +02:00
|
|
|
import './App.css';
|
|
|
|
|
|
|
|
|
|
|
|
function isCheckedIn(movements) {
|
|
|
|
if (movements.length === 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
const lastMov = movements[movements.length - 1].type;
|
|
|
|
if (lastMov === "Entrata") {
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
class BadgePage extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {loggedIn: props.loggedIn, checkedIn: false, loading: false};
|
|
|
|
}
|
|
|
|
|
|
|
|
_isMounted = false
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this._isMounted = true;
|
|
|
|
|
|
|
|
fetch(this.props.targetUrl + '/movements', {credentials: 'include'})
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => {if (this._isMounted) {this.setState({
|
|
|
|
checkedIn: isCheckedIn(data.movements), loggedIn: data.logged_in
|
|
|
|
})}})
|
|
|
|
.catch(error => console.log(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this._isMounted = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
handleToggle = (event) => {
|
|
|
|
this.setState({loading: true})
|
|
|
|
|
|
|
|
var path;
|
|
|
|
if (this.state.checkedIn) {
|
|
|
|
path = '/checkout';
|
|
|
|
} else {
|
|
|
|
path = '/checkin';
|
|
|
|
}
|
|
|
|
fetch(this.props.targetUrl + path, {
|
2019-08-07 10:31:36 +02:00
|
|
|
method: 'POST',
|
|
|
|
credentials: 'include'
|
2019-08-06 10:09:10 +02:00
|
|
|
})
|
|
|
|
.then(response => response.json())
|
|
|
|
.then(data => this.setState({
|
|
|
|
checkedIn: data.checked_in,
|
|
|
|
loggedIn: data.logged_in,
|
|
|
|
loading: false
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
buttonValue() {
|
|
|
|
if (this.state.checkedIn) {
|
|
|
|
return "CHECKOUT";
|
|
|
|
} else {
|
|
|
|
return "CHECKIN";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dynButton() {
|
|
|
|
const badge = (
|
2019-08-06 14:04:27 +02:00
|
|
|
<ModalBox
|
|
|
|
title="Badge"
|
|
|
|
data={<Button variant="primary" type="button" onClick={this.handleToggle}>{this.buttonValue()}</Button>}
|
|
|
|
/>
|
2019-08-06 10:09:10 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const loading =
|
2019-08-06 14:04:27 +02:00
|
|
|
<ModalBox
|
|
|
|
title="Badge"
|
|
|
|
data=<Spinner animation="border" variant="primary" />
|
|
|
|
/>;
|
2019-08-06 10:09:10 +02:00
|
|
|
|
|
|
|
if (this.state.loading) {
|
|
|
|
return loading;
|
|
|
|
} else {
|
|
|
|
return badge;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
if (this.state.loggedIn) {
|
|
|
|
return this.dynButton();
|
|
|
|
} else {
|
2019-08-06 14:04:27 +02:00
|
|
|
return (<ModalBox title="Badge" data=<h2>You have to login first!</h2> />);
|
2019-08-06 10:09:10 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default BadgePage;
|