Proyectos de Subversion LeadersLinked - SPA

Rev

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