Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2889 | Rev 2891 | 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 { useDispatch, useSelector } from 'react-redux'
2888 stevensc 3
import { Box, Typography } from '@mui/material'
2889 stevensc 4
import { ChatOutlined, Edit } from '@mui/icons-material'
5 stevensc 5
 
2888 stevensc 6
import { axios, parse } from '@utils'
2885 stevensc 7
import { withReactions } from '@hocs'
2888 stevensc 8
import { addNotification } from '@store/notification/notification.actions'
5 stevensc 9
 
2888 stevensc 10
import Widget from '@components/UI/Widget'
11
import Button from '@components/UI/buttons/Buttons'
12
import CommentForm from '@components/dashboard/linkedin/comments/comment-form'
13
import CommentsList from '@components/dashboard/linkedin/comments/comment-list'
2889 stevensc 14
import Options from '@components/UI/Option'
553 stevensc 15
 
5 stevensc 16
const AnswerCard = ({
17
  time_elapsed = '',
18
  user_image = '',
19
  user_url = '',
20
  user_name = '',
21
  text = '',
22
  reaction = '',
23
  total_comments = 0,
24
  total_reactions = 0,
25
  comments: defaultComments = [],
26
  link_edit = '',
27
  link_delete = '',
28
  link_reaction_delete = '',
29
  link_save_reaction = '',
30
  link_add_comment = '',
31
  onEdit = () => {},
32
  onDelete = () => {},
33
  updateComments = () => {},
655 stevensc 34
  updateReactions = () => {}
5 stevensc 35
}) => {
36
  const [comments, setComments] = useState(defaultComments)
37
  const [totalComments, setTotalComments] = useState(total_comments)
38
  const [totalReactions, setTotalReactions] = useState(total_reactions)
554 stevensc 39
  const [showComments, setShowComments] = useState(false)
5 stevensc 40
  const labels = useSelector(({ intl }) => intl.labels)
41
  const dispatch = useDispatch()
42
 
1896 stevensc 43
  const addComment = (comment) => {
1979 stevensc 44
    const formData = new FormData()
45
    formData.append('comment', comment)
46
 
5 stevensc 47
    axios
1979 stevensc 48
      .post(link_add_comment, formData)
5 stevensc 49
      .then((response) => {
50
        const { success, data } = response.data
51
 
52
        if (!success) {
53
          const errorMessage =
54
            typeof data === 'string'
55
              ? data
56
              : 'Error interno. Intente más tarde.'
57
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
58
          return
59
        }
60
 
61
        setComments([...comments, data.item])
62
        updateComments(data.total_comments_question)
63
        setTotalComments(data.total_comments_answer)
64
      })
655 stevensc 65
      .catch((err) => {
66
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 67
      })
68
  }
69
 
70
  const deleteComment = (commentUnique, deleteCommentUrl) => {
71
    axios
72
      .post(deleteCommentUrl)
73
      .then((response) => {
74
        const { success, data } = response.data
75
 
76
        if (!success) {
77
          const errorMessage =
78
            typeof data === 'string'
79
              ? data
80
              : 'Error interno. Intente más tarde.'
81
 
82
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
83
          return
84
        }
85
 
86
        const newComments = comments.filter(
87
          ({ unique }) => unique !== commentUnique
88
        )
89
 
90
        dispatch(addNotification({ style: 'success', msg: data.message }))
91
 
92
        setComments(newComments)
93
        updateComments(data.total_comments_question)
94
        setTotalComments(data.total_comments_answer)
95
      })
655 stevensc 96
      .catch((err) => {
97
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 98
      })
99
  }
100
 
2162 stevensc 101
  const ReactionsButton = withReactions(Button)
102
 
5 stevensc 103
  return (
104
    <>
2888 stevensc 105
      <Widget>
106
        <Widget.Header
107
          avatar={user_image}
108
          title={user_name}
2890 stevensc 109
          subheader={time_elapsed}
2888 stevensc 110
          renderAction={() => (
2889 stevensc 111
            <Options>
112
              {link_delete && (
113
                <Options.Item onClick={() => onDelete(link_delete)}>
114
                  Borrar
115
                </Options.Item>
116
              )}
117
            </Options>
2888 stevensc 118
          )}
119
        />
5 stevensc 120
 
2888 stevensc 121
        <Widget.Body>
2864 stevensc 122
          <Typography>{parse(text)}</Typography>
2890 stevensc 123
 
124
          <Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
125
            <Typography variant='body2'>{`${totalReactions} ${labels.reactions}`}</Typography>
126
            <Typography variant='body2'>{`${totalComments} ${labels.comments}`}</Typography>
127
          </Box>
2888 stevensc 128
        </Widget.Body>
535 stevensc 129
 
2888 stevensc 130
        <Widget.Actions>
527 stevensc 131
          {link_save_reaction && (
132
            <ReactionsButton
2162 stevensc 133
              currentReactionType={reaction}
527 stevensc 134
              saveUrl={link_save_reaction}
135
              deleteUrl={link_reaction_delete}
2162 stevensc 136
              onReaction={({
137
                total_reactions_answer,
138
                total_reactions_question
139
              }) => {
140
                setTotalReactions(total_reactions_answer)
141
                updateReactions(total_reactions_question)
527 stevensc 142
              }}
143
            />
144
          )}
554 stevensc 145
          {link_add_comment && (
2888 stevensc 146
            <Button onClick={() => setShowComments(!showComments)}>
147
              <ChatOutlined />
554 stevensc 148
              {labels.comment}
2888 stevensc 149
            </Button>
554 stevensc 150
          )}
5 stevensc 151
          {link_edit && (
2888 stevensc 152
            <Button onClick={() => onEdit(link_edit, text)}>
153
              <Edit />
5 stevensc 154
              {labels.edit}
2888 stevensc 155
            </Button>
5 stevensc 156
          )}
2888 stevensc 157
        </Widget.Actions>
1650 stevensc 158
 
2890 stevensc 159
        <Box sx={{ padding: 0.5, display: showComments ? 'block' : 'none' }}>
2888 stevensc 160
          <CommentForm onSubmit={addComment} />
161
          <CommentsList comments={comments} onDelete={deleteComment} />
162
        </Box>
163
      </Widget>
5 stevensc 164
    </>
165
  )
166
}
167
 
168
export default AnswerCard