Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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