-
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.
[Feat] 차트관련 주식 종목 검색 및 선택 기능 구현 및 테스트 완료
- 중앙 차트 관련하여 종목 검색 및 선택 기능 구현 및 테스트 완료 - 테스트 위하여 차트관련 컴포넌트에 임시로 테스트 요소 (input, button 엘리먼트) 만들어놓은 상황으로 이후 삭제할 예정 - 기능 구현 위한 코드들 실제적으로 필요한 컴포넌트로 이동시킨 후 삭제할 예정 Issues #14
- Loading branch information
1 parent
cdef1f6
commit e5b4a5e
Showing
7 changed files
with
320 additions
and
32 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
import { useDispatch } from "react-redux"; | ||
import { styled } from "styled-components"; | ||
import EChartsReact from "echarts-for-react"; | ||
import { changeCompanyId } from "../../reducer/CompanyId-Reducer"; | ||
|
||
import useGetKospiChart from "../../hooks/useGetKospiChart"; | ||
|
||
// 🔴 회사 목록 데이터 불러오는 로직 | ||
import useGetCompanyList from "../../hooks/useGetCompanyList"; | ||
|
||
const loadingText = "로딩 중 입니다..."; | ||
const errorText = "화면을 불러올 수 없습니다"; | ||
|
||
//🔴 테스트 | ||
import { useEffect, useState } from "react"; | ||
|
||
const KospiChart = () => { | ||
const dispatch = useDispatch(); | ||
|
||
const { isLoading, error, options, chartStyle } = useGetKospiChart(); | ||
|
||
// 🔴 차트 변환 테스트 | ||
|
||
// 🔴 1) 검색 이벤트 | ||
const { companyList } = useGetCompanyList(); | ||
const [companyLists, setCompanyLists] = useState([]); | ||
const [searchWord, setSearchWord] = useState(""); | ||
|
||
// 회사 목록 불러오면 -> companyList 상태에 할당 | ||
useEffect(() => { | ||
setCompanyLists(companyList); | ||
}, [companyList]); | ||
|
||
useEffect(() => { | ||
console.log(companyLists); | ||
}, [companyLists]); | ||
|
||
const handleChangeSearchWord = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
setSearchWord(e.target.value); | ||
}; | ||
|
||
const handleSearchCompany = () => { | ||
let searchResult: string = "noExistCompany"; | ||
|
||
companyLists.forEach((company: CompanyProps) => { | ||
if (company.korName === searchWord) { | ||
searchResult = "ExistCompany"; | ||
dispatch(changeCompanyId(company.companyId)); | ||
} | ||
}); | ||
|
||
if (searchResult === "noExistCompany") { | ||
dispatch(changeCompanyId(-1)); | ||
} | ||
}; | ||
|
||
// 🔴 2) 클릭 이벤트 | ||
const handleKospi = () => { | ||
dispatch(changeCompanyId(0)); | ||
}; | ||
|
||
const handleStock1 = () => { | ||
dispatch(changeCompanyId(1)); | ||
}; | ||
|
||
const handleStock10 = () => { | ||
dispatch(changeCompanyId(10)); | ||
}; | ||
// | ||
|
||
if (isLoading) { | ||
return <LoadingContainer>{loadingText}</LoadingContainer>; | ||
} | ||
|
||
if (error) { | ||
return <ErrorContainer>{errorText}</ErrorContainer>; | ||
} | ||
|
||
return ( | ||
<Container> | ||
{/* 🔴 차트 변경 이벤트 테스트 */} | ||
<label> | ||
종목 검색 | ||
<input onChange={handleChangeSearchWord} /> | ||
<button onClick={handleSearchCompany}>검색</button> | ||
</label> | ||
<button onClick={handleKospi}>코스피 버튼</button> | ||
<button onClick={handleStock1}>1번 주식 버튼</button> | ||
<button onClick={handleStock10}>10번 주식 버튼</button> | ||
{/* 🔴 차트 변경 이벤트 테스트 */} | ||
<EChartsReact option={options} style={chartStyle} /> | ||
</Container> | ||
); | ||
}; | ||
|
||
export default KospiChart; | ||
|
||
const Container = styled.div` | ||
height: 100%; | ||
display: flex; | ||
flex-direction: column; | ||
justify-content: center; | ||
align-items: center; | ||
`; | ||
|
||
const LoadingContainer = styled.div` | ||
width: 100%; | ||
height: 100%; | ||
display: flex; | ||
justify-content: center; | ||
align-items: center; | ||
font-size: 20px; | ||
font-weight: 500; | ||
color: #999999; | ||
`; | ||
|
||
const ErrorContainer = styled(LoadingContainer)``; | ||
|
||
//🔴 테스트 | ||
// type 선언 | ||
interface CompanyProps { | ||
companyId: number; | ||
code: string; | ||
korName: string; | ||
stockAsBiResponseDto: null; | ||
stockInfResponseDto: null; | ||
} |
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,18 @@ | ||
import { useQuery } from "react-query"; | ||
import axios from "axios"; | ||
|
||
const useGetCompanyList = () => { | ||
const { data, isLoading, error } = useQuery("companyList", getCompanyList, {}); | ||
|
||
return { companyList: data, isLoading, error }; | ||
}; | ||
|
||
export default useGetCompanyList; | ||
|
||
// 서버에서 Company 목록 fetch 하는 함수 | ||
const getCompanyList = async () => { | ||
const res = await axios.get("http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com:8080/companies"); | ||
const companyList = res.data; | ||
|
||
return companyList; | ||
}; |
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,89 @@ | ||
import { useState, useEffect } from "react"; | ||
import { useQuery } from "react-query"; | ||
import axios from "axios"; | ||
|
||
const useGetKospiChart = () => { | ||
const [kospiData, setKospiData] = useState([]); | ||
|
||
const { data, isLoading, error } = useQuery("kospi", getKospiData, { | ||
// onSuccess: () => { | ||
// console.log(data); | ||
// }, | ||
}); | ||
|
||
useEffect(() => { | ||
if (data) { | ||
setKospiData(data); | ||
} | ||
}, [data]); | ||
|
||
const options = { | ||
xAxis: { | ||
type: "category", | ||
data: kospiData.map((kospi: KospiProps) => { | ||
const year = kospi.stck_bsop_date.slice(0, 4); | ||
const month = kospi.stck_bsop_date.slice(4, 6); | ||
const period = `${year}년 ${month}월`; | ||
return period; | ||
}), | ||
}, | ||
yAxis: [ | ||
{ | ||
type: "value", | ||
position: "right", | ||
interval: 100, | ||
min: 2000, | ||
}, | ||
], | ||
dataZoom: [ | ||
{ | ||
type: "inside", | ||
}, | ||
], | ||
tooltip: { | ||
trigger: "axis", | ||
axisPointer: { | ||
type: "cross", | ||
}, | ||
}, | ||
series: [ | ||
{ | ||
name: "코스피 지수", | ||
type: "candlestick", | ||
data: kospiData.map((kospi: KospiProps) => { | ||
return [kospi.bstp_nmix_oprc, kospi.bstp_nmix_prpr, kospi.bstp_nmix_lwpr, kospi.bstp_nmix_hgpr]; | ||
}), | ||
yAxisIndex: 0, | ||
}, | ||
], | ||
}; | ||
|
||
const chartStyle = { | ||
width: "100%", | ||
height: "100%", | ||
}; | ||
|
||
return { isLoading, error, options, chartStyle }; | ||
}; | ||
|
||
export default useGetKospiChart; | ||
|
||
// kospi 차트 데이터 fetch 로직 | ||
const getKospiData = async () => { | ||
const res = await axios.get("http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com/kospi"); | ||
const chartData = res.data.output2; | ||
const kospiData = chartData.reverse(); | ||
|
||
return kospiData; | ||
}; | ||
|
||
interface KospiProps { | ||
acml_tr_pbmn: string; | ||
acml_vol: string; | ||
bstp_nmix_hgpr: string; | ||
bstp_nmix_lwpr: string; | ||
bstp_nmix_oprc: string; | ||
bstp_nmix_prpr: string; | ||
mod_yn: string; | ||
stck_bsop_date: string; | ||
} |
Oops, something went wrong.