Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
5 stevensc 1
import React, { useState } from 'react'
2
import { axios } from '../../utils'
3
import { Link } from 'react-router-dom'
4
import { Avatar } from '@mui/material'
5
import { addNotification } from '../../redux/notification/notification.actions'
6
import { useDispatch, useSelector } from 'react-redux'
7
import parse from 'html-react-parser'
8
import styled from 'styled-components'
9
import EditIcon from '@mui/icons-material/Edit'
10
import DeleteIcon from '@mui/icons-material/Delete'
11
 
12
import {
13
  QuestionActions,
14
  QuestionDetails,
15
  QuestionStats,
16
  QuestionUserInfo,
17
  StyledQuestionCard,
18
} from './QuestionCard'
19
import { CommentForm, CommentsList } from '../feed/CommentSection'
20
import ReactionsButton from '../UI/buttons/ReactionsButton'
21
 
22
const AnswerActions = styled(QuestionActions)`
23
  margin-bottom: 0.5rem;
388 andreina 24
  .no_margin{
25
    margin: 0px -15px !important;
26
  }
5 stevensc 27
`
28
 
29
const AnswerCard = ({
30
  // unique = '',
31
  // uuid = '',
32
  // question_uuid = '',
33
  time_elapsed = '',
34
  user_image = '',
35
  user_url = '',
36
  user_name = '',
37
  text = '',
38
  reaction = '',
39
  total_comments = 0,
40
  total_reactions = 0,
41
  comments: defaultComments = [],
42
  link_edit = '',
43
  link_delete = '',
44
  link_reaction_delete = '',
45
  link_save_reaction = '',
46
  link_add_comment = '',
47
  onEdit = () => {},
48
  onDelete = () => {},
49
  updateComments = () => {},
50
  updateReactions = () => {},
51
}) => {
52
  const [comments, setComments] = useState(defaultComments)
53
  const [totalComments, setTotalComments] = useState(total_comments)
54
  const [totalReactions, setTotalReactions] = useState(total_reactions)
55
  const labels = useSelector(({ intl }) => intl.labels)
56
  const dispatch = useDispatch()
57
 
58
  const addComment = ({ comment }) => {
59
    const formData = new FormData()
60
    formData.append('comment', comment)
61
 
62
    axios
63
      .post(link_add_comment, formData)
64
      .then((response) => {
65
        const { success, data } = response.data
66
 
67
        if (!success) {
68
          const errorMessage =
69
            typeof data === 'string'
70
              ? data
71
              : 'Error interno. Intente más tarde.'
72
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
73
          return
74
        }
75
 
76
        setComments([...comments, data.item])
77
        updateComments(data.total_comments_question)
78
        setTotalComments(data.total_comments_answer)
79
      })
80
      .catch((error) => {
81
        dispatch(
82
          addNotification({
83
            style: 'danger',
84
            msg: 'Error interno. Intente más tarde.',
85
          })
86
        )
87
        throw new Error(error)
88
      })
89
  }
90
 
91
  const deleteComment = (commentUnique, deleteCommentUrl) => {
92
    axios
93
      .post(deleteCommentUrl)
94
      .then((response) => {
95
        const { success, data } = response.data
96
 
97
        if (!success) {
98
          const errorMessage =
99
            typeof data === 'string'
100
              ? data
101
              : 'Error interno. Intente más tarde.'
102
 
103
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
104
          return
105
        }
106
 
107
        const newComments = comments.filter(
108
          ({ unique }) => unique !== commentUnique
109
        )
110
 
111
        dispatch(addNotification({ style: 'success', msg: data.message }))
112
 
113
        setComments(newComments)
114
        updateComments(data.total_comments_question)
115
        setTotalComments(data.total_comments_answer)
116
      })
117
      .catch((error) => {
118
        dispatch(
119
          addNotification({
120
            style: 'danger',
121
            msg: 'Error interno. Intente más tarde.',
122
          })
123
        )
124
        throw new Error(error)
125
      })
126
  }
127
 
128
  return (
129
    <>
130
      <StyledQuestionCard>
131
        <QuestionDetails>
132
          <QuestionUserInfo>
133
            <Link to={user_url}>
134
              <Avatar
135
                src={user_image}
136
                alt={`${user_name} profile image`}
137
                sx={{ width: '50px', height: '50px' }}
138
              />
139
            </Link>
140
            <p>{user_name}</p>
141
          </QuestionUserInfo>
142
 
143
          <QuestionStats className="mb-2">
144
            <span>{`${labels.published} ${time_elapsed}`}</span>
145
            <span>{`${totalReactions} ${labels.reactions}`}</span>
146
            <span>{`${totalComments} ${labels.comments}`}</span>
147
          </QuestionStats>
148
        </QuestionDetails>
149
        {text && parse(text)}
150
 
151
        <AnswerActions>
152
          {link_edit && (
153
            <button
154
              className="btn feed__share-option"
155
              onClick={() => onEdit(link_edit, text)}
156
            >
157
              <EditIcon />
158
              {labels.edit}
159
            </button>
160
          )}
161
          {link_delete && (
162
            <button
163
              className="btn feed__share-option"
164
              onClick={() => onDelete(link_delete)}
165
            >
166
              <DeleteIcon />
167
              {labels.delete}
168
            </button>
169
          )}
170
          {link_save_reaction && (
171
            <ReactionsButton
172
              className="btn feed__share-option"
173
              currentReaction={reaction}
174
              saveUrl={link_save_reaction}
175
              deleteUrl={link_reaction_delete}
176
              onChange={(res) => {
177
                setTotalReactions(res.total_reactions_answer)
178
                updateReactions(res.total_reactions_question)
179
              }}
180
              withLabel
181
            />
182
          )}
183
        </AnswerActions>
184
        <CommentForm onSubmit={addComment} />
185
        <CommentsList comments={comments} onDelete={deleteComment} />
186
      </StyledQuestionCard>
187
    </>
188
  )
189
}
190
 
191
export default AnswerCard