diff --git a/client/src/components/StockOrderSection/Index.tsx b/client/src/components/StockOrderSection/Index.tsx index 3e1b1d91..c5cc3a10 100644 --- a/client/src/components/StockOrderSection/Index.tsx +++ b/client/src/components/StockOrderSection/Index.tsx @@ -28,10 +28,6 @@ const StockOrderSection = () => { const { stockInfo, stockInfoLoading, stockInfoError } = useGetStockInfo(companyId); const { stockPrice, stockPriceLoading, stockPriceError } = useGetStockData(companyId); - console.log(isLogin); - const localData = localStorage.getItem("authToken"); - console.log(localData); - // 주식주문 창 닫기 const handleStockOrderClose = () => { dispatch(stockOrderClose()); diff --git a/client/src/components/StockOrderSection/StockOrder.tsx b/client/src/components/StockOrderSection/StockOrder.tsx index 76cac06a..9dac79a4 100644 --- a/client/src/components/StockOrderSection/StockOrder.tsx +++ b/client/src/components/StockOrderSection/StockOrder.tsx @@ -3,6 +3,7 @@ import { isHoliday } from "@hyunbinseo/holidays-kr"; import { closeDecisionWindow } from "../../reducer/SetDecisionWindow-Reducer"; import { styled } from "styled-components"; import { StateProps } from "../../models/stateProps"; +import useTradeStock from "../../hooks/useTradeStock"; import StockPriceList from "./StockPriceList"; import StockOrderSetting from "./StockOrderSetting"; @@ -41,6 +42,24 @@ const StockOrder = ({ corpName }: { corpName: string }) => { dispatch(closeDecisionWindow()); }; + //🔴 주문 관련 테스트 + const orderRequest = useTradeStock(); + + const handleStockOrder = () => { + orderRequest.mutate(); + const { isLoading, isError } = orderRequest; + + if (isLoading) { + console.log("주식 주문 진행 중"); + } + + if (isError) { + console.log("주문 오류 발생"); + } + + handleCloseDecisionWindow(); + }; + // 1) 주말, 공휴일 여부 체크 const today = new Date(); const nonBusinessDay = isHoliday(today, { include: { saturday: true, sunday: true } }); // 토요일, 일요일, 공휴일 (임시 공휴일 포함) @@ -53,7 +72,9 @@ const StockOrder = ({ corpName }: { corpName: string }) => { const closingTime = isBefore9AM || isAfter330PM; // 주문 실패 케이스 1) 개장시간 2) 가격/거래량 설정 - const orderFailureCase01 = nonBusinessDay || closingTime; + // 🔴 3시 30분 이후 작업 위해 closingTime 조건 해제 + const orderFailureCase01 = nonBusinessDay; + // const orderFailureCase01 = nonBusinessDay || closingTime; const orderFailureCase02 = orderPrice === 0 || orderVolume === 0; return ( @@ -105,7 +126,7 @@ const StockOrder = ({ corpName }: { corpName: string }) => { - diff --git a/client/src/hooks/useTradeStock.ts b/client/src/hooks/useTradeStock.ts new file mode 100644 index 00000000..4aeb06b2 --- /dev/null +++ b/client/src/hooks/useTradeStock.ts @@ -0,0 +1,45 @@ +import { useSelector } from "react-redux"; +import { StateProps } from "../models/stateProps"; +import { useMutation } from "react-query"; +import axios from "axios"; + +const useTradeStock = () => { + const orderType = useSelector((state: StateProps) => state.stockOrderType); + const companyId = useSelector((state: StateProps) => state.companyId); + const orderPrice = useSelector((state: StateProps) => state.stockOrderPrice); + const orderVolume = useSelector((state: StateProps) => state.stockOrderVolume); + + const orderRequest = useMutation(() => postOrderRequest(orderType, companyId, orderPrice, orderVolume)); + return orderRequest; +}; + +export default useTradeStock; + +const postOrderRequest = async (orderType: boolean, companyId: number, price: number, volume: number) => { + const authToken = localStorage.getItem("authToken"); + console.log(authToken); + const options = { + headers: { + Authorization: `${authToken}`, + "Content-Type": "application/json", + }, + }; + + // 매수 + if (!orderType) { + const response = await axios.post(`http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com:8080/stock/buy?companyId=${companyId}&price=${price}&stockCount=${volume}`, {}, options); + const orderResult = await response.data; + + console.log(orderResult); // 테스트 코드 + return orderResult; + } + + // 매도 + if (orderType) { + const response = await axios.post(`http://ec2-13-125-246-160.ap-northeast-2.compute.amazonaws.com:8080/stock/sell?companyId=${companyId}&price=${price}&stockCount=${volume}`, {}, options); + const orderResult = await response.data; + + console.log(orderResult); // 테스트 코드 + return orderResult; + } +};