Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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