File size: 2,243 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import React from 'react';
import { connect } from 'react-redux';
import { showModal } from '../../actions/ui_actions';

const mapStateToProps = state => ({
  currentUserId: state.session.currentUserId
})

const mapDispatchToProps = dispatch => ({
  showModal: (modalType, modalProps) => dispatch(showModal(modalType, modalProps))
})

class PostEditButton extends React.Component {
  openDropdown(e) {
    document
      .getElementById(`post-dropdown-${this.props.post.id}`)
      .classList.add("edit-dropdown-visible");
    document
      .getElementById(`post-background-${this.props.post.id}`)
      .classList.add("edit-dropdown-visible");
  }

  closeDropdown(e) {
    document
      .getElementById(`post-dropdown-${this.props.post.id}`)
      .classList.remove("edit-dropdown-visible");
    document
      .getElementById(`post-background-${this.props.post.id}`)
      .classList.remove("edit-dropdown-visible");
  }
  
  render() {
    const { post, currentUserId } = this.props;

    if (post.authorId === currentUserId) {
      return (
        <div className="edit-dropdown">
          <div className="edit-button" onClick={e => this.openDropdown(e)}>
            <i className="fas fa-ellipsis-h" />
          </div>
          <div id={`post-background-${post.id}`} className="dd-background" onClick={e => this.closeDropdown(e)}>
          </div>
          <div id={`post-dropdown-${post.id}`} className="dropdown-box">
            <button onClick={() => {
              this.closeDropdown();
              this.props.showModal('edit', post);
            }}>Edit</button><br/>
            <button onClick={() => {
              this.closeDropdown();
              this.props.showModal('delete', post);
            }}>Delete</button>
          </div>
        </div>
      );
    } else {
      return null;
    }
  }
}

/* <div>
  <a>
    <div className="navbar-settings" onClick={() => this.openDropdown()} />
  </a>
</div>
  <div id="dropdown-background" className="dd-background" onClick={() => this.closeDropdown()}>
  </div>
  <div id="settings" className="dropdown-box">
    <button onClick={() => this.props.logout()}>Log Out</button>
  </div> */

export default connect(mapStateToProps, mapDispatchToProps)(PostEditButton);