Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7231 | | 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 = () => {},
7231 stevensc 47
  updateReactions = () => {},
7217 stevensc 48
}) => {
7229 stevensc 49
  const [comments, setComments] = useState(defaultComments)
50
  const [totalComments, setTotalComments] = useState(total_comments)
7230 stevensc 51
  const [totalReactions, setTotalReactions] = useState(total_reactions)
7217 stevensc 52
  const labels = useSelector(({ intl }) => intl.labels)
7226 stevensc 53
  const dispatch = useDispatch()
7217 stevensc 54
 
7226 stevensc 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) => {
7229 stevensc 62
        const { success, data } = response.data
7226 stevensc 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])
7229 stevensc 74
        updateComments(data.total_comments_question)
75
        setTotalComments(data.total_comments_answer)
7226 stevensc 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) => {
7229 stevensc 92
        const { success, data } = response.data
7226 stevensc 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)
7229 stevensc 111
        updateComments(data.total_comments_question)
112
        setTotalComments(data.total_comments_answer)
7226 stevensc 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
 
7217 stevensc 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">
7218 stevensc 141
            <span>{`${labels.published} ${time_elapsed}`}</span>
7230 stevensc 142
            <span>{`${totalReactions} ${labels.reactions}`}</span>
7226 stevensc 143
            <span>{`${totalComments} ${labels.comments}`}</span>
7217 stevensc 144
          </QuestionStats>
145
        </QuestionDetails>
146
        {text && parse(text)}
147
 
7218 stevensc 148
        <AnswerActions>
7217 stevensc 149
          {link_edit && (
150
            <button
151
              className="btn feed__share-option"
7220 stevensc 152
              onClick={() => onEdit(link_edit, text)}
7217 stevensc 153
            >
154
              <EditIcon />
155
              {labels.edit}
156
            </button>
157
          )}
158
          {link_delete && (
159
            <button
160
              className="btn feed__share-option"
161
              onClick={() => onDelete(link_delete)}
162
            >
163
              <DeleteIcon />
164
              {labels.delete}
165
            </button>
166
          )}
7218 stevensc 167
          {link_save_reaction && (
168
            <ReactionsButton
7219 stevensc 169
              className="btn feed__share-option"
7218 stevensc 170
              currentReaction={reaction}
171
              saveUrl={link_save_reaction}
172
              deleteUrl={link_reaction_delete}
7232 stevensc 173
              onChange={(res) => {
174
                setTotalReactions(res.total_reactions_answer)
175
                updateReactions(res.total_reactions_question)
7230 stevensc 176
              }}
7218 stevensc 177
              withLabel
178
            />
179
          )}
180
        </AnswerActions>
7226 stevensc 181
        <CommentForm onSubmit={addComment} />
182
        <CommentsList comments={comments} onDelete={deleteComment} />
7217 stevensc 183
      </StyledQuestionCard>
184
    </>
185
  )
186
}
187
 
188
export default AnswerCard