Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3694 | | Comparar con el anterior | Ultima modificación | Ver Log |

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