-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
useCompanyData 훅 생성 기업Id를 1부터 14까지 모두 삽입 세로 스크롤 구현 현재 이름이 관심목록인 것을 전체목록으로 정정 필요 Issues #19
- Loading branch information
김병현
authored and
김병현
committed
Sep 15, 2023
1 parent
256fad8
commit 914b662
Showing
3 changed files
with
114 additions
and
53 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { useQuery } from 'react-query'; | ||
import axios from 'axios'; | ||
|
||
const BASE_URL = 'http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com:8080'; | ||
|
||
// 데이터 타입 정의 | ||
type CompanyData = { | ||
companyId: number; | ||
code: string; | ||
korName: string; | ||
stockInfResponseDto: { | ||
stck_prpr: string; | ||
prdy_vrss: string; | ||
prdy_ctrt: string; | ||
}; | ||
}; | ||
|
||
// 커스텀 훅 정의 | ||
function useCompanyData(startCompanyId: number, endCompanyId: number) { | ||
const fetchData = async (companyId: number) => { | ||
const url = `${BASE_URL}/companies/${companyId}`; | ||
const response = await axios.get<CompanyData>(url); | ||
return response.data; | ||
}; | ||
|
||
// companyId 범위에 대한 배열 생성 | ||
const companyIds = Array.from({ length: endCompanyId - startCompanyId + 1 }, (_, index) => startCompanyId + index); | ||
|
||
// 리액트-쿼리의 useQuery 훅 사용 | ||
const { data, isLoading, isError } = useQuery<CompanyData[]>( | ||
['companyData', startCompanyId, endCompanyId], | ||
async () => { | ||
const promises = companyIds.map((companyId) => fetchData(companyId)); | ||
return Promise.all(promises); | ||
} | ||
); | ||
|
||
// 필요한 데이터 추출 및 저장 | ||
const extractedData = data?.map((company) => { | ||
return { | ||
companyId: company.companyId, | ||
code: company.code, | ||
korName: company.korName, | ||
stockPrice: company.stockInfResponseDto.stck_prpr, | ||
stockChangeAmount: company.stockInfResponseDto.prdy_vrss, | ||
stockChangeRate: company.stockInfResponseDto.prdy_ctrt, | ||
}; | ||
}); | ||
|
||
return { | ||
data: extractedData, | ||
isLoading, | ||
isError, | ||
}; | ||
} | ||
|
||
export default useCompanyData; |