Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3411 | Ir a la última revisión | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3411 Rev 3694
Línea 1... Línea 1...
1
import React, { useState } from 'react'
1
import React, { useState } from 'react';
2
import { useDispatch, useSelector } from 'react-redux'
2
import { useDispatch, useSelector } from 'react-redux';
3
import { Box, Typography } from '@mui/material'
3
import { Box, Typography } from '@mui/material';
4
import { ChatOutlined } from '@mui/icons-material'
4
import ChatOutlined from '@mui/icons-material/ChatOutlined';
5
 
5
 
6
import { axios } from '@utils'
6
import { axios } from '@utils';
7
import { withReactions } from '@hocs'
7
// import { withReactions } from '@hocs';
8
import { addNotification } from '@store/notification/notification.actions'
8
import { addNotification } from '@store/notification/notification.actions';
9
 
9
 
10
import Widget from '@components/UI/Widget'
10
import Widget from '@components/UI/Widget';
11
import Button from '@components/UI/buttons/Buttons'
11
import Button from '@components/UI/buttons/Buttons';
12
import CommentForm from '@components/dashboard/linkedin/comments/comment-form'
12
import CommentForm from '@components/dashboard/linkedin/comments/comment-form';
13
import CommentsList from '@components/dashboard/linkedin/comments/comment-list'
13
import CommentsList from '@components/dashboard/linkedin/comments/comment-list';
14
import FeedDescription from '@components/dashboard/feed/feed-description'
14
import FeedDescription from '@components/dashboard/feed/feed-description';
15
import Options from '@components/UI/Option'
15
import Options from '@components/UI/Option';
Línea 16... Línea 16...
16
 
16
 
