Proyectos de Subversion LeadersLinked - SPA

Rev

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