Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3411 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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