Proyectos de Subversion LeadersLinked - SPA

Rev

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