17
const AnswerCard = ({
17
const AnswerCard = ({
18
  time_elapsed = '',
18
  time_elapsed = '',
19
  user_image = '',
19
  user_image = '',
20
  user_url = '',
20
  // user_url = '',
21
  user_name = '',
21
  user_name = '',
22
  text = '',
22
  text = '',
23
  reaction = '',
23
  // reaction = '',
24
  total_comments = 0,
24
  total_comments = 0,
25
  total_reactions = 0,
25
  total_reactions = 0,
26
  comments: defaultComments = [],
26
  comments: defaultComments = [],
27
  link_edit = '',
27
  link_edit = '',
28
  link_delete = '',
28
  link_delete = '',
29
  link_reaction_delete = '',
29
  // link_reaction_delete = '',
30
  link_save_reaction = '',
30
  // link_save_reaction = '',
31
  link_add_comment = '',
31
  link_add_comment = '',
32
  onEdit = () => {},
32
  onEdit = () => {},
33
  onDelete = () => {},
33
  onDelete = () => {},
34
  updateComments = () => {},
34
  updateComments = () => {}
35
  updateReactions = () => {}
35
  // updateReactions = () => {}
36
}) => {
36
}) => {
37
  const [comments, setComments] = useState(defaultComments)
37
  const [comments, setComments] = useState(defaultComments);
38
  const [totalComments, setTotalComments] = useState(total_comments)
38
  const [totalComments, setTotalComments] = useState(total_comments);
39
  const [totalReactions, setTotalReactions] = useState(total_reactions)
39
  const [totalReactions, setTotalReactions] = useState(total_reactions);
40
  const [showComments, setShowComments] = useState(false)
40
  const [showComments, setShowComments] = useState(false);
41
  const labels = useSelector(({ intl }) => intl.labels)
41
  const labels = useSelector(({ intl }) => intl.labels);
Línea 42... Línea 42...
42
  const dispatch = useDispatch()
42
  const dispatch = useDispatch();
43
 
43
 
44
  const addComment = (comment) => {
44
  const addComment = (comment) => {
Línea 45... Línea 45...
45
    const formData = new FormData()
45
    const formData = new FormData();
46
    formData.append('comment', comment)
46
    formData.append('comment', comment);
47
 
47
 
48
    axios
48
    axios
Línea 49... Línea 49...
49
      .post(link_add_comment, formData)
49
      .post(link_add_comment, formData)
50
      .then((response) => {
50
      .then((response) => {
51
        const { success, data } = response.data
-
 
52
 
-
 
53
        if (!success) {
51
        const { success, data } = response.data;
54
          const errorMessage =
52
 
55
            typeof data === 'string'
53
        if (!success) {
56
              ? data
54
          const errorMessage =
Línea 57... Línea 55...
57
              : 'Error interno. Intente más tarde.'
55
            typeof data === 'string' ? data : 'Error interno. Intente más tarde.';
58
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
56
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
59
          return
57
          return;
60
        }
58
        }
61
 
59
 
62
        setComments([...comments, data.item])
60
        setComments([...comments, data.item]);
63
        updateComments(data.total_comments_question)
61
        updateComments(data.total_comments_question);
64
        setTotalComments(data.total_comments_answer)
62
        setTotalComments(data.total_comments_answer);
Línea 65... Línea 63...
65
      })
63
      })
66
      .catch((err) => {
64
      .catch((err) => {
67
        dispatch(addNotification({ style: 'danger', msg: err.message }))
65
        dispatch(addNotification({ style: 'danger', msg: err.message }));
68
      })
66
      });
69
  }
67
  };
Línea 70... Línea 68...
70
 
68
 
71
  const deleteComment = (commentUnique, deleteCommentUrl) => {
69
  const deleteComment = (commentUnique, deleteCommentUrl) => {
72
    axios
-
 
73
      .post(deleteCommentUrl)
-
 
74
      .then((response) => {
70
    axios
Línea 75... Línea 71...
75
        const { success, data } = response.data
71
      .post(deleteCommentUrl)
76
 
72
      .then((response) => {
77
        if (!success) {
73
        const { success, data } = response.data;
Línea 78... Línea -...
78
          const errorMessage =
-
 
79
            typeof data === 'string'
74
 
80
              ? data
-
 
81
              : 'Error interno. Intente más tarde.'
75
        if (!success) {
82
 
76
          const errorMessage =
83
          dispatch(addNotification({ style: 'danger', msg: errorMessage }))
77
            typeof data === 'string' ? data : 'Error interno. Intente más tarde.';
84
          return
78
 
85
        }
79
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
86
 
80
          return;
87
        const newComments = comments.filter(
81
        }
88
          ({ unique }) => unique !== commentUnique
82
 
89
        )
83
        const newComments = comments.filter(({ unique }) => unique !== commentUnique);
90
 
84
 
91
        dispatch(addNotification({ style: 'success', msg: data.message }))
85
        dispatch(addNotification({ style: 'success', msg: data.message }));
Línea 92... Línea 86...
92
 
86
 
Línea 93... Línea 87...
93
        setComments(newComments)
87
        setComments(newComments);
94
        updateComments(data.total_comments_question)
88
        updateComments(data.total_comments_question);
95
        setTotalComments(data.total_comments_answer)
89
        setTotalComments(data.total_comments_answer);
96
      })
90
      })
Línea 109... Línea 103...
109
          title={user_name}
103
          title={user_name}
110
          subheader={time_elapsed}
104
          subheader={time_elapsed}
111
          renderAction={() => (
105
          renderAction={() => (
112
            <Options>
106
            <Options>
113
              {link_delete && (
107
              {link_delete && (
114
                <Options.Item onClick={() => onDelete(link_delete)}>
108
                <Options.Item onClick={() => onDelete(link_delete)}>Borrar</Options.Item>
115
                  Borrar
-
 
116
                </Options.Item>
-
 
117
              )}
109
              )}
118
              {link_edit && (
110
              {link_edit && (
119
                <Options.Item onClick={() => onEdit(link_edit, text)}>
111
                <Options.Item onClick={() => onEdit(link_edit, text)}>{labels.edit}</Options.Item>
120
                  {labels.edit}
-
 
121
                </Options.Item>
-
 
122
              )}
112
              )}
123
            </Options>
113
            </Options>
124
          )}
114
          )}
125
        />
115
        />
Línea 160... Línea 150...
160
          <CommentForm onSubmit={addComment} />
150
          <CommentForm onSubmit={addComment} />
161
          <CommentsList comments={comments} onDelete={deleteComment} />
151
          <CommentsList comments={comments} onDelete={deleteComment} />
162
        </Box>
152
        </Box>
163
      </Widget>
153
      </Widget>
164
    </>
154
    </>
165
  )
155
  );
166
}
156
};
Línea 167... Línea 157...
167
 
157