BotZ/bot.z_web/src/Logoff.js

74 lines
1.8 KiB
JavaScript
Raw Normal View History

2019-08-06 10:09:10 +02:00
import React from 'react';
2019-08-06 14:04:27 +02:00
import { Spinner, Button } from 'react-bootstrap';
import ModalBox from './ModalBox.js';
2019-08-06 10:09:10 +02:00
import './App.css';
import './bootstrap.min.css';
class LogoffPage extends React.Component {
constructor(props) {
super(props);
this.state = {logged_in: props.loggedIn, loading: false};
}
componentDidMount() {
fetch(this.props.targetUrl + '/status', {credentials: 'include'})
.then(response => response.json())
.then(data => this.setState({
loggedIn: data.logged_in
}));
}
handleToggle = (event) => {
this.setState({loading: true})
fetch(this.props.targetUrl + '/logout', {
method: 'POST',
credentials: 'include'
})
.then(response => response.json())
.then(data => this.setState({
loggedIn: data.logged_in, loading: false
}));
}
buttonValue() {
if (this.state.loggedIn) {
return "LOGOUT";
} else {
return "LOGIN";
}
}
dynButton() {
const logout = (
2019-08-06 14:04:27 +02:00
<ModalBox
title="Logoff"
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="Logout"
data=<Spinner animation="border" variant="primary" />
/>;
2019-08-06 10:09:10 +02:00
if (this.state.loading) {
return loading;
} else {
return logout;
}
}
render() {
if (this.state.loggedIn) {
return this.dynButton();
} else {
2019-08-06 14:04:27 +02:00
return (<ModalBox title="Logout" data=<h2>You have to login first!</h2> />);
2019-08-06 10:09:10 +02:00
}
}
}
export default LogoffPage;