Proyectos de Subversion LeadersLinked - SPA

Rev

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