Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 4892 | Rev 5118 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
5115 stevensc 1
/* eslint-disable camelcase */
3943 stevensc 2
/* eslint-disable react/prop-types */
4100 stevensc 3
import React, { useEffect, useRef, useState } from 'react'
4050 stevensc 4
import { axios } from '../../../../utils'
3943 stevensc 5
import { useForm } from 'react-hook-form'
6
import { connect } from 'react-redux'
7
import { addNotification } from '../../../../redux/notification/notification.actions'
3947 stevensc 8
import ConfirmModal from '../../../../shared/confirm-modal/ConfirmModal'
3943 stevensc 9
import FormErrorFeedback from '../../../../shared/form-error-feedback/FormErrorFeedback'
10
 
11
const FeedCommentSection = ({
5115 stevensc 12
  isShow = true,
13
  image = '',
14
  addUrl = '',
15
  updateTotalComments = function () { },
16
  comments = []
3943 stevensc 17
}) => {
5115 stevensc 18
  const { register, handleSubmit, errors, reset } = useForm()
19
  const [commentsState, setCommentsState] = useState(comments)
3943 stevensc 20
 
5115 stevensc 21
  useEffect(() => {
22
    setCommentsState(comments)
23
  }, [comments])
3943 stevensc 24
 
5115 stevensc 25
  const submitCommentHandler = (data) => {
26
    const currentFormData = new FormData()
27
    Object.entries(data).forEach(([key, value]) => currentFormData.append(key, value))
4822 stevensc 28
 
5115 stevensc 29
    axios.post(addUrl, currentFormData)
30
      .then(({ data: response }) => {
31
        const { data: newComment, success, total_comments } = response
3944 stevensc 32
 
5115 stevensc 33
        if (!success) {
34
          return addNotification({ style: 'danger', msg: newComment })
35
        }
3943 stevensc 36
 
5115 stevensc 37
        updateTotalComments(total_comments)
38
        setCommentsState([newComment, ...commentsState])
39
        reset()
40
      })
41
  }
3943 stevensc 42
 
5115 stevensc 43
  return (
3962 stevensc 44
        <div className={`comments-container ${isShow ? 'show' : 'hidden'}`}>
3943 stevensc 45
            <form
3964 stevensc 46
                className='feedCommentContainer'
3943 stevensc 47
                onSubmit={handleSubmit(submitCommentHandler)}
48
            >
4820 stevensc 49
                {image && <img src={image} alt="User profile image" />}
3964 stevensc 50
                <input
51
                    className='commentInput'
52
                    type="text"
53
                    name="comment"
54
                    maxLength="256"
5115 stevensc 55
                    placeholder={LABELS.WRITE_A_COMMENT}
56
                    ref={register({ required: 'El campo es requerido' })}
3964 stevensc 57
                />
58
                <button className='shareIconContainer iconActive' >
4061 stevensc 59
                    <img src='/images/icons/send.png' className='fa img-icon' />
3964 stevensc 60
                </button>
3943 stevensc 61
            </form>
62
            {errors.comment && <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>}
63
            <FeedCommentSection.CommentsList
64
                comments={commentsState}
65
                updateTotalComments={updateTotalComments}
66
                setComments={setCommentsState}
67
            />
3962 stevensc 68
        </div>
5115 stevensc 69
  )
3943 stevensc 70
}
71
 
72
const CommentsList = ({ comments, updateTotalComments, setComments }) => {
5115 stevensc 73
  const deleteCommentHandler = (commentUnique, deleteCommentUrl) => {
74
    axios.post(deleteCommentUrl)
75
      .then(({ data: response }) => {
76
        const { success, data, total_comments } = response
3943 stevensc 77
 
5115 stevensc 78
        if (!success) {
79
          return addNotification({ style: 'danger', msg: data })
80
        }
3943 stevensc 81
 
5115 stevensc 82
        updateTotalComments(total_comments)
83
        setComments(prevComments => prevComments.filter((comment) => comment.unique !== commentUnique))
84
        addNotification({ style: 'success', msg: data })
85
      })
86
      .catch((error) => addNotification({ style: 'danger', msg: error.message }))
87
  }
3943 stevensc 88
 
5115 stevensc 89
  return (
3953 stevensc 90
        <ul className='comment-list'>
91
            {comments.reverse().map((comment) => {
5115 stevensc 92
              return (
3953 stevensc 93
                    <FeedCommentSection.CommentTemplate
94
                        commentData={comment}
95
                        onDeleteHandler={deleteCommentHandler}
96
                        key={comment.unique}
97
                    />
5115 stevensc 98
              )
3953 stevensc 99
            })}
100
        </ul>
5115 stevensc 101
  )
3943 stevensc 102
}
103
 
