Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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