Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 1896 | Rev 1979 | 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
    axios
1971 stevensc 60
      .post(link_add_comment, { comment })
5 stevensc 61
      .then((response) => {
62
        const { success, data } = response.data
63
 
64
        if (!success) {
65
          const errorMessage =
66
            typeof data === 'string'
67
              ? data
68
              : 'Error interno. Intente más tarde.'
69
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
70
          return
71
        }
72
 
73
        setComments([...comments, data.item])
74
        updateComments(data.total_comments_question)
75
        setTotalComments(data.total_comments_answer)
76
      })
655 stevensc 77
      .catch((err) => {
78
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 79
      })
80
  }
81
 
82
  const deleteComment = (commentUnique, deleteCommentUrl) => {
83
    axios
84
      .post(deleteCommentUrl)
85
      .then((response) => {
86
        const { success, data } = response.data
87
 
88
        if (!success) {
89
          const errorMessage =
90
            typeof data === 'string'
91
              ? data
92
              : 'Error interno. Intente más tarde.'
93
 
94
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
95
          return
96
        }
97
 
98
        const newComments = comments.filter(
99
          ({ unique }) => unique !== commentUnique
100
        )
101
 
102
        dispatch(addNotification({ style: 'success', msg: data.message }))
103
 
104
        setComments(newComments)
105
        updateComments(data.total_comments_question)
106
        setTotalComments(data.total_comments_answer)
107
      })
655 stevensc 108
      .catch((err) => {
109
        dispatch(addNotification({ style: 'danger', msg: err.message }))
5 stevensc 110
      })
111
  }
112
 
113
  return (
114
    <>
1507 stevensc 115
      <WidgetWrapper>
116
        <WidgetWrapper.Header image={user_image} title={user_name}>
536 stevensc 117
          <QuestionStats>
5 stevensc 118
            <span>{`${labels.published} ${time_elapsed}`}</span>
119
            <span>{`${totalReactions} ${labels.reactions}`}</span>
120
            <span>{`${totalComments} ${labels.comments}`}</span>
121
          </QuestionStats>
1507 stevensc 122
        </WidgetWrapper.Header>
5 stevensc 123
 
1507 stevensc 124
        <WidgetWrapper.Body>
535 stevensc 125
          <Paraphrase>{text}</Paraphrase>
1507 stevensc 126
        </WidgetWrapper.Body>
535 stevensc 127
 
1507 stevensc 128
        <WidgetWrapper.Actions>
527 stevensc 129
          {link_save_reaction && (
130
            <ReactionsButton
131
              currentReaction={reaction}
132
              saveUrl={link_save_reaction}
133
              deleteUrl={link_reaction_delete}
134
              onChange={(res) => {
135
                setTotalReactions(res.total_reactions_answer)
136
                updateReactions(res.total_reactions_question)
137
              }}
138
              withLabel
139
            />
140
          )}
554 stevensc 141
          {link_add_comment && (
142
            <button onClick={() => setShowComments(!showComments)}>
556 stevensc 143
              <ChatOutlinedIcon />
554 stevensc 144
              {labels.comment}
145
            </button>
146
          )}
5 stevensc 147
          {link_edit && (
535 stevensc 148
            <button onClick={() => onEdit(link_edit, text)}>
5 stevensc 149
              <EditIcon />
150
              {labels.edit}
151
            </button>
152
          )}
153
          {link_delete && (
535 stevensc 154
            <button onClick={() => onDelete(link_delete)}>
5 stevensc 155
              <DeleteIcon />
156
              {labels.delete}
157
            </button>
158
          )}
1507 stevensc 159
        </WidgetWrapper.Actions>
1650 stevensc 160
 
655 stevensc 161
        <div className='px-1 pb-1'>
554 stevensc 162
          <StyledForm image={user_image} onSubmit={addComment} />
163
          {showComments && (
164
            <CommentsList comments={comments} onDelete={deleteComment} />
165
          )}
166
        </div>
1507 stevensc 167
      </WidgetWrapper>
5 stevensc 168
    </>
169
  )
170
}
171
 
172
export default AnswerCard