diff --git a/client/src/components/CentralChart/Index.tsx b/client/src/components/CentralChart/Index.tsx index 33406389..850f66d3 100644 --- a/client/src/components/CentralChart/Index.tsx +++ b/client/src/components/CentralChart/Index.tsx @@ -3,6 +3,7 @@ import { styled } from "styled-components"; import { StateProps } from "../../models/stateProps"; import UpperMenuBar from "../CentralChartMenu/Index"; +import KospiChart from "./KospiChart"; import StockChart from "./StockChart"; const CentralChart = () => { @@ -11,7 +12,8 @@ const CentralChart = () => { return ( - {companyId !== 0 ?
코스피 차트
: } + {companyId === 0 ? : } + {/* */}
); }; diff --git a/client/src/components/CentralChart/KospiChart.tsx b/client/src/components/CentralChart/KospiChart.tsx new file mode 100644 index 00000000..09744a95 --- /dev/null +++ b/client/src/components/CentralChart/KospiChart.tsx @@ -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) => { + 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 {loadingText}; + } + + if (error) { + return {errorText}; + } + + return ( + + {/* 🔴 차트 변경 이벤트 테스트 */} + + + + + {/* 🔴 차트 변경 이벤트 테스트 */} + + + ); +}; + +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; +} diff --git a/client/src/components/CentralChart/StockChart.tsx b/client/src/components/CentralChart/StockChart.tsx index ae392268..25a786c8 100644 --- a/client/src/components/CentralChart/StockChart.tsx +++ b/client/src/components/CentralChart/StockChart.tsx @@ -1,43 +1,76 @@ -import { useEffect } from "react"; +import { useSelector, useDispatch } from "react-redux"; import { styled } from "styled-components"; import EChartsReact from "echarts-for-react"; +import { StateProps } from "../../models/stateProps"; +import { changeCompanyId } from "../../reducer/CompanyId-Reducer"; + import useGetStockData from "../../hooks/useGetStockData"; -import useGetChart from "../../hooks/useGetChart"; +import useGetStockChart from "../../hooks/useGetStockChart"; + +// 🔴 회사 목록 데이터 불러오는 로직 +import useGetCompanyList from "../../hooks/useGetCompanyList"; const loadingText = "로딩 중 입니다..."; const errorText = "화면을 불러올 수 없습니다"; -// 🔴test -import { useState } from "react"; -import axios from "axios"; +//🔴 테스트 +import { useEffect, useState } from "react"; const StockChart = () => { - // 🔴test - const [params, setParams] = useState(1); + const companyId = useSelector((state: StateProps) => state.companyId); + const dispatch = useDispatch(); - const handlePlus = () => { - setParams((state) => state + 1); - }; + const { isLoading, error } = useGetStockData(companyId); + const { options, chartStyle } = useGetStockChart(companyId); + + // 🔴 차트 변환 테스트 - const handleMinus = () => { - setParams((state) => state - 1); + // 🔴 1) 검색 이벤트 + const { companyList } = useGetCompanyList(); + const [companyLists, setCompanyLists] = useState([]); + const [searchWord, setSearchWord] = useState(""); + + // 회사 목록 불러오면 -> companyList 상태에 할당 + useEffect(() => { + setCompanyLists(companyList); + }, [companyList]); + + const handleChangeSearchWord = (e: React.ChangeEvent) => { + setSearchWord(e.target.value); }; - // 코스피 데이터 정렬 - const testKospi = async () => { - const res = await axios.get("http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com/kospi"); - const kospi = res.data.output2; - return kospi.reverse(); + 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)); + } }; useEffect(() => { - const kospi = testKospi(); - console.log(kospi); - }, []); - // 테스트 + console.log(companyId); + }, [companyId]); + + // 🔴 2) 클릭 이벤트 + const handleKospi = () => { + dispatch(changeCompanyId(0)); + }; - const { isLoading, error } = useGetStockData(params); - const { options, chartStyle } = useGetChart(params); + const handleStock1 = () => { + dispatch(changeCompanyId(1)); + }; + + const handleStock10 = () => { + dispatch(changeCompanyId(10)); + }; + // if (isLoading) { return {loadingText}; @@ -49,8 +82,16 @@ const StockChart = () => { return ( - - + {/* 🔴 차트 변경 이벤트 테스트 */} + + + + + {/* 🔴 차트 변경 이벤트 테스트 */} ); @@ -79,3 +120,13 @@ const LoadingContainer = styled.div` `; const ErrorContainer = styled(LoadingContainer)``; + +//🔴 테스트 +// type 선언 +interface CompanyProps { + companyId: number; + code: string; + korName: string; + stockAsBiResponseDto: null; + stockInfResponseDto: null; +} diff --git a/client/src/hooks/useGetCompanyList.ts b/client/src/hooks/useGetCompanyList.ts new file mode 100644 index 00000000..d3a08000 --- /dev/null +++ b/client/src/hooks/useGetCompanyList.ts @@ -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; +}; diff --git a/client/src/hooks/useGetKospiChart.ts b/client/src/hooks/useGetKospiChart.ts new file mode 100644 index 00000000..90e9f356 --- /dev/null +++ b/client/src/hooks/useGetKospiChart.ts @@ -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; +} diff --git a/client/src/hooks/useGetChart.ts b/client/src/hooks/useGetStockChart.ts similarity index 94% rename from client/src/hooks/useGetChart.ts rename to client/src/hooks/useGetStockChart.ts index 347ba201..51743364 100644 --- a/client/src/hooks/useGetChart.ts +++ b/client/src/hooks/useGetStockChart.ts @@ -1,7 +1,7 @@ import { useState, useEffect } from "react"; import useGetStockData from "./useGetStockData"; -const useGetChart = (companyId: number) => { +const useGetStockChart = (companyId: number) => { const { data } = useGetStockData(companyId); const [chartData, setChartData] = useState([]); @@ -59,7 +59,7 @@ const useGetChart = (companyId: number) => { return { options, chartStyle }; }; -export default useGetChart; +export default useGetStockChart; interface StockProps { stockMinId: number; diff --git a/client/src/hooks/useGetStockData.ts b/client/src/hooks/useGetStockData.ts index a097389e..e241da62 100644 --- a/client/src/hooks/useGetStockData.ts +++ b/client/src/hooks/useGetStockData.ts @@ -39,10 +39,10 @@ const useGetStockData = (companyId: number) => { enabled: true, refetchInterval: autoRefetch && 60000 * 10, // 정각 혹은 30분에 맞춰서 10분 마다 데이터 리패칭 refetchOnMount: true, - onSuccess: () => { - console.log(new Date()); - console.log(data); - }, + // onSuccess: () => { + // console.log(new Date()); + // console.log(data); + // }, }); return { data, isLoading, error };