Skip to content

Commit

Permalink
[Add] 매수/매도 통신 로직 구현 및 적용 (오류 존재)
Browse files Browse the repository at this point in the history
- 주식 매수/매도 관련 서버 통신 로직 작성 후 적용하였으나 오류 발생하는 상황 (500 Error)
- BE 담당자와 소통하여 문제 해결할 예정

Issues #17
  • Loading branch information
novice1993 committed Sep 12, 2023
1 parent c8c8779 commit bbbd055
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
4 changes: 0 additions & 4 deletions client/src/components/StockOrderSection/Index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
25 changes: 23 additions & 2 deletions client/src/components/StockOrderSection/StockOrder.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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 } }); // 토요일, 일요일, 공휴일 (임시 공휴일 포함)
Expand All @@ -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 (
Expand Down Expand Up @@ -105,7 +126,7 @@ const StockOrder = ({ corpName }: { corpName: string }) => {
<button className="cancel" onClick={handleCloseDecisionWindow}>
{cancelButtonText}
</button>
<button className="confirm" onClick={handleCloseDecisionWindow}>
<button className="confirm" onClick={handleStockOrder}>
{confirmButtonText}
</button>
</div>
Expand Down
45 changes: 45 additions & 0 deletions client/src/hooks/useTradeStock.ts
Original file line number Diff line number Diff line change
@@ -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;
}
};

0 comments on commit bbbd055

Please sign in to comment.