Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 2885 | Rev 2889 | 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'
4
import { ChatOutlined, Edit, Delete } 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 { QuestionStats } from './QuestionCard'
11
import Widget from '@components/UI/Widget'
12
import Button from '@components/UI/buttons/Buttons'
13
import CommentForm from '@components/dashboard/linkedin/comments/comment-form'
14
import CommentsList from '@components/dashboard/linkedin/comments/comment-list'
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}
109
          renderAction={() => (
110
            <QuestionStats>
111
              <span>{`${labels.published} ${time_elapsed}`}</span>
112
              <span>{`${totalReactions} ${labels.reactions}`}</span>
113
              <span>{`${totalComments} ${labels.comments}`}</span>
114
            </QuestionStats>
115
          )}
116
        />
5 stevensc 117
 
2888 stevensc 118
        <Widget.Body>
2864 stevensc 119
          <Typography>{parse(text)}</Typography>
2888 stevensc 120
        </Widget.Body>
535 stevensc 121
 
2888 stevensc 122
        <Widget.Actions>
527 stevensc 123
          {link_save_reaction && (
124
            <ReactionsButton
2162 stevensc 125
              currentReactionType={reaction}
527 stevensc 126
              saveUrl={link_save_reaction}
127
              deleteUrl={link_reaction_delete}
2162 stevensc 128
              onReaction={({
129
                total_reactions_answer,
130
                total_reactions_question
131
              }) => {
132
                setTotalReactions(total_reactions_answer)
133
                updateReactions(total_reactions_question)
527 stevensc 134
              }}
135
            />
136
          )}
554 stevensc 137
          {link_add_comment && (
2888 stevensc 138
            <Button onClick={() => setShowComments(!showComments)}>
139
              <ChatOutlined />
554 stevensc 140
              {labels.comment}
2888 stevensc 141
            </Button>
554 stevensc 142
          )}
5 stevensc 143
          {link_edit && (
2888 stevensc 144
            <Button onClick={() => onEdit(link_edit, text)}>
145
              <Edit />
5 stevensc 146
              {labels.edit}
2888 stevensc 147
            </Button>
5 stevensc 148
          )}
149
          {link_delete && (
2888 stevensc 150
            <Button onClick={() => onDelete(link_delete)}>
151
              <Delete />
5 stevensc 152
              {labels.delete}
2888 stevensc 153
            </Button>
5 stevensc 154
          )}
2888 stevensc 155
        </Widget.Actions>
1650 stevensc 156
 
2888 stevensc 157
        <Box sx={{ padding: 0.5 }}>
158
          <CommentForm onSubmit={addComment} />
159
          <CommentsList comments={comments} onDelete={deleteComment} />
160
        </Box>
161
      </Widget>
5 stevensc 162
    </>
163
  )
164
}
165
 
166
export default AnswerCard