Proyectos de Subversion LeadersLinked - SPA

Rev

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