Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 7221 | Rev 7229 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
7226 stevensc 1
import React, { useEffect, useState } from 'react'
2
import { axios } from '../../utils'
3
import { Link } from 'react-router-dom'
7217 stevensc 4
import { Avatar } from '@mui/material'
7226 stevensc 5
import { addNotification } from '../../redux/notification/notification.actions'
6
import { useDispatch, useSelector } from 'react-redux'
7217 stevensc 7
import parse from 'html-react-parser'
7218 stevensc 8
import styled from 'styled-components'
7217 stevensc 9
import EditIcon from '@mui/icons-material/Edit'
10
import DeleteIcon from '@mui/icons-material/Delete'
11
 
12
import {
13
  QuestionActions,
14
  QuestionDetails,
15
  QuestionStats,
16
  QuestionUserInfo,
17
  StyledQuestionCard,
18
} from './QuestionCard'
7226 stevensc 19
import { CommentForm, CommentsList } from '../feed/CommentSection'
7218 stevensc 20
import ReactionsButton from '../UI/buttons/ReactionsButton'
7217 stevensc 21
 
7218 stevensc 22
const AnswerActions = styled(QuestionActions)`
23
  margin-bottom: 0.5rem;
24
`
25
 
7217 stevensc 26
const AnswerCard = ({
7218 stevensc 27
  // unique = '',
28
  // uuid = '',
29
  // question_uuid = '',
30
  time_elapsed = '',
7217 stevensc 31
  user_image = '',
32
  user_url = '',
33
  user_name = '',
34
  text = '',
35
  reaction = '',
36
  total_comments = 0,
37
  total_reactions = 0,
7226 stevensc 38
  comments: defaultComments = [],
7217 stevensc 39
  link_edit = '',
40
  link_delete = '',
41
  link_reaction_delete = '',
42
  link_save_reaction = '',
43
  link_add_comment = '',
7220 stevensc 44
  onEdit = () => {},
7221 stevensc 45
  onDelete = () => {},
7226 stevensc 46
  updateComments = () => {},
7217 stevensc 47
}) => {
7226 stevensc 48
  const [comments, setComments] = useState([])
49
  const [totalComments, setTotalComments] = useState(0)
7217 stevensc 50
  const labels = useSelector(({ intl }) => intl.labels)
7226 stevensc 51
  const dispatch = useDispatch()
7217 stevensc 52
 
7226 stevensc 53
  const addComment = ({ comment }) => {
54
    const formData = new FormData()
55
    formData.append('comment', comment)
56
 
57
    axios
58
      .post(link_add_comment, formData)
59
      .then((response) => {
60
        const {
61
          success,
62
          data,
63
          total_comments_answer,
64
          total_comments_question,
65
        } = response.data
66
 
67
        if (!success) {
68
          const errorMessage =
69
            typeof data === 'string'
70
              ? data
71
              : 'Error interno. Intente más tarde.'
72
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
73
          return
74
        }
75
 
76
        setComments([...comments, data.item])
77
        updateComments(total_comments_question)
78
        setTotalComments(total_comments_answer)
79
      })
80
      .catch((error) => {
81
        dispatch(
82
          addNotification({
83
            style: 'danger',
84
            msg: 'Error interno. Intente más tarde.',
85
          })
86
        )
87
        throw new Error(error)
88
      })
89
  }
90
 
91
  const deleteComment = (commentUnique, deleteCommentUrl) => {
92
    axios
93
      .post(deleteCommentUrl)
94
      .then((response) => {
95
        const {
96
          success,
97
          data,
98
          total_comments_answer,
99
          total_comments_question,
100
        } = response.data
101
 
102
        if (!success) {
103
          const errorMessage =
104
            typeof data === 'string'
105
              ? data
106
              : 'Error interno. Intente más tarde.'
107
 
108
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
109
          return
110
        }
111
 
112
        const newComments = comments.filter(
113
          ({ unique }) => unique !== commentUnique
114
        )
115
 
116
        dispatch(addNotification({ style: 'success', msg: data.message }))
117
 
118
        setComments(newComments)
119
        updateComments(total_comments_question)
120
        setTotalComments(total_comments_answer)
121
      })
122
      .catch((error) => {
123
        dispatch(
124
          addNotification({
125
            style: 'danger',
126
            msg: 'Error interno. Intente más tarde.',
127
          })
128
        )
129
        throw new Error(error)
130
      })
131
  }
132
 
133
  useEffect(() => {
134
    setComments(defaultComments)
135
  }, [defaultComments])
136
 
137
  useEffect(() => {
138
    setTotalComments(total_comments)
139
  }, [total_comments])
140
 
7217 stevensc 141
  return (
142
    <>
143
      <StyledQuestionCard>
144
        <QuestionDetails>
145
          <QuestionUserInfo>
146
            <Link to={user_url}>
147
              <Avatar
148
                src={user_image}
149
                alt={`${user_name} profile image`}
150
                sx={{ width: '50px', height: '50px' }}
151
              />
152
            </Link>
153
            <p>{user_name}</p>
154
          </QuestionUserInfo>
155
 
156
          <QuestionStats className="mb-2">
7218 stevensc 157
            <span>{`${labels.published} ${time_elapsed}`}</span>
158
            <span>{`${total_reactions} ${labels.reactions}`}</span>
7226 stevensc 159
            <span>{`${totalComments} ${labels.comments}`}</span>
7217 stevensc 160
          </QuestionStats>
161
        </QuestionDetails>
162
        {text && parse(text)}
163
 
7218 stevensc 164
        <AnswerActions>
7217 stevensc 165
          {link_edit && (
166
            <button
167
              className="btn feed__share-option"
7220 stevensc 168
              onClick={() => onEdit(link_edit, text)}
7217 stevensc 169
            >
170
              <EditIcon />
171
              {labels.edit}
172
            </button>
173
          )}
174
          {link_delete && (
175
            <button
176
              className="btn feed__share-option"
177
              onClick={() => onDelete(link_delete)}
178
            >
179
              <DeleteIcon />
180
              {labels.delete}
181
            </button>
182
          )}
7218 stevensc 183
          {link_save_reaction && (
184
            <ReactionsButton
7219 stevensc 185
              className="btn feed__share-option"
7218 stevensc 186
              currentReaction={reaction}
187
              saveUrl={link_save_reaction}
188
              deleteUrl={link_reaction_delete}
189
              withLabel
190
            />
191
          )}
192
        </AnswerActions>
7226 stevensc 193
        <CommentForm onSubmit={addComment} />
194
        <CommentsList comments={comments} onDelete={deleteComment} />
7217 stevensc 195
      </StyledQuestionCard>
196
    </>
197
  )
198
}
199
 
200
export default AnswerCard