Proyectos de Subversion LeadersLinked - SPA

Rev

Rev 3741 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 3741 Rev 3746
Línea 1... Línea 1...
1
import { useEffect, useState } from 'react';
1
import { useEffect, useState } from 'react';
Línea 2... Línea 2...
2
 
2
 
3
import { useApi, useDebouncedSearchParam, usePagination } from '@shared/hooks';
3
import { useAlert, useApi, useDebouncedSearchParam, usePagination } from '@shared/hooks';
Línea 4... Línea 4...
4
import { addQuestion, editQuestion, deleteQuestion } from '@my-coach/services';
4
import { addQuestion, editQuestion, deleteQuestion } from '@my-coach/services';
5
 
5
 
Línea 6... Línea 6...
6
export function useQuestions() {
6
export function useQuestions() {
Línea -... Línea 7...
-
 
7
  const [questions, setQuestions] = useState([]);
-
 
8
 
7
  const [questions, setQuestions] = useState([]);
9
  const { data, loading, hasMore, loadMore, applyFilters } = usePagination('/my-coach/questions');
-
 
10
 
-
 
11
  const { showSuccess, showError } = useAlert();
-
 
12
 
-
 
13
  const { execute: executeAddQuestion } = useApi(addQuestion, {
-
 
14
    onSuccess: ({ question, message }) => {
-
 
15
      setQuestions((prev) => [...prev, question]);
-
 
16
      showSuccess(message);
-
 
17
    },
8
 
18
    onError: ({ message }) => {
-
 
19
      showError(message);
-
 
20
    }
-
 
21
  });
-
 
22
  const { execute: executeEditQuestion } = useApi(editQuestion, {
-
 
23
    onSuccess: ({ question, message }) => {
-
 
24
      setQuestions((prev) => prev.map((q) => (q.uuid === question.uuid ? question : q)));
-
 
25
      showSuccess(message);
-
 
26
    },
9
  const { data, loading, hasMore, loadMore, applyFilters } = usePagination('/my-coach/questions');
27
    onError: ({ message }) => {
-
 
28
      showError(message);
-
 
29
    }
-
 
30
  });
-
 
31
  const { execute: executeDeleteQuestion } = useApi(deleteQuestion, {
-
 
32
    onSuccess: ({ question, message }) => {
-
 
33
      setQuestions((prev) => prev.filter((q) => q.uuid !== question.uuid));
-
 
34
      showSuccess(message);
-
 
35
    },
Línea 10... Línea 36...
10
 
36
    onError: ({ message }) => {
11
  const { data: addedQuestion, execute: executeAddQuestion } = useApi(addQuestion);
37
      showError(message);
12
  const { data: updatedQuestion, execute: executeEditQuestion } = useApi(editQuestion);
38
    }
13
  const { data: deletedQuestion, execute: executeDeleteQuestion } = useApi(deleteQuestion);
39
  });
Línea 40... Línea 66...
40
    if (data) {
66
    if (data) {
41
      setQuestions(data);
67
      setQuestions(data);
42
    }
68
    }
43
  }, [data]);
69
  }, [data]);
Línea 44... Línea -...
44
 
-
 
45
  useEffect(() => {
-
 
46
    if (addedQuestion) {
-
 
47
      setQuestions([...questions, addedQuestion]);
-
 
48
    }
-
 
49
  }, [addedQuestion]);
-
 
50
 
-
 
51
  useEffect(() => {
-
 
52
    if (updatedQuestion) {
-
 
53
      setQuestions(
-
 
54
        questions.map((question) =>
-
 
55
          question.id === updatedQuestion.id ? updatedQuestion : question
-
 
56
        )
-
 
57
      );
-
 
58
    }
-
 
59
  }, [updatedQuestion]);
-
 
60
 
-
 
61
  useEffect(() => {
-
 
62
    if (deletedQuestion) {
-
 
63
      setQuestions(questions.filter((question) => question.id !== deletedQuestion.id));
-
 
64
    }
-
 
65
  }, [deletedQuestion]);
-
 
66
 
70
 
67
  return {
71
  return {
68
    questions,
72
    questions,
69
    loading,
73
    loading,
70
    hasMore,
74
    hasMore,