Proyectos de Subversion LeadersLinked - SPA

Rev

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