Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 552 | Rev 554 | 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'
553 stevensc 7
import styled from 'styled-components'
5 stevensc 8
 
535 stevensc 9
import { QuestionStats } from './QuestionCard'
5 stevensc 10
import { CommentForm, CommentsList } from '../feed/CommentSection'
550 stevensc 11
import Paraphrase from '../UI/Paraphrase'
12
import StyledContainer from '../widgets/WidgetLayout'
5 stevensc 13
import ReactionsButton from '../UI/buttons/ReactionsButton'
14
 
550 stevensc 15
const StyledForm = styled(CommentForm)`
553 stevensc 16
  border: 1px solid lightgray;
17
  background-color: #fff;
18
  border-radius: 30px;
19
  padding: 5px;
20
  padding-left: 1rem;
21
  flex: 1;
22
  cursor: pointer;
23
 
24
  &:hover {
25
    background-color: rgba(0, 0, 0, 0.08);
26
  }
550 stevensc 27
`
28
 
5 stevensc 29
const AnswerCard = ({
30
  time_elapsed = '',
31
  user_image = '',
32
  user_url = '',
33
  user_name = '',
34
  text = '',
35
  reaction = '',
36
  total_comments = 0,
37
  total_reactions = 0,
38
  comments: defaultComments = [],
39
  link_edit = '',
40
  link_delete = '',
41
  link_reaction_delete = '',
42
  link_save_reaction = '',
43
  link_add_comment = '',
44
  onEdit = () => {},
45
  onDelete = () => {},
46
  updateComments = () => {},
47
  updateReactions = () => {},
48
}) => {
49
  const [comments, setComments] = useState(defaultComments)
50
  const [totalComments, setTotalComments] = useState(total_comments)
51
  const [totalReactions, setTotalReactions] = useState(total_reactions)
52
  const labels = useSelector(({ intl }) => intl.labels)
53
  const dispatch = useDispatch()
54
 
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) => {
62
        const { success, data } = response.data
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])
74
        updateComments(data.total_comments_question)
75
        setTotalComments(data.total_comments_answer)
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) => {
92
        const { success, data } = response.data
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)
111
        updateComments(data.total_comments_question)
112
        setTotalComments(data.total_comments_answer)
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
 
125
  return (
126
    <>
535 stevensc 127
      <StyledContainer>
128
        <StyledContainer.Header image={user_image} title={user_name}>
536 stevensc 129
          <QuestionStats>
5 stevensc 130
            <span>{`${labels.published} ${time_elapsed}`}</span>
131
            <span>{`${totalReactions} ${labels.reactions}`}</span>
132
            <span>{`${totalComments} ${labels.comments}`}</span>
133
          </QuestionStats>
535 stevensc 134
        </StyledContainer.Header>
5 stevensc 135
 
535 stevensc 136
        <StyledContainer.Body>
137
          <Paraphrase>{text}</Paraphrase>
138
        </StyledContainer.Body>
139
 
140
        <StyledContainer.Actions>
527 stevensc 141
          {link_save_reaction && (
142
            <ReactionsButton
143
              currentReaction={reaction}
144
              saveUrl={link_save_reaction}
145
              deleteUrl={link_reaction_delete}
146
              onChange={(res) => {
147
                setTotalReactions(res.total_reactions_answer)
148
                updateReactions(res.total_reactions_question)
149
              }}
150
              withLabel
151
            />
152
          )}
553 stevensc 153
          {link_add_comment && <button></button>}
5 stevensc 154
          {link_edit && (
535 stevensc 155
            <button onClick={() => onEdit(link_edit, text)}>
5 stevensc 156
              <EditIcon />
157
              {labels.edit}
158
            </button>
159
          )}
160
          {link_delete && (
535 stevensc 161
            <button onClick={() => onDelete(link_delete)}>
5 stevensc 162
              <DeleteIcon />
163
              {labels.delete}
164
            </button>
165
          )}
535 stevensc 166
        </StyledContainer.Actions>
553 stevensc 167
 
550 stevensc 168
        <StyledForm image={user_image} onSubmit={addComment} />
5 stevensc 169
        <CommentsList comments={comments} onDelete={deleteComment} />
535 stevensc 170
      </StyledContainer>
5 stevensc 171
    </>
172
  )
173
}
174
 
175
export default AnswerCard