Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 527 | Rev 536 | 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 { addNotification } from '../../redux/notification/notification.actions'
4
import { useDispatch, useSelector } from 'react-redux'
5
import EditIcon from '@mui/icons-material/Edit'
6
import DeleteIcon from '@mui/icons-material/Delete'
7
 
535 stevensc 8
import StyledContainer from '../widgets/WidgetLayout'
9
import { QuestionStats } from './QuestionCard'
5 stevensc 10
import { CommentForm, CommentsList } from '../feed/CommentSection'
11
import ReactionsButton from '../UI/buttons/ReactionsButton'
535 stevensc 12
import Paraphrase from '../UI/Paraphrase'
5 stevensc 13
 
14
const AnswerCard = ({
15
  time_elapsed = '',
16
  user_image = '',
17
  user_url = '',
18
  user_name = '',
19
  text = '',
20
  reaction = '',
21
  total_comments = 0,
22
  total_reactions = 0,
23
  comments: defaultComments = [],
24
  link_edit = '',
25
  link_delete = '',
26
  link_reaction_delete = '',
27
  link_save_reaction = '',
28
  link_add_comment = '',
29
  onEdit = () => {},
30
  onDelete = () => {},
31
  updateComments = () => {},
32
  updateReactions = () => {},
33
}) => {
34
  const [comments, setComments] = useState(defaultComments)
35
  const [totalComments, setTotalComments] = useState(total_comments)
36
  const [totalReactions, setTotalReactions] = useState(total_reactions)
37
  const labels = useSelector(({ intl }) => intl.labels)
38
  const dispatch = useDispatch()
39
 
40
  const addComment = ({ comment }) => {
41
    const formData = new FormData()
42
    formData.append('comment', comment)
43
 
44
    axios
45
      .post(link_add_comment, formData)
46
      .then((response) => {
47
        const { success, data } = response.data
48
 
49
        if (!success) {
50
          const errorMessage =
51
            typeof data === 'string'
52
              ? data
53
              : 'Error interno. Intente más tarde.'
54
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
55
          return
56
        }
57
 
58
        setComments([...comments, data.item])
59
        updateComments(data.total_comments_question)
60
        setTotalComments(data.total_comments_answer)
61
      })
62
      .catch((error) => {
63
        dispatch(
64
          addNotification({
65
            style: 'danger',
66
            msg: 'Error interno. Intente más tarde.',
67
          })
68
        )
69
        throw new Error(error)
70
      })
71
  }
72
 
73
  const deleteComment = (commentUnique, deleteCommentUrl) => {
74
    axios
75
      .post(deleteCommentUrl)
76
      .then((response) => {
77
        const { success, data } = response.data
78
 
79
        if (!success) {
80
          const errorMessage =
81
            typeof data === 'string'
82
              ? data
83
              : 'Error interno. Intente más tarde.'
84
 
85
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
86
          return
87
        }
88
 
89
        const newComments = comments.filter(
90
          ({ unique }) => unique !== commentUnique
91
        )
92
 
93
        dispatch(addNotification({ style: 'success', msg: data.message }))
94
 
95
        setComments(newComments)
96
        updateComments(data.total_comments_question)
97
        setTotalComments(data.total_comments_answer)
98
      })
99
      .catch((error) => {
100
        dispatch(
101
          addNotification({
102
            style: 'danger',
103
            msg: 'Error interno. Intente más tarde.',
104
          })
105
        )
106
        throw new Error(error)
107
      })
108
  }
109
 
110
  return (
111
    <>
535 stevensc 112
      <StyledContainer>
113
        <StyledContainer.Header image={user_image} title={user_name}>
5 stevensc 114
          <QuestionStats className="mb-2">
115
            <span>{`${labels.published} ${time_elapsed}`}</span>
116
            <span>{`${totalReactions} ${labels.reactions}`}</span>
117
            <span>{`${totalComments} ${labels.comments}`}</span>
118
          </QuestionStats>
535 stevensc 119
        </StyledContainer.Header>
5 stevensc 120
 
535 stevensc 121
        <StyledContainer.Body>
122
          <Paraphrase>{text}</Paraphrase>
123
        </StyledContainer.Body>
124
 
125
        <StyledContainer.Actions>
527 stevensc 126
          {link_save_reaction && (
127
            <ReactionsButton
128
              currentReaction={reaction}
129
              saveUrl={link_save_reaction}
130
              deleteUrl={link_reaction_delete}
131
              onChange={(res) => {
132
                setTotalReactions(res.total_reactions_answer)
133
                updateReactions(res.total_reactions_question)
134
              }}
135
              withLabel
136
            />
137
          )}
5 stevensc 138
          {link_edit && (
535 stevensc 139
            <button onClick={() => onEdit(link_edit, text)}>
5 stevensc 140
              <EditIcon />
141
              {labels.edit}
142
            </button>
143
          )}
144
          {link_delete && (
535 stevensc 145
            <button onClick={() => onDelete(link_delete)}>
5 stevensc 146
              <DeleteIcon />
147
              {labels.delete}
148
            </button>
149
          )}
535 stevensc 150
        </StyledContainer.Actions>
5 stevensc 151
        <CommentForm onSubmit={addComment} />
152
        <CommentsList comments={comments} onDelete={deleteComment} />
535 stevensc 153
      </StyledContainer>
5 stevensc 154
    </>
155
  )
156
}
157
 
158
export default AnswerCard