-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from PreOnBoarding-Team17/feature/logic
[황상섭] feat: 커스텀훅 생성
- Loading branch information
Showing
8 changed files
with
106 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
import axios, { AxiosInstance } from 'axios'; | ||
import { DataInterface } from 'Utils/Interface'; | ||
import axios from 'axios'; | ||
|
||
const BASE_URL = 'http://localhost:4000/requests'; | ||
|
||
export const callAPI: AxiosInstance = axios.create({ | ||
baseURL: `${BASE_URL}`, | ||
}); | ||
|
||
export const getAPI = async () => { | ||
const response = await axios.get<DataInterface>(BASE_URL); | ||
return response.data; | ||
const response = await axios.get(BASE_URL); | ||
const data = await response.data; | ||
return data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { useState, useEffect } from 'react'; | ||
import { getAPI } from 'API'; | ||
import { DataInterface } from 'Utils/Interface'; | ||
|
||
export default function useApi() { | ||
const [datas, setDatas] = useState<DataInterface[]>([]); | ||
const [cardDatas, setCardDatas] = useState<DataInterface[]>([]); | ||
|
||
useEffect(() => { | ||
const fetchAPI = async () => { | ||
try { | ||
const data = await getAPI(); | ||
setDatas(data); | ||
setCardDatas(data); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
}; | ||
fetchAPI(); | ||
}, []); | ||
|
||
return { datas, cardDatas, setCardDatas }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { useState, useEffect } from 'react'; | ||
import useApi from './useApi'; | ||
|
||
export default function useFilter() { | ||
const [toggle, setToggle] = useState<boolean>(false); | ||
const [method, setMethod] = useState<string[]>([]); | ||
const [material, setMaterial] = useState<string[]>([]); | ||
|
||
const { datas, setCardDatas } = useApi(); | ||
|
||
const handleToggle = () => { | ||
setToggle(!toggle); | ||
}; | ||
|
||
const handleReset = () => { | ||
setMethod([]); | ||
setMaterial([]); | ||
if (toggle) setToggle(false); | ||
}; | ||
|
||
useEffect(() => { | ||
const filterToggle = toggle | ||
? datas.filter((element) => element.status === '상담중') | ||
: datas; | ||
|
||
const filterMethod = | ||
method.length > 0 | ||
? filterToggle.filter((element) => { | ||
return method.every((selectedMethod) => | ||
element.method.find((method) => method === selectedMethod) | ||
); | ||
}) | ||
: filterToggle; | ||
|
||
const filterMaterial = | ||
material.length > 0 | ||
? filterMethod.filter((element) => { | ||
return material.every((selectedMaterial) => | ||
element.material.find((material) => material === selectedMaterial) | ||
); | ||
}) | ||
: filterMethod; | ||
|
||
setCardDatas(filterMaterial); | ||
}, [toggle, method, material]); | ||
|
||
return { | ||
toggle, | ||
handleToggle, | ||
handleReset, | ||
method, | ||
material, | ||
setMethod, | ||
setMaterial, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters