Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 556 | Rev 1507 | 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 = () => {},
655 stevensc 48
  updateReactions = () => {}
5 stevensc 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
      })
655 stevensc 79
      .catch((err) => {
80
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 81
      })
82
  }
83
 
84
  const deleteComment = (commentUnique, deleteCommentUrl) => {
85
    axios
86
      .post(deleteCommentUrl)
87
      .then((response) => {
88
        const { success, data } = response.data
89
 
90
        if (!success) {
91
          const errorMessage =
92
            typeof data === 'string'
93
              ? data
94
              : 'Error interno. Intente más tarde.'
95
 
96
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
97
          return
98
        }
99
 
100
        const newComments = comments.filter(
101
          ({ unique }) => unique !== commentUnique
102
        )
103
 
104
        dispatch(addNotification({ style: 'success', msg: data.message }))
105
 
106
        setComments(newComments)
107
        updateComments(data.total_comments_question)
108
        setTotalComments(data.total_comments_answer)
109
      })
655 stevensc 110
      .catch((err) => {
111
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 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
          )}
554 stevensc 143
          {link_add_comment && (
144
            <button onClick={() => setShowComments(!showComments)}>
556 stevensc 145
              <ChatOutlinedIcon />
554 stevensc 146
              {labels.comment}
147
            </button>
148
          )}
5 stevensc 149
          {link_edit && (
535 stevensc 150
            <button onClick={() => onEdit(link_edit, text)}>
5 stevensc 151
              <EditIcon />
152
              {labels.edit}
153
            </button>
154
          )}
155
          {link_delete && (
535 stevensc 156
            <button onClick={() => onDelete(link_delete)}>
5 stevensc 157
              <DeleteIcon />
158
              {labels.delete}
159
            </button>
160
          )}
535 stevensc 161
        </StyledContainer.Actions>
655 stevensc 162
        <div className='px-1 pb-1'>
554 stevensc 163
          <StyledForm image={user_image} onSubmit={addComment} />
164
          {showComments && (
165
            <CommentsList comments={comments} onDelete={deleteComment} />
166
          )}
167
        </div>
535 stevensc 168
      </StyledContainer>
5 stevensc 169
    </>
170
  )
171
}
172
 
173
export default AnswerCard