Proyectos de Subversion LeadersLinked - SPA

Rev

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