| 3741 |
stevensc |
1 |
import { useEffect, useState } from 'react';
|
|
|
2 |
|
| 3746 |
stevensc |
3 |
import { useAlert, useApi, useDebouncedSearchParam, usePagination } from '@shared/hooks';
|
| 3741 |
stevensc |
4 |
import { addQuestion, editQuestion, deleteQuestion } from '@my-coach/services';
|
|
|
5 |
|
|
|
6 |
export function useQuestions() {
|
|
|
7 |
const [questions, setQuestions] = useState([]);
|
|
|
8 |
|
|
|
9 |
const { data, loading, hasMore, loadMore, applyFilters } = usePagination('/my-coach/questions');
|
|
|
10 |
|
| 3746 |
stevensc |
11 |
const { showSuccess, showError } = useAlert();
|
| 3741 |
stevensc |
12 |
|
| 3746 |
stevensc |
13 |
const { execute: executeAddQuestion } = useApi(addQuestion, {
|
|
|
14 |
onSuccess: ({ question, message }) => {
|
|
|
15 |
setQuestions((prev) => [...prev, question]);
|
|
|
16 |
showSuccess(message);
|
|
|
17 |
},
|
|
|
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 |
},
|
|
|
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 |
},
|
|
|
36 |
onError: ({ message }) => {
|
|
|
37 |
showError(message);
|
|
|
38 |
}
|
|
|
39 |
});
|
|
|
40 |
|
| 3741 |
stevensc |
41 |
const {
|
|
|
42 |
inputValue: searchTerm,
|
|
|
43 |
setInputValue: setSearchTerm,
|
|
|
44 |
debouncedValue: debouncedSearchTerm
|
|
|
45 |
} = useDebouncedSearchParam('search');
|
|
|
46 |
|
|
|
47 |
const {
|
|
|
48 |
inputValue: searchCategory,
|
|
|
49 |
setInputValue: setSearchCategory,
|
|
|
50 |
debouncedValue: debouncedSearchCategory
|
|
|
51 |
} = useDebouncedSearchParam('category_id');
|
|
|
52 |
|
|
|
53 |
const clearFilters = () => {
|
|
|
54 |
setSearchTerm('');
|
|
|
55 |
setSearchCategory('');
|
|
|
56 |
};
|
|
|
57 |
|
|
|
58 |
useEffect(() => {
|
|
|
59 |
applyFilters({
|
|
|
60 |
search: debouncedSearchTerm,
|
|
|
61 |
category_id: debouncedSearchCategory
|
|
|
62 |
});
|
|
|
63 |
}, [debouncedSearchTerm, debouncedSearchCategory]);
|
|
|
64 |
|
|
|
65 |
useEffect(() => {
|
|
|
66 |
if (data) {
|
|
|
67 |
setQuestions(data);
|
|
|
68 |
}
|
|
|
69 |
}, [data]);
|
|
|
70 |
|
|
|
71 |
return {
|
|
|
72 |
questions,
|
|
|
73 |
loading,
|
|
|
74 |
hasMore,
|
|
|
75 |
search: searchTerm,
|
|
|
76 |
category: searchCategory,
|
|
|
77 |
loadMore,
|
|
|
78 |
applyFilters,
|
|
|
79 |
searchQuestion: setSearchTerm,
|
|
|
80 |
searchCategory: setSearchCategory,
|
|
|
81 |
clearFilters,
|
|
|
82 |
addQuestion: executeAddQuestion,
|
|
|
83 |
editQuestion: executeEditQuestion,
|
|
|
84 |
deleteQuestion: executeDeleteQuestion
|
|
|
85 |
};
|
|
|
86 |
}
|