Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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