Passed
Push — feature/azure-webapp-pipeline-... ( 9e9b64...fdb227 )
by Grant
07:49 queued 11s
created

jobBuilderHooks.ts ➔ useLoadTasks   B

Complexity

Conditions 5

Size

Total Lines 27
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 20
dl 0
loc 27
rs 8.9332
c 0
b 0
f 0
cc 5
1
import { useEffect, useState } from "react";
2
import { useSelector } from "react-redux";
3
import { DispatchType } from "../configureStore";
4
import {
5
  Criteria,
6
  Department,
7
  Job,
8
  JobPosterKeyTask,
9
  Skill,
10
} from "../models/types";
11
import {
12
  getDepartments,
13
  departmentsIsLoading,
14
} from "../store/Department/deptSelector";
15
import { getDepartments as fetchDepartments } from "../store/Department/deptActions";
16
import {
17
  getCriteriaByJob,
18
  getCriteriaForJobIsUpdating,
19
  getJob,
20
  getJobIsUpdating,
21
  getTasksByJob,
22
  getTasksForJobIsUpdating,
23
} from "../store/Job/jobSelector";
24
import { RootState } from "../store/store";
25
import {
26
  fetchCriteria,
27
  fetchJob,
28
  fetchJobTasks,
29
  setSelectedJob,
30
} from "../store/Job/jobActions";
31
import { getSkills, getSkillsUpdating } from "../store/Skill/skillSelector";
32
import { fetchSkills } from "../store/Skill/skillActions";
33
34
export function useLoadJob(
35
  jobId: number | null,
36
  dispatch: DispatchType,
37
): { job: Job | null; isLoadingJob: boolean } {
38
  const job = useSelector((state: RootState) =>
39
    jobId !== null ? getJob(state, { jobId }) : null,
40
  );
41
  const isLoadingJob = useSelector((state: RootState) =>
42
    jobId !== null ? getJobIsUpdating(state, jobId) : false,
43
  );
44
  useEffect(() => {
45
    if (jobId !== null && job === null && !isLoadingJob) {
46
      dispatch(fetchJob(jobId));
47
    }
48
  }, [jobId, job, isLoadingJob, dispatch]);
49
  useEffect(() => {
50
    dispatch(setSelectedJob(jobId));
51
  }, [jobId, dispatch]);
52
  return { job, isLoadingJob };
53
}
54
55
export function useLoadTasks(
56
  jobId: number | null,
57
  dispatch: DispatchType,
58
): { tasks: JobPosterKeyTask[]; isLoadingTasks: boolean } {
59
  const tasks = useSelector((state: RootState) =>
60
    jobId !== null ? getTasksByJob(state, { jobId }) : [],
61
  );
62
  const isLoadingTasks = useSelector((state: RootState) =>
63
    jobId !== null ? getTasksForJobIsUpdating(state, jobId) : false,
64
  );
65
  const [hasFetchedTasks, setHasFetchedTasks] = useState(false);
66
  useEffect((): (() => void) => {
67
    let isSubscribed = true;
68
    if (jobId && tasks.length === 0 && !isLoadingTasks && !hasFetchedTasks) {
69
      setHasFetchedTasks(true);
70
      dispatch(fetchJobTasks(jobId)).catch((): void => {
71
        if (isSubscribed) {
72
          setHasFetchedTasks(false);
73
        }
74
      });
75
    }
76
    return (): void => {
77
      isSubscribed = false;
78
    };
79
  }, [jobId, tasks.length, isLoadingTasks, hasFetchedTasks, dispatch]);
80
  return { tasks, isLoadingTasks };
81
}
82
83
export function useLoadCriteria(
84
  jobId: number | null,
85
  dispatch: DispatchType,
86
): {
87
  criteria: Criteria[];
88
  isLoadingCriteria: boolean;
89
} {
90
  const criteria = useSelector((state: RootState) =>
91
    jobId ? getCriteriaByJob(state, { jobId }) : [],
92
  );
93
  const isLoadingCriteria = useSelector((state: RootState) =>
94
    jobId ? getCriteriaForJobIsUpdating(state, jobId) : false,
95
  );
96
  const [hasFetchedCriteria, setHasFetchedCriteria] = useState(false);
97
  useEffect((): (() => void) => {
98
    let isSubscribed = true;
99
    if (
100
      jobId &&
101
      criteria.length === 0 &&
102
      !isLoadingCriteria &&
103
      !hasFetchedCriteria
104
    ) {
105
      setHasFetchedCriteria(true);
106
      dispatch(fetchCriteria(jobId)).catch((): void => {
107
        if (isSubscribed) {
108
          setHasFetchedCriteria(false);
109
        }
110
      });
111
    }
112
    return (): void => {
113
      isSubscribed = false;
114
    };
115
  }, [jobId, criteria.length, isLoadingCriteria, hasFetchedCriteria, dispatch]);
116
  return { criteria, isLoadingCriteria };
117
}
118
119
export function useLoadDepartments(
120
  dispatch: DispatchType,
121
): {
122
  departments: Department[];
123
  isLoadingDepartments: boolean;
124
} {
125
  const departments = useSelector(getDepartments);
126
  const isLoading = useSelector(departmentsIsLoading);
127
  useEffect((): void => {
128
    if (departments.length === 0 && !isLoading) {
129
      dispatch(fetchDepartments());
130
    }
131
  }, [departments.length, isLoading, dispatch]);
132
  return { departments, isLoadingDepartments: isLoading };
133
}
134
135
export function useLoadSkills(
136
  dispatch: DispatchType,
137
): {
138
  skills: Skill[];
139
  isLoadingSkills: boolean;
140
} {
141
  const skills = useSelector(getSkills);
142
  const isLoading = useSelector(getSkillsUpdating);
143
  useEffect((): void => {
144
    if (skills.length === 0 && !isLoading) {
145
      dispatch(fetchSkills());
146
    }
147
  }, [skills.length, isLoading, dispatch]);
148
  return { skills, isLoadingSkills: isLoading };
149
}
150