Skip to content
Snippets Groups Projects
UserItem.js 5.58 KiB
Newer Older
Orlando Piñero's avatar
sth
Orlando Piñero committed
import React, { Component } from "react";
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 Card from 'react-bootstrap/Card';
import ListGroup from 'react-bootstrap/ListGroup';
import { connect } from "react-redux";
Orlando Piñero's avatar
Orlando Piñero committed
import { bindActionCreators } from "redux";
import DeleteUserModal from './DeleteUserModal'
Orlando Piñero's avatar
Orlando Piñero committed
import EditUser from './EditUser'
Orlando Piñero's avatar
Orlando Piñero committed
import CreateNewUser from './CreateNewUser'
import { LinkContainer } from "react-router-bootstrap";
import Nav from 'react-bootstrap/Nav';
Orlando Piñero's avatar
Orlando Piñero committed

Orlando Piñero's avatar
Orlando Piñero committed

import * as userManagementActions from '../actions/UserManagementActions'
Orlando Piñero's avatar
sth
Orlando Piñero committed

const mapStateToProps = state => {
    return state;
}

Orlando Piñero's avatar
Orlando Piñero committed
const DISPLAY = {
    LIST: "list",
Orlando Piñero's avatar
Orlando Piñero committed
    EDIT: "edit",
    CREATE: "create"
Orlando Piñero's avatar
Orlando Piñero committed
}
Orlando Piñero's avatar
sth
Orlando Piñero committed

