Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 29 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | import { useEffect } from "react"; |
||
2 | import { useSelector } from "react-redux"; |
||
3 | import { DispatchType } from "../configureStore"; |
||
4 | import { Classification } from "../models/types"; |
||
5 | import { |
||
6 | getClassifications, |
||
7 | classificationsIsLoading, |
||
8 | } from "../store/Classification/classificationSelector"; |
||
9 | import { loadClassificationsIntoState } from "../store/Classification/classificationActions"; |
||
10 | |||
11 | // eslint-disable-next-line import/prefer-default-export |
||
12 | export function useLoadClassifications( |
||
13 | dispatch: DispatchType, |
||
14 | ): { |
||
15 | classifications: Classification[]; |
||
16 | isLoadingClassifications: boolean; |
||
17 | } { |
||
18 | const classifications = useSelector(getClassifications); |
||
19 | const isLoading = useSelector(classificationsIsLoading); |
||
20 | |||
21 | useEffect((): void => { |
||
22 | if (classifications.length === 0 && !isLoading) { |
||
23 | dispatch(loadClassificationsIntoState()); |
||
24 | } |
||
25 | }, [classifications.length, isLoading, dispatch]); |
||
26 | |||
27 | return { classifications, isLoadingClassifications: isLoading }; |
||
28 | } |
||
29 |