Skip to content
Snippets Groups Projects
CreateNewUser.js 3.86 KiB
Newer Older
Orlando Piñero's avatar
sth
Orlando Piñero committed
import React, { Component } from "react";
import Form from 'react-bootstrap/Form';
Orlando Piñero's avatar
Orlando Piñero committed
import Button from "react-bootstrap/esm/Button";
Orlando Piñero's avatar
sth
Orlando Piñero committed

import { connect } from "react-redux";
import { bindActionCreators } from "redux";
Orlando Piñero's avatar
Orlando Piñero committed
import * as userManagementActions from "../state/UserManagementActions";
Orlando Piñero's avatar
sth
Orlando Piñero committed

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

class CreateNewUserForm extends Component {

    constructor(props) {
        super(props);
        this.state = {
            userID: '',
            firstName: '',
            lastName: '',
            password: '',
            isAdministrator: false
        }
        this.handleSubmit = this.handleSubmit.bind(this);
        this.handleChange = this.handleChange.bind(this);
    }

    handleChange(e) {
        const { name, value } = e.target;
Orlando Piñero's avatar
Orlando Piñero committed
        if (name === 'isAdministrator') {
            if (e.target.checked) {
                this.setState({ [name]: true })
Orlando Piñero's avatar
sth
Orlando Piñero committed
            } else {
Orlando Piñero's avatar
Orlando Piñero committed
                this.setState({ [name]: false })
Orlando Piñero's avatar
sth
Orlando Piñero committed
            }
Orlando Piñero's avatar
Orlando Piñero committed
        } else {
Orlando Piñero's avatar
sth
Orlando Piñero committed
            this.setState({ [name]: value })
        }
    }

Orlando Piñero's avatar
Orlando Piñero committed
    async handleSubmit(e) {
Orlando Piñero's avatar
sth
Orlando Piñero committed
        e.preventDefault();
        const { createNewUserAction } = this.props;
Orlando Piñero's avatar
Orlando Piñero committed
        this.props.goBack()
        await createNewUserAction(this.state, this.props.accessToken)
Orlando Piñero's avatar
sth
Orlando Piñero committed
    }

    render() {
        return (
Orlando Piñero's avatar
Orlando Piñero committed
            <div style={{ display: 'flex', justifyContent: 'center', textAlign: 'left', padding: '2em', background: '#fff', borderRadius: '30px', margin: '3rem' }}>
Orlando Piñero's avatar
sth
Orlando Piñero committed
                <Form style={{ width: '60rem' }}>
Orlando Piñero's avatar
Orlando Piñero committed
                    <h2>Benutzer anlegen</h2>
Orlando Piñero's avatar
sth
Orlando Piñero committed
                    <Form.Group className="mb-3">
                        <Form.Label>User-ID</Form.Label>
                        <Form.Control id="CreateUserComponentEditUserID" name='userID' type="text" placeholder="Bitte User-ID eingeben" onChange={this.handleChange} required />
                        <Form.Text className="text-muted">
                            Die User-ID kann im Nachhinein nicht mehr geändert werden.
                        </Form.Text>
                    </Form.Group>

                    <Form.Group className="mb-3">
                        <Form.Label>Vorname</Form.Label>
                        <Form.Control id="CreateUserComponentEditFirstName" name="firstName" type="text" placeholder="Bitte Vornamen eingeben" onChange={this.handleChange} />
                    </Form.Group>

                    <Form.Group className="mb-3">
                        <Form.Label>Nachname</Form.Label>
                        <Form.Control id="CreateUserComponentEditLastName" name="lastName" type="text" placeholder="Bitte Nachnamen eingeben" onChange={this.handleChange} />
                    </Form.Group>

                    <Form.Group className="mb-3">
                        <Form.Label>Passwort</Form.Label>
                        <Form.Control id="CreateUserComponentEditPassword" name="password" type="password" placeholder="Bitte Passwort eingeben" onChange={this.handleChange} required />
                    </Form.Group>

                    <Form.Group className="mb-3">
                        <Form.Check id="CreateUserComponentEditIsAdministrator" name="isAdministrator" type="checkbox" label="Administrator-Rechte" onChange={this.handleChange} />
                    </Form.Group>
Orlando Piñero's avatar
Orlando Piñero committed
                    <Button id="CreateUserComponentCreateUserButton" variant="primary" type="submit" onClick={this.handleSubmit}>
                        Anlegen
                    </Button>
Orlando Piñero's avatar
sth
Orlando Piñero committed
                </Form>
            </div >
        )
    }
}

const mapDispatchToProps = dispatch => bindActionCreators({
Orlando Piñero's avatar
Orlando Piñero committed
    createNewUserAction: userManagementActions.passNewUser,
    showAllUsers: userManagementActions.displayUsers
Orlando Piñero's avatar
sth
Orlando Piñero committed
}, dispatch)

const ConnectedUserForm = connect(mapStateToProps, mapDispatchToProps)(CreateNewUserForm)
export default ConnectedUserForm