Proyectos de Subversion LeadersLinked - SPA

Rev

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