Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3741 stevensc 1
import React, { useState } from 'react';
3746 stevensc 2
import { useSelector } from 'react-redux';
3741 stevensc 3
import { Box, Typography } from '@mui/material';
3746 stevensc 4
import { ChatOutlined } from '@mui/icons-material';
3741 stevensc 5
 
3746 stevensc 6
import { useAnswerComments } from '@my-coach/hooks';
7
import { parse } from '@shared/utils';
3741 stevensc 8
 
3746 stevensc 9
import {
10
  Card,
11
  CardActions,
12
  CardContent,
13
  CardHeader,
14
  CommentForm,
15
  Menu,
16
  Button,
17
  CommentList
18
} from '@shared/components';
19
import { useAlertModal } from '@shared/hooks';
3741 stevensc 20
 
21
export function AnswerCard({
3746 stevensc 22
  answer: {
23
    uuid = '',
24
    time_elapsed = '',
25
    user_image = '',
26
    user_name = '',
27
    text = '',
28
    total_comments = 0,
29
    total_reactions = 0,
30
    comments = [],
31
    link_add_comment = ''
32
  },
33
  onEdit,
34
  onDelete,
35
  onUpdateComments
3741 stevensc 36
}) {
37
  const [showComments, setShowComments] = useState(false);
38
  const labels = useSelector(({ intl }) => intl.labels);
39
 
3746 stevensc 40
  const { showAlert, closeAlert } = useAlertModal();
41
 
42
  const {
43
    comments: answerComments,
44
    addComment,
45
    deleteComment
46
  } = useAnswerComments(comments, {
47
    onAddComment: (data) => {
48
      onUpdateComments(uuid, data);
49
    },
50
    onDeleteComment: (data) => {
51
      onUpdateComments(uuid, data);
52
      closeAlert();
53
    }
54
  });
55
 
56
  const handleAddComment = (comment) => {
57
    addComment(link_add_comment, comment);
58
  };
59
 
60
  const handleDeleteComment = (url) => {
61
    showAlert({
62
      title: 'Eliminar comentario',
63
      message: '¿Estás seguro de querer eliminar este comentario?',
64
      onConfirm: () => deleteComment(url)
65
    });
66
  };
67
 
68
  /* const addComment = (comment) => {
3741 stevensc 69
    const formData = new FormData();
70
    formData.append('comment', comment);
71
 
72
    axios
73
      .post(link_add_comment, formData)
74
      .then((response) => {
75
        const { success, data } = response.data;
76
 
77
        if (!success) {
78
          const errorMessage =
79
            typeof data === 'string' ? data : 'Error interno. Intente más tarde.';
80
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
81
          return;
82
        }
83
 
84
        setComments([...comments, data.item]);
85
        updateComments(data.total_comments_question);
86
        setTotalComments(data.total_comments_answer);
87
      })
88
      .catch((err) => {
89
        dispatch(addNotification({ style: 'danger', msg: err.message }));
90
      });
91
  };
92
 
93
  const deleteComment = (commentUnique, deleteCommentUrl) => {
94
    axios
95
      .post(deleteCommentUrl)
96
      .then((response) => {
97
        const { success, data } = response.data;
98
 
99
        if (!success) {
100
          const errorMessage =
101
            typeof data === 'string' ? data : 'Error interno. Intente más tarde.';
102
 
103
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
104
          return;
105
        }
106
 
107
        const newComments = comments.filter(({ unique }) => unique !== commentUnique);
108
 
109
        dispatch(addNotification({ style: 'success', msg: data.message }));
110
 
111
        setComments(newComments);
112
        updateComments(data.total_comments_question);
113
        setTotalComments(data.total_comments_answer);
114
      })
115
      .catch((err) => {
116
        dispatch(addNotification({ style: 'danger', msg: err.message }));
117
      });
3746 stevensc 118
  }; */
3741 stevensc 119
 
120
  // const ReactionsButton = withReactions(Button);
121
 
122
  return (
3746 stevensc 123
    <Card>
124
      <CardHeader
125
        avatar={user_image}
126
        title={user_name}
127
        subheader={time_elapsed}
128
        renderAction={() => (
129
          <Menu>
130
            {onDelete && <Menu.Item onClick={onDelete}>Borrar</Menu.Item>}
131
            {onEdit && <Menu.Item onClick={onEdit}>{labels.edit}</Menu.Item>}
132
          </Menu>
133
        )}
134
      />
3741 stevensc 135
 
3746 stevensc 136
      <CardContent>
137
        <Typography variant='body1'>{parse(text)}</Typography>
3741 stevensc 138
 
3746 stevensc 139
        <Box sx={{ display: 'flex', justifyContent: 'space-between' }}>
140
          <Typography variant='overline'>{`${total_reactions} ${labels.reactions}`}</Typography>
141
          <Typography variant='overline'>{`${total_comments} ${labels.comments}`}</Typography>
142
        </Box>
143
      </CardContent>
3741 stevensc 144
 
3746 stevensc 145
      <CardActions>
146
        {/* {link_save_reaction && (
3741 stevensc 147
            <ReactionsButton
148
              currentReactionType={reaction}
149
              saveUrl={link_save_reaction}
150
              deleteUrl={link_reaction_delete}
151
              onReaction={({
152
                total_reactions_answer,
153
                total_reactions_question
154
              }) => {
155
                setTotalReactions(total_reactions_answer)
156
                updateReactions(total_reactions_question)
157
              }}
158
            />
159
          )} */}
3746 stevensc 160
        {link_add_comment && (
161
          <Button onClick={() => setShowComments(!showComments)}>
162
            <ChatOutlined />
163
            {labels.comment}
164
          </Button>
165
        )}
166
      </CardActions>
3741 stevensc 167
 
3746 stevensc 168
      <Box
169
        sx={{
170
          padding: 1,
171
          display: showComments ? 'flex' : 'none',
172
          flexDirection: 'column',
173
          gap: 1,
174
          width: '100%'
175
        }}
176
      >
177
        <CommentForm onSubmit={handleAddComment} />
178
        <CommentList comments={answerComments} onDelete={handleDeleteComment} />
179
      </Box>
180
    </Card>
3741 stevensc 181
  );
182
}