Orlando Piñero's avatar
Orlando Piñero committed
class UserItem extends Component {
Orlando Piñero's avatar
sth
Orlando Piñero committed
    constructor(props) {
        super(props);
Orlando Piñero's avatar
Orlando Piñero committed
        this.state = {
admin's avatar
admin committed
            displayingPage: DISPLAY.LIST,
            editingUser: null
Orlando Piñero's avatar
Orlando Piñero committed
        }
Orlando Piñero's avatar
sth
Orlando Piñero committed

        this.MapAllUsers = this.MapAllUsers.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
        this.handleShowDeleteModal = this.handleShowDeleteModal.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
        this.handleClose = this.handleClose.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
        this.handleSwitchToList = this.handleSwitchToList.bind(this);
        this.handleShowCreatePage = this.handleShowCreatePage.bind(this);
Orlando Piñero's avatar
Orlando Piñero committed
    handleShowDeleteModal(e, dialogModalData) {
Orlando Piñero's avatar
Orlando Piñero committed
        e.preventDefault();
        const { showDeleteUserModalAction } = this.props
        showDeleteUserModalAction(dialogModalData)
    }

admin's avatar
admin committed
    handleShowEditPage(e, user) {
Orlando Piñero's avatar
Orlando Piñero committed
        e.preventDefault();
Orlando Piñero's avatar
Orlando Piñero committed
        this.setState({ editingUser: user })
Orlando Piñero's avatar
Orlando Piñero committed
        this.setState({ displayingPage: DISPLAY.EDIT })
Orlando Piñero's avatar
Orlando Piñero committed
    }

Orlando Piñero's avatar
Orlando Piñero committed
    handleShowCreatePage(e) {
        e.preventDefault();
        this.setState({ displayingPage: DISPLAY.CREATE })
    }

Orlando Piñero's avatar
Orlando Piñero committed
    handleClose() {
        const { hideDeleteUserModalAction } = this.props;
        hideDeleteUserModalAction();
Orlando Piñero's avatar
sth
Orlando Piñero committed
    }

Orlando Piñero's avatar
Orlando Piñero committed
    handleSwitchToList(e) {
        this.setState({ displayingPage: DISPLAY.LIST })
    }

Orlando Piñero's avatar
sth
Orlando Piñero committed
    MapAllUsers() {
        const allUsers = this.props.userList
        if (allUsers) {
Orlando Piñero's avatar
Orlando Piñero committed
            var showDeleteUserModal = this.props.showDeleteUserModal
            if (showDeleteUserModal === undefined) {
                showDeleteUserModal = false
            }
            console.log("deleteModal:" + showDeleteUserModal)
Orlando Piñero's avatar
sth
Orlando Piñero committed
            var users = allUsers.map(user => {
                let editButtonID = "EditButton" + user.userID
                let deleteButtonID = "DeleteButton" + user.userID
                let itemID = "UserItem" + user.userID
Orlando Piñero's avatar
Orlando Piñero committed
                let dialogData = {
                    dialogID: "DeleteDialogUser" + user.userID,
                    name: user.firstName + " " + user.lastName,
                    userID: user.userID
                }

Orlando Piñero's avatar
Orlando Piñero committed
                return (
                    <div key={user.userID}>
                        <DeleteUserModal />
                        <Card style={{ width: "18rem", background: '#ebebeb' }} id={itemID}>
                            <Card.Header>{user.firstName} {user.lastName}</Card.Header>
                            <ListGroup variant="flush">
                                <ListGroup.Item >User ID: {user.userID}</ListGroup.Item>
                                <ListGroup.Item >First Name: {user.firstName}</ListGroup.Item>
                                <ListGroup.Item >Last Name: {user.lastName}</ListGroup.Item>
                            </ListGroup>
                            <Card.Footer style={{ display: 'flex', justifyContent: 'space-evenly' }}>
                                <Button variant="primary" onClick={(e) => this.handleShowEditPage(e, user)} id={editButtonID} >Edit</Button>
                                <Button variant="primary" type="submit" onClick={(e) => this.handleShowDeleteModal(e, dialogData)} id={deleteButtonID} style={{ background: '#ffc800', color: 'black', border: 'none' }}>
                                    Delete
                                </Button>
                            </Card.Footer>
                        </Card>
                    </div>
                )
Orlando Piñero's avatar
sth
Orlando Piñero committed
            })
Orlando Piñero's avatar
Orlando Piñero committed
            console.log(users)
Orlando Piñero's avatar
sth
Orlando Piñero committed
            return users
        }
    }

    render() {
admin's avatar
admin committed
        var workspace
Orlando Piñero's avatar
Orlando Piñero committed
        
        console.log(this.MapAllUsers())
admin's avatar
admin committed
        switch (this.state.displayingPage) {
Orlando Piñero's avatar
Orlando Piñero committed
            case DISPLAY.LIST:
admin's avatar
admin committed
                workspace = <Button onClick={this.handleShowCreatePage}>
Orlando Piñero's avatar
Orlando Piñero committed
                    <img alt="Add-User-Button" src="https://cdn.icon-icons.com/icons2/933/PNG/512/round-add-button_icon-icons.com_72595.png" style={{ width: '2rem' }}></img>
                </Button>
Orlando Piñero's avatar
Orlando Piñero committed
                {this.MapAllUsers()}
Orlando Piñero's avatar
Orlando Piñero committed
                break
            case DISPLAY.CREATE:
                workspace = <CreateNewUser goBack={this.handleSwitchToList} />
                break
            case DISPLAY.EDIT:
                workspace = <EditUser goBack={this.handleSwitchToList} editUser={this.state.editingUser} />
                break
            default:
                workspace = <Button onClick={this.handleShowCreatePage}>
                    <img alt="Add-User-Button" src="https://cdn.icon-icons.com/icons2/933/PNG/512/round-add-button_icon-icons.com_72595.png" style={{ width: '2rem' }}></img>
                </Button>
Orlando Piñero's avatar
Orlando Piñero committed
                {this.MapAllUsers()}
admin's avatar
admin committed
        }
        return (
            <div >
                {workspace}
Orlando Piñero's avatar
sth
Orlando Piñero committed
            </div>
        )
    }
}

Orlando Piñero's avatar
Orlando Piñero committed
const mapDispatchToProps = dispatch => bindActionCreators({
    showDeleteUserModalAction: userManagementActions.getShowDeleteUserModalAction,
Orlando Piñero's avatar
Orlando Piñero committed
    hideDeleteUserModalAction: userManagementActions.getHideDeleteUserModalAction
Orlando Piñero's avatar
Orlando Piñero committed
}, dispatch)

const ConnectedUserItems = connect(mapStateToProps, mapDispatchToProps)(UserItem)
Orlando Piñero's avatar
sth
Orlando Piñero committed
export default ConnectedUserItems