import React from 'react'; import { Form, Button, Spinner } from 'react-bootstrap'; import ModalBox from './ModalBox.js'; import './App.css'; class LoginForm extends React.Component { constructor(props) { super(props); this.state = { user: '', password: '', loggedIn: props.loggedIn, loading: false, error: false }; } componentDidUpdate() { if (this.state.error) { console.log(this.state); this.timeout = setTimeout(this.resetComponentState, 3000); } } componentDidMount() { fetch(this.props.targetUrl + '/status', {credentials: 'include'}) .then(response => response.json()) .then(data => this.setState({ loggedIn: data.logged_in }) ); } componentWillUnmount() { clearTimeout(this.timeout) } handleUserChange = (event) => { this.setState({user: event.target.value}); }; handlePasswordChange = (event) => { this.setState({password: event.target.value}); }; handleSubmit = (event) => { event.preventDefault(); this.setState({loading: true}); const formData = event.target.elements; var data = {username: formData["username"].value, password: formData["password"].value}; fetch(this.props.targetUrl + '/login', { method: 'POST', body: JSON.stringify(data), credentials: 'include' }) .then(response => response.json()) .then(jsonData => this.setState({loggedIn: jsonData.logged_in, loading: false})) .catch((error) => {console.log(error); this.setState({error: true})}) } dynForm() { const form = Login Form data=
/>; const loading = />; if (this.state.loading) { return loading; } else { return form; } } resetComponentState = () => { this.setState({error: false, loading: false, username: '', password: ''}) } render() { if (this.state.loggedIn) { return (Yet logged in! />); } if (this.state.error) { return (

Error!

); } else { return this.dynForm(); } } } export default LoginForm;