-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuseFetchSolutionList.js
35 lines (31 loc) · 1.04 KB
/
useFetchSolutionList.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import useSetSolutionList from "./useSetSolutionList";
import { requestSolutionListByLevelAPI } from "../../apis/solutionList";
const useFetchSolutionList = () => {
const setSolutionList = useSetSolutionList();
const POSSIBLE_LEVEL = [1, 2, 3, 4, 5];
const fetchSolutionList = async () => {
setSolutionList([]);
POSSIBLE_LEVEL.forEach((level) => {
requestSolutionListByLevelAPI(level).then((response) => {
const newSolutionList = response
.filter((solution) => solution.name !== "00-해답-예시.js")
.map((solution) => ({
name: formattedFileName(solution.name),
level,
}));
setSolutionList((solutionList) => [
...solutionList,
...newSolutionList,
]);
});
});
setSolutionList((solutionList) =>
solutionList.sort((a, b) => a.level - b.level)
);
};
return fetchSolutionList;
};
export default useFetchSolutionList;
function formattedFileName(fileName) {
return fileName.replace(/-/g, " ").replace(".js", "");
}