Skip to content
Snippets Groups Projects
userSessionWidget.js 4.2 KiB
Newer Older
Orlando Piñero's avatar
Orlando Piñero committed
import Button from "react-bootstrap/esm/Button";
import Modal from "react-bootstrap/Modal";
Orlando Piñero's avatar
Orlando Piñero committed
import Form from 'react-bootstrap/Form';

import React, { Component } from "react";
import { connect } from "react-redux";
import { bindActionCreators } from "redux";

import * as authenticationActions from '../../actions/AuthenticationActions'
Orlando Piñero's avatar
Orlando Piñero committed

const mapStateToProps = state => {
    return {
        showLoginDialog: state.AuthenticationReducer.showLoginDialog,
        user: state.AuthenticationReducer.user
    };
Orlando Piñero's avatar
Orlando Piñero committed

Orlando Piñero's avatar
Orlando Piñero committed
class userSessionWidget extends Component {

    constructor(props) {
        super(props)
Orlando Piñero's avatar
Orlando Piñero committed
        this.state = {
            username: '',
            password: ''
        };
Orlando Piñero's avatar
Orlando Piñero committed
        this.handleShow = this.handleShow.bind(this);
        this.handleClose = this.handleClose.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
        this.handleChange = this.handleChange.bind(this);
        this.handleSubmit = this.handleSubmit.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
        this.handleLogout = this.handleLogout.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
    }

    handleShow(e) {
        e.preventDefault();
Orlando Piñero's avatar
Orlando Piñero committed
        const { showLoginDialogAction } = this.props;
        showLoginDialogAction();
    }

    handleClose() {
        const { hideLoginDialogAction } = this.props;
        hideLoginDialogAction();
    }

    handleChange(e) {
        const { name, value } = e.target;
        this.setState({ [name]: value })
        console.log(JSON.stringify(this.state))
Orlando Piñero's avatar
Orlando Piñero committed
    }

Orlando Piñero's avatar
Orlando Piñero committed
    handleSubmit(e) {
Orlando Piñero's avatar
Orlando Piñero committed
        e.preventDefault();
Orlando Piñero's avatar
Orlando Piñero committed
        const { username, password } = this.state;
        const { authenticateUserAction } = this.props;
        authenticateUserAction(username, password);
        console.log(username)
        console.log(password)
        console.log("Pushed Submit")
Orlando Piñero's avatar
Orlando Piñero committed
    }

Orlando Piñero's avatar
Orlando Piñero committed
    handleLogout(e) {
        e.preventDefault()
        const { logout } = this.props;
        logout()
    }

Orlando Piñero's avatar
Orlando Piñero committed
    render() {
Orlando Piñero's avatar
Orlando Piñero committed
        const user = this.props.user
        let button;
        if (!user) {
            button = <Button variant="primary" onClick={this.handleShow} id="OpenLoginDialogButton">
                Login
            </Button>
        } else {
            button = <Button variant="primary" onClick={this.handleLogout} id="LogoutButton">
                Logout
            </Button>
        }
Orlando Piñero's avatar
Orlando Piñero committed

        var showDialog = this.props.showLoginDialog
        if (showDialog === undefined) {
            showDialog = false
        }
Orlando Piñero's avatar
Orlando Piñero committed
        return (
            <div>
Orlando Piñero's avatar
Orlando Piñero committed
                {button}
Orlando Piñero's avatar
Orlando Piñero committed

Orlando Piñero's avatar
Orlando Piñero committed
                <Modal show={showDialog} onHide={this.handleClose} id="LoginDialog">
Orlando Piñero's avatar
Orlando Piñero committed
                    <Modal.Header closeButton>
Orlando Piñero's avatar
Orlando Piñero committed
                        <Modal.Title>Login</Modal.Title>
Orlando Piñero's avatar
Orlando Piñero committed
                    </Modal.Header>
                    <Modal.Body>
Orlando Piñero's avatar
Orlando Piñero committed
                        <Form>
                            <Form.Group className="mb-3">
Orlando Piñero's avatar
Orlando Piñero committed
                                <Form.Label>Username</Form.Label>
Orlando Piñero's avatar
Orlando Piñero committed
                                <Form.Control type="text" placeholder="Bitte User-ID eingeben" name='username' onChange={this.handleChange} id="LoginDialogUserIDText" />
Orlando Piñero's avatar
Orlando Piñero committed
                            </Form.Group>

                            <Form.Group className="mb-3">
                                <Form.Label>Password</Form.Label>
Orlando Piñero's avatar
Orlando Piñero committed
                                <Form.Control type="password" placeholder="Password" name='password' onChange={this.handleChange} id="LoginDialogPasswordText" />
Orlando Piñero's avatar
Orlando Piñero committed
                            </Form.Group>
                            <Button variant="primary" type="submit" onClick={this.handleSubmit} id="PerformLoginButton">
                                Submit
                            </Button>
                        </Form>
Orlando Piñero's avatar
Orlando Piñero committed
                    </Modal.Body>

                    <Modal.Footer>
Orlando Piñero's avatar
Orlando Piñero committed
                        Passwort vergessen?
Orlando Piñero's avatar
Orlando Piñero committed
                    </Modal.Footer>
                </Modal>
            </div>
        )
    }
}

Orlando Piñero's avatar
Orlando Piñero committed
const mapDispatchToProps = dispatch => bindActionCreators({
    showLoginDialogAction: authenticationActions.getShowLoginDialogAction,
    hideLoginDialogAction: authenticationActions.getHideLoginDialogAction,
Orlando Piñero's avatar
Orlando Piñero committed
    authenticateUserAction: authenticationActions.authenticateUser,
    logout: authenticationActions.getLogoutAction
Orlando Piñero's avatar
Orlando Piñero committed
}, dispatch)

const ConnectedUserSessionWidget = connect(mapStateToProps, mapDispatchToProps)(userSessionWidget)
export default ConnectedUserSessionWidget