Proyectos de Subversion LeadersLinked - SPA

Rev

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