Skip to content
Snippets Groups Projects
UserItem.js 4.3 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 { LinkContainer } from "react-router-bootstrap";
import Nav from 'react-bootstrap/Nav';
// import Button from "react-bootstrap/esm/Button";
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'

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

const mapStateToProps = state => {
    return state;
}

class UserItem extends Component {

    constructor(props) {
        super(props);

        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
    handleShowDeleteModal(e, dialogModalData) {
Orlando Piñero's avatar
Orlando Piñero committed
        e.preventDefault();
        const { showDeleteUserModalAction } = this.props
        showDeleteUserModalAction(dialogModalData)
    }

Orlando Piñero's avatar
Orlando Piñero committed
    handleShowEditPage(e, userID) {
        e.preventDefault();
        const { passEditUserData } = this.props
        passEditUserData(userID)
        this.props.history.push('/editUserForm')
    }

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
    }

    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
                let currentUserID = user.userID

Orlando Piñero's avatar
sth
Orlando Piñero committed
                return (
                    <div key={user.userID}>
Orlando Piñero's avatar
Orlando Piñero committed
                        <DeleteUserModal />

Orlando Piñero's avatar
sth
Orlando Piñero committed
                        <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' }}>
Orlando Piñero's avatar
Orlando Piñero committed
                                <LinkContainer to="/editUserForm" id={editButtonID} onClick={(e) => this.handleShowEditPage(e, currentUserID)}>
Orlando Piñero's avatar
sth
Orlando Piñero committed
                                    <Nav.Link>Edit</Nav.Link>
                                </LinkContainer>
Orlando Piñero's avatar
Orlando Piñero committed
                                {/* <Button variant="primary" onClick={(e) => this.handleShowEditPage(e, currentUserID)}>Edit</Button> */}

                                <Button variant="primary" type="submit" onClick={(e) => this.handleShowDeleteModal(e, dialogData)} id={deleteButtonID} style={{ background: '#ffc800', color: 'black', border: 'none' }}>
Orlando Piñero's avatar
sth
Orlando Piñero committed
                                    Delete
Orlando Piñero's avatar
Orlando Piñero committed
                                </Button>
Orlando Piñero's avatar
sth
Orlando Piñero committed
                            </Card.Footer>
                        </Card>
                    </div>
                )
            })
            return users
        }
    }

    render() {
        return (
            <div>
                {this.MapAllUsers()}
            </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,
    passEditUserData: userManagementActions.editingUserWithID
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