Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 6357 | | 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'
6357 stevensc 10
import useOutsideClick from '../../../../hooks/useOutsideClick'
3943 stevensc 11
 
5577 stevensc 12
const FeedComments = ({
5115 stevensc 13
  isShow = true,
14
  image = '',
15
  addUrl = '',
5577 stevensc 16
  updateTotalComments = function () {},
17
  currentComments = [],
18
  addNotification,
3943 stevensc 19
}) => {
5115 stevensc 20
  const { register, handleSubmit, errors, reset } = useForm()
5584 stevensc 21
  const [comments, setComments] = useState(currentComments)
3943 stevensc 22
 
5577 stevensc 23
  const submitCommet = (data) => {
5115 stevensc 24
    const currentFormData = new FormData()
5577 stevensc 25
    Object.entries(data).forEach(([key, value]) =>
26
      currentFormData.append(key, value)
27
    )
4822 stevensc 28
 
5577 stevensc 29
    axios.post(addUrl, currentFormData).then(({ data: response }) => {
5586 stevensc 30
      const { success, data: message, total_comments } = response
31
 
32
      if (!success) {
33
        addNotification({ style: 'danger', msg: message })
5577 stevensc 34
        return
35
      }
3944 stevensc 36
 
5586 stevensc 37
      updateTotalComments(total_comments)
38
      setComments((prevMessages) => [...prevMessages, message])
5577 stevensc 39
      reset()
40
    })
5115 stevensc 41
  }
3943 stevensc 42
 
5577 stevensc 43
  const deleteComment = (commentUnique, deleteCommentUrl) => {
44
    axios
45
      .post(deleteCommentUrl)
5115 stevensc 46
      .then(({ data: response }) => {
47
        const { success, data, total_comments } = response
3943 stevensc 48
 
5115 stevensc 49
        if (!success) {
50
          return addNotification({ style: 'danger', msg: data })
51
        }
3943 stevensc 52
 
5115 stevensc 53
        updateTotalComments(total_comments)
5577 stevensc 54
        setComments((prevComments) =>
55
          prevComments.filter((comment) => comment.unique !== commentUnique)
56
        )
5115 stevensc 57
        addNotification({ style: 'success', msg: data })
58
      })
6358 stevensc 59
      .catch((error) => addNotification({ style: 'danger', msg: error }))
5115 stevensc 60
  }
3943 stevensc 61
 
5609 stevensc 62
  useEffect(() => {
63
    setComments(currentComments)
64
  }, [currentComments])
65
 
5115 stevensc 66
  return (
5577 stevensc 67
    <div className={`comments-container ${isShow ? 'show' : 'hidden'}`}>
68
      <form
69
        className="feedCommentContainer"
70
        onSubmit={handleSubmit(submitCommet)}
71
      >
72
        {image && <img src={image} alt="User profile image" />}
73
        <input
74
          className="commentInput"
75
          type="text"
76
          name="comment"
77
          maxLength="256"
78
          placeholder={LABELS.WRITE_A_COMMENT}
79
          ref={register({ required: 'El campo es requerido' })}
80
        />
81
        <button className="shareIconContainer iconActive">
82
          <img src="/images/icons/send.png" className="fa img-icon" />
83
        </button>
84
      </form>
85
      {errors.comment && (
86
        <FormErrorFeedback>{errors.comment.message}</FormErrorFeedback>
87
      )}
88
      <FeedComments.List comments={comments} onDelete={deleteComment} />
89
    </div>
5115 stevensc 90
  )
3943 stevensc 91
}
92
 
5577 stevensc 93
const CommentsList = ({ comments, onDelete }) => {
94
  return (
95
    <ul className="comment-list">
5596 stevensc 96
      {comments.map((comment) => {
5577 stevensc 97
        return (
98
          <CommentsList.Item
99
            comment={comment}
100
            onDelete={onDelete}
101
            key={comment.unique}
102
          />
103
        )
104
      })}
105
    </ul>
106
  )
107
}
3947 stevensc 108
 
5577 stevensc 109
const CommentTemplate = ({ onDelete, comment }) => {
5582 stevensc 110
  const {
111
    unique,
112
    user_url,
113
    user_name,
114
    user_image,
115
    time_elapsed,
116
    comment: content,
117
    link_delete,
118
  } = comment
5115 stevensc 119
  const [showConfirmModal, setShowConfirmModal] = useState(false)
120
  const [displayOption, setDisplayOption] = useState(false)
6357 stevensc 121
  const deleteButton = useRef(null)
122
  const outsideClick = useOutsideClick(deleteButton)
3947 stevensc 123
 
5577 stevensc 124
  const toggleModal = () => setShowConfirmModal(!showConfirmModal)
3947 stevensc 125
 
5115 stevensc 126
  useEffect(() => {
6357 stevensc 127
    if (!outsideClick) return
128
    setDisplayOption(false)
129
  }, [outsideClick])
3947 stevensc 130
 
5115 stevensc 131
  return (
5577 stevensc 132
    <li>
133
      <div className="comment-container">
5582 stevensc 134
        {user_image && (
135
          <img src={user_image} alt="user-image" className="user-image" />
5577 stevensc 136
        )}
137
        <div className="comment-content">
138
          <div className="info">
5582 stevensc 139
            <a href={user_url}>
140
              <h3>{user_name}</h3>
5577 stevensc 141
            </a>
142
            <span>
5582 stevensc 143
              {time_elapsed}
5577 stevensc 144
              {comment.link_delete && (
145
                <>
146
                  <img
147
                    src="/images/icons/options.png"
148
                    className="cursor-pointer img-icon options-sm"
149
                    onClick={() => setDisplayOption(!displayOption)}
150
                  />
151
                  <div
152
                    className={`comments-options ${
153
                      displayOption ? 'active' : ''
154
                    }`}
155
                  >
156
                    <ul>
157
                      <li>
158
                        <button
159
                          className="option-btn"
160
                          onClick={toggleModal}
161
                          ref={deleteButton}
162
                        >
163
                          <i className="fa fa-trash-o mr-1" />
164
                          {LABELS.DELETE}
165
                        </button>
166
                      </li>
167
                    </ul>
168
                  </div>
169
                </>
170
              )}
171
            </span>
172
          </div>
5582 stevensc 173
          <p>{content}</p>
5577 stevensc 174
        </div>
175
      </div>
176
      <ConfirmModal
177
        show={showConfirmModal}
178
        onClose={toggleModal}
5582 stevensc 179
        onAccept={() => onDelete(unique, link_delete)}
5577 stevensc 180
        acceptLabel={LABELS.ACCEPT}
181
      />
182
    </li>
5115 stevensc 183
  )
184
}
3947 stevensc 185
 
5577 stevensc 186
FeedComments.List = CommentsList
187
CommentsList.Item = CommentTemplate
3943 stevensc 188
 
189
const mapDispatchToProps = {
5577 stevensc 190
  addNotification: (notification) => addNotification(notification),
5115 stevensc 191
}
3943 stevensc 192
 
5577 stevensc 193
export default connect(null, mapDispatchToProps)(FeedComments)