3947 stevensc 104
const CommentTemplate = ({ onDeleteHandler, commentData }) => {
5115 stevensc 105
  const {
106
    user_name,
107
    user_url,
108
    user_image,
109
    link_delete,
110
    time_elapsed,
111
    comment,
112
    unique
113
  } = commentData
3947 stevensc 114
 
5115 stevensc 115
  const [showConfirmModal, setShowConfirmModal] = useState(false)
116
  const [displayOption, setDisplayOption] = useState(false)
117
  const deleteButton = useRef()
3947 stevensc 118
 
5115 stevensc 119
  const handleShowConfirmModal = () => setShowConfirmModal(!showConfirmModal)
3947 stevensc 120
 
5115 stevensc 121
  const handleModalAccept = () => onDeleteHandler(unique, link_delete)
3947 stevensc 122
 
5115 stevensc 123
  useEffect(() => {
124
    const handleClickOutside = (event) => {
125
      if (deleteButton.current && !deleteButton.current.contains(event.target)) {
126
        setDisplayOption(false)
127
      }
128
    }
129
    document.addEventListener('mousedown', handleClickOutside)
3947 stevensc 130
 
5115 stevensc 131
    return () => {
132
      document.removeEventListener('mousedown', handleClickOutside)
133
    }
134
  }, [deleteButton])
4100 stevensc 135
 
5115 stevensc 136
  return (
3954 stevensc 137
        <li>
138
            <div className="comment-container">
4818 stevensc 139
                {user_image &&
140
                    <img
141
                        src={user_image}
142
                        alt="user-image"
143
                        className='user-image'
144
                    />}
3954 stevensc 145
                <div className='comment-content'>
146
                    <div className='info'>
147
                        <a href={user_url}>
148
                            <h3>{user_name}</h3>
149
                        </a>
150
                        <span>
151
                            {time_elapsed}
152
                            {link_delete &&
4100 stevensc 153
                                <>
154
                                    <img
155
                                        src='/images/icons/options.png'
4106 stevensc 156
                                        className='cursor-pointer img-icon options-sm'
4100 stevensc 157
                                        onClick={() => setDisplayOption(!displayOption)}
158
                                    />
4107 stevensc 159
                                    <div className={`comments-options ${displayOption ? 'active' : ''}`}>
4100 stevensc 160
                                        <ul>
161
                                            <li>
162
                                                <button
163
                                                    className="option-btn"
164
                                                    onClick={handleShowConfirmModal}
165
                                                    ref={deleteButton}
166
                                                >
167
                                                    <i className="fa fa-trash-o mr-1" />
5115 stevensc 168
                                                    {LABELS.DELETE}
4100 stevensc 169
                                                </button>
170
                                            </li>
171
                                        </ul>
172
                                    </div>
173
                                </>
3954 stevensc 174
                            }
175
                        </span>
3947 stevensc 176
                    </div>
3954 stevensc 177
                    <p>{comment}</p>
3947 stevensc 178
                </div>
3954 stevensc 179
            </div>
3947 stevensc 180
            <ConfirmModal
181
                show={showConfirmModal}
182
                onClose={() => setShowConfirmModal(false)}
183
                onAccept={handleModalAccept}
184
                acceptLabel="Aceptar"
185
            />
5115 stevensc 186
        </li>
187
  )
188
}
3947 stevensc 189
 
3943 stevensc 190
FeedCommentSection.CommentsList = CommentsList
3947 stevensc 191
FeedCommentSection.CommentTemplate = CommentTemplate
3943 stevensc 192
 
193
const mapDispatchToProps = {
5115 stevensc 194
  addNotification: (notification) => addNotification(notification)
195
}
3943 stevensc 196
 
5115 stevensc 197
export default connect(null, mapDispatchToProps)(FeedCommentSection)