Proyectos de Subversion LeadersLinked - SPA

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
3746 stevensc 1
import { useState } from 'react';
2
 
3
import { useAlert, useApi } from '@shared/hooks';
4
import { deleteQuestion, getQuestion } from '@my-coach/services';
5
 
6
export function useQuestion(uuid, { onDeleteQuestion }) {
7
  const [question, setQuestion] = useState(null);
8
 
9
  const { loading } = useApi(getQuestion, {
10
    autoFetch: true,
11
    autoFetchArgs: [uuid],
12
    onSuccess: (data) => {
13
      setQuestion(data);
14
    },
15
    onError: (error) => {
16
      showError(error.message);
17
    }
18
  });
19
 
20
  const { showSuccess, showError } = useAlert();
21
 
22
  const { execute: executeDeleteQuestion } = useApi(deleteQuestion);
23
 
24
  const updateTotalAnswers = (answers) => {
25
    setQuestion((prev) => ({ ...prev, answers }));
26
  };
27
 
28
  const updateTotalComments = (comments) => {
29
    setQuestion((prev) => ({ ...prev, comments }));
30
  };
31
 
32
  const updateTotalReactions = (reactions) => {
33
    setQuestion((prev) => ({ ...prev, reactions }));
34
  };
35
 
36
  const removeQuestion = async (url) => {
37
    await executeDeleteQuestion(url)
38
      .then(({ message }) => {
39
        showSuccess(message);
40
        setQuestion(null);
41
        onDeleteQuestion();
42
      })
43
      .catch((error) => {
44
        showError(error.message);
45
      });
46
  };
47
 
48
  return {
49
    question,
50
    loading,
51
    updateTotalAnswers,
52
    updateTotalComments,
53
    updateTotalReactions,
54
    deleteQuestion: removeQuestion
55
  };
56
}