|
1 |
| -import React, { Component } from 'react'; |
| 1 | +import React, { useRef } from 'react'; |
2 | 2 | import ProfileImageWithDefault from './ProfileImageWithDefault';
|
3 | 3 | import { format } from 'timeago.js';
|
4 | 4 | import { Link } from 'react-router-dom';
|
5 | 5 | import { connect } from 'react-redux';
|
| 6 | +import useClickTracker from '../shared/useClickTracker'; |
6 | 7 |
|
7 |
| -class HoaxView extends Component { |
8 |
| - render() { |
9 |
| - const { hoax, onClickDelete } = this.props; |
10 |
| - const { user, date } = hoax; |
11 |
| - const { username, displayName, image } = user; |
12 |
| - const relativeDate = format(date); |
13 |
| - const attachmentImageVisible = |
14 |
| - hoax.attachment && hoax.attachment.fileType.startsWith('image'); |
| 8 | +const HoaxView = (props) => { |
| 9 | + const actionArea = useRef(); |
| 10 | + const dropDownVisible = useClickTracker(actionArea); |
| 11 | + const { hoax, onClickDelete } = props; |
| 12 | + const { user, date } = hoax; |
| 13 | + const { username, displayName, image } = user; |
| 14 | + const relativeDate = format(date); |
| 15 | + const attachmentImageVisible = |
| 16 | + hoax.attachment && hoax.attachment.fileType.startsWith('image'); |
15 | 17 |
|
16 |
| - const ownedByLoggedInUser = user.id === this.props.loggedInUser.id; |
17 |
| - return ( |
18 |
| - <div className="card p-1"> |
19 |
| - <div className="d-flex"> |
20 |
| - <ProfileImageWithDefault |
21 |
| - className="rounded-circle m-1" |
22 |
| - width="32" |
23 |
| - height="32" |
24 |
| - image={image} |
25 |
| - /> |
26 |
| - <div className="flex-fill m-auto pl-2"> |
27 |
| - <Link to={`/${username}`} className="list-group-item-action"> |
28 |
| - <h6 className="d-inline"> |
29 |
| - {displayName}@{username} |
30 |
| - </h6> |
31 |
| - </Link> |
32 |
| - <span className="text-black-50"> - </span> |
33 |
| - <span className="text-black-50">{relativeDate}</span> |
34 |
| - </div> |
35 |
| - {ownedByLoggedInUser && ( |
36 |
| - <button |
37 |
| - className="btn btn-outline-danger btn-sm" |
38 |
| - onClick={onClickDelete} |
39 |
| - > |
40 |
| - <i className="far fa-trash-alt" /> |
41 |
| - </button> |
42 |
| - )} |
| 18 | + const ownedByLoggedInUser = user.id === props.loggedInUser.id; |
| 19 | + |
| 20 | + let dropDownClass = 'p-0 shadow dropdown-menu'; |
| 21 | + if (dropDownVisible) { |
| 22 | + dropDownClass += ' show'; |
| 23 | + } |
| 24 | + |
| 25 | + return ( |
| 26 | + <div className="card p-1"> |
| 27 | + <div className="d-flex"> |
| 28 | + <ProfileImageWithDefault |
| 29 | + className="rounded-circle m-1" |
| 30 | + width="32" |
| 31 | + height="32" |
| 32 | + image={image} |
| 33 | + /> |
| 34 | + <div className="flex-fill m-auto pl-2"> |
| 35 | + <Link to={`/${username}`} className="list-group-item-action"> |
| 36 | + <h6 className="d-inline"> |
| 37 | + {displayName}@{username} |
| 38 | + </h6> |
| 39 | + </Link> |
| 40 | + <span className="text-black-50"> - </span> |
| 41 | + <span className="text-black-50">{relativeDate}</span> |
43 | 42 | </div>
|
44 |
| - <div className="pl-5">{hoax.content}</div> |
45 |
| - {attachmentImageVisible && ( |
46 |
| - <div className="pl-5"> |
47 |
| - <img |
48 |
| - alt="attachment" |
49 |
| - src={`/images/attachments/${hoax.attachment.name}`} |
50 |
| - className="img-fluid" |
| 43 | + {ownedByLoggedInUser && ( |
| 44 | + <div className="dropdown"> |
| 45 | + <span |
| 46 | + className="btn btn-sm btn-light dropdown-toggle" |
| 47 | + data-testid="hoax-actions" |
| 48 | + ref={actionArea} |
51 | 49 | />
|
| 50 | + <div className={dropDownClass} data-testid="hoax-action-dropdown"> |
| 51 | + <button |
| 52 | + className="btn btn-outline-danger btn-sm btn-block text-left" |
| 53 | + onClick={onClickDelete} |
| 54 | + > |
| 55 | + <i className="far fa-trash-alt" /> Delete |
| 56 | + </button> |
| 57 | + </div> |
52 | 58 | </div>
|
53 | 59 | )}
|
54 | 60 | </div>
|
55 |
| - ); |
56 |
| - } |
57 |
| -} |
| 61 | + <div className="pl-5">{hoax.content}</div> |
| 62 | + {attachmentImageVisible && ( |
| 63 | + <div className="pl-5"> |
| 64 | + <img |
| 65 | + alt="attachment" |
| 66 | + src={`/images/attachments/${hoax.attachment.name}`} |
| 67 | + className="img-fluid" |
| 68 | + /> |
| 69 | + </div> |
| 70 | + )} |
| 71 | + </div> |
| 72 | + ); |
| 73 | +}; |
58 | 74 |
|
59 | 75 | const mapStateToProps = (state) => {
|
60 | 76 | return {
|
61 |
| - loggedInUser: state |
| 77 | + loggedInUser: state, |
62 | 78 | };
|
63 | 79 | };
|
64 | 80 |
|
|
0 commit comments