Proyectos de Subversion LeadersLinked - SPA

Rev

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

Rev Autor Línea Nro. Línea
3741 stevensc 1
import React, { useEffect, useRef, useState } from 'react';
2
import { useNavigate, useLocation } from 'react-router-dom';
3
import { useDispatch, useSelector } from 'react-redux';
3745 stevensc 4
import { QuestionCard } from '@my-coach/components';
3741 stevensc 5
 
6
export function MyCoachViewPage() {
7
  const [question, setQuestion] = useState({});
8
  const [answers, setAnswers] = useState([]);
9
  const [modalShow, setModalShow] = useState(null);
10
  const [currentAnswer, setCurrentAnswer] = useState('');
11
  const addUrl = useRef('');
12
  const actionUrl = useRef('');
13
  const labels = useSelector(({ intl }) => intl.labels);
14
  const dispatch = useDispatch();
15
  const { pathname } = useLocation();
16
  const navigate = useNavigate();
17
 
18
  const getQuestion = () => {
19
    axios
20
      .get(pathname, {
21
        headers: {
22
          'Content-Type': 'application/json'
23
        }
24
      })
25
      .then((response) => {
26
        const { data, success } = response.data;
27
 
28
        if (!success) {
29
          const errorMessage =
30
            typeof data === 'string'
31
              ? data
32
              : 'Error interno. Por favor, inténtelo de nuevo más tarde.';
33
 
34
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
35
          return;
36
        }
37
 
38
        addUrl.current = data.link_answers_add;
39
        setQuestion(data);
40
      })
41
      .catch((error) => {
42
        dispatch(addNotification({ style: 'danger', msg: error.message }));
43
      });
44
  };
45
 
46
  const confirmDelete = () => {
47
    axios
48
      .post(actionUrl.current)
49
      .then((response) => {
50
        const { data, success } = response.data;
51
 
52
        if (!success) {
53
          const errorMessage =
54
            typeof data === 'string' ? data : 'Ha ocurrido un error, por favor intente más tarde.';
55
 
56
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
57
          return;
58
        }
59
 
60
        closeModal();
61
        dispatch(addNotification({ style: 'success', msg: data }));
62
        navigate('/my-coach');
63
      })
64
      .catch((error) => {
65
        dispatch(addNotification({ style: 'danger', msg: error.message }));
66
      });
67
  };
68
 
69
  const confirmDeleteAnswer = () => {
70
    axios
71
      .post(actionUrl.current)
72
      .then((response) => {
73
        const { data, success } = response.data;
74
 
75
        if (!success) {
76
          const errorMessage =
77
            typeof data === 'string' ? data : 'Ha ocurrido un error, por favor intente más tarde.';
78
 
79
          dispatch(addNotification({ style: 'danger', msg: errorMessage }));
80
          return;
81
        }
82
 
83
        setQuestion((prevQuestion) => ({
84
          ...prevQuestion,
85
          comments: data.total_comments,
86
          answers: data.total_answers,
87
          reactions: data.total_reactions
88
        }));
89
        closeModal();
90
      })
91
      .catch((error) => {
92
        dispatch(addNotification({ style: 'danger', msg: error.message }));
93
      });
94
  };
95
 
96
  const deleteQuestion = (url) => {
97
    actionUrl.current = url;
98
    setModalShow('delete');
99
  };
100
 
101
  const editQuestion = (url) => {
102
    actionUrl.current = url;
103
    setModalShow('edit');
104
  };
105
 
106
  const closeModal = () => {
107
    actionUrl.current = '';
108
    setModalShow(null);
109
    setCurrentAnswer('');
110
  };
111
 
112
  const addAnswer = () => {
113
    setModalShow('addAnswer');
114
  };
115
 
116
  const editAnswer = (url, text) => {
117
    setModalShow('editAnswer');
118
    actionUrl.current = url;
119
    setCurrentAnswer(text);
120
  };
121
 
122
  const deleteAnswer = (url) => {
123
    setModalShow('deleteAnswer');
124
    actionUrl.current = url;
125
  };
126
 
127
  const onAddAnswer = ({ answers, item }) => {
128
    setQuestion((prevQuestion) => ({ ...prevQuestion, answers }));
129
    setAnswers((prevAnswers) => [item, ...prevAnswers]);
130
  };
131
 
132
  const onEditAnswer = ({ description }) => {
133
    const newAnswers = answers.map((answer) => {
134
      if (answer.link_edit === actionUrl.current) {
135
        return { ...answer, text: description };
136
      }
137
 
138
      return answer;
139
    });
140
 
141
    setAnswers(newAnswers);
142
  };
143
 
144
  const updateTotalComments = (total) => {
145
    setQuestion({
146
      ...question,
147
      comments: total
148
    });
149
  };
150
 
151
  const updateTotalReactions = (total) => {
152
    setQuestion({
153
      ...question,
154
      reactions: total
155
    });
156
  };
157
 
158
  useEffect(() => {
159
    getQuestion();
160
  }, []);
161
 
162
  useEffect(() => {
163
    if (question.link_answers) {
164
      axios
165
        .get(question.link_answers)
166
        .then((response) => {
167
          const { data, success } = response.data;
168
 
169
          if (!success) {
170
            const errorMessage =
171
              typeof data === 'string'
172
                ? data
173
                : 'Error interno. Por favor, inténtelo de nuevo más tarde.';
174
 
175
            dispatch(addNotification({ style: 'danger', msg: errorMessage }));
176
            return;
177
          }
178
 
179
          setAnswers(data.items);
180
        })
181
        .catch((error) => {
182
          dispatch(addNotification({ style: 'danger', msg: error.message }));
183
        });
184
    }
185
  }, [question]);
186
 
187
  return (
3745 stevensc 188
    <>
189
      <QuestionCard
190
        onEdit={editQuestion}
191
        onDelete={deleteQuestion}
192
        onReply={addAnswer}
193
        question={question}
194
      />
195
      {/* {answers.map((answer) => (
196
        <AnswerCard
197
          key={answer.unique}
198
          {...answer}
199
          onEdit={editAnswer}
200
          onDelete={deleteAnswer}
201
          updateComments={updateTotalComments}
202
          updateReactions={updateTotalReactions}
203
        />
204
      ))}
3741 stevensc 205
      <AnswerModal
206
        url={currentAnswer ? actionUrl.current : addUrl.current}
207
        show={['addAnswer', 'editAnswer'].includes(modalShow)}
208
        currentAnswer={currentAnswer}
209
        onClose={closeModal}
210
        onComplete={currentAnswer ? onEditAnswer : onAddAnswer}
211
      />
212
      <ConfirmModal
213
        show={modalShow === 'deleteAnswer'}
214
        onClose={closeModal}
215
        onAccept={confirmDeleteAnswer}
216
      />
3745 stevensc 217
      <ConfirmModal show={modalShow === 'delete'} onClose={closeModal} onAccept={confirmDelete} /> */}
218
    </>
3741 stevensc 219
  );
220
}