Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 554 | Rev 556 | 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)
554 stevensc 52
  const [showComments, setShowComments] = useState(false)
5 stevensc 53
  const labels = useSelector(({ intl }) => intl.labels)
54
  const dispatch = useDispatch()
55
 
56
  const addComment = ({ comment }) => {
57
    const formData = new FormData()
58
    formData.append('comment', comment)
59
 
60
    axios
61
      .post(link_add_comment, formData)
62
      .then((response) => {
63
        const { success, data } = response.data
64
 
65
        if (!success) {
66
          const errorMessage =
67
            typeof data === 'string'
68
              ? data
69
              : 'Error interno. Intente más tarde.'
70
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
71
          return
72
        }
73
 
74
        setComments([...comments, data.item])
75
        updateComments(data.total_comments_question)
76
        setTotalComments(data.total_comments_answer)
77
      })
78
      .catch((error) => {
79
        dispatch(
80
          addNotification({
81
            style: 'danger',
82
            msg: 'Error interno. Intente más tarde.',
83
          })
84
        )
85
        throw new Error(error)
86
      })
87
  }
88
 
89
  const deleteComment = (commentUnique, deleteCommentUrl) => {
90
    axios
91
      .post(deleteCommentUrl)
92
      .then((response) => {
93
        const { success, data } = response.data
94
 
95
        if (!success) {
96
          const errorMessage =
97
            typeof data === 'string'
98
              ? data
99
              : 'Error interno. Intente más tarde.'
100
 
101
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
102
          return
103
        }
104
 
105
        const newComments = comments.filter(
106
          ({ unique }) => unique !== commentUnique
107
        )
108
 
109
        dispatch(addNotification({ style: 'success', msg: data.message }))
110
 
111
        setComments(newComments)
112
        updateComments(data.total_comments_question)
113
        setTotalComments(data.total_comments_answer)
114
      })
115
      .catch((error) => {
116
        dispatch(
117
          addNotification({
118
            style: 'danger',
119
            msg: 'Error interno. Intente más tarde.',
120
          })
121
        )
122
        throw new Error(error)
123
      })
124
  }
125
 
126
  return (
127
    <>
535 stevensc 128
      <StyledContainer>
129
        <StyledContainer.Header image={user_image} title={user_name}>
536 stevensc 130
          <QuestionStats>
5 stevensc 131
            <span>{`${labels.published} ${time_elapsed}`}</span>
132
            <span>{`${totalReactions} ${labels.reactions}`}</span>
133
            <span>{`${totalComments} ${labels.comments}`}</span>
134
          </QuestionStats>
535 stevensc 135
        </StyledContainer.Header>
5 stevensc 136
 
535 stevensc 137
        <StyledContainer.Body>
138
          <Paraphrase>{text}</Paraphrase>
139
        </StyledContainer.Body>
140
 
141
        <StyledContainer.Actions>
527 stevensc 142
          {link_save_reaction && (
143
            <ReactionsButton
144
              currentReaction={reaction}
145
              saveUrl={link_save_reaction}
146
              deleteUrl={link_reaction_delete}
147
              onChange={(res) => {
148
                setTotalReactions(res.total_reactions_answer)
149
                updateReactions(res.total_reactions_question)
150
              }}
151
              withLabel
152
            />
153
          )}
554 stevensc 154
          {link_add_comment && (
155
            <button onClick={() => setShowComments(!showComments)}>
156
              {labels.comment}
157
            </button>
158
          )}
5 stevensc 159
          {link_edit && (
535 stevensc 160
            <button onClick={() => onEdit(link_edit, text)}>
5 stevensc 161
              <EditIcon />
162
              {labels.edit}
163
            </button>
164
          )}
165
          {link_delete && (
535 stevensc 166
            <button onClick={() => onDelete(link_delete)}>
5 stevensc 167
              <DeleteIcon />
168
              {labels.delete}
169
            </button>
170
          )}
535 stevensc 171
        </StyledContainer.Actions>
555 stevensc 172
        <div className="px-1 pb-1">
554 stevensc 173
          <StyledForm image={user_image} onSubmit={addComment} />
174
          {showComments && (
175
            <CommentsList comments={comments} onDelete={deleteComment} />
176
          )}
177
        </div>
535 stevensc 178
      </StyledContainer>
5 stevensc 179
    </>
180
  )
181
}
182
 
183
export default AnswerCard