import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs"; import type { DashboardStockItem, DashboardOrderSide, } from "@/features/dashboard/types/dashboard.types"; import { useOrder } from "@/features/dashboard/hooks/useOrder"; import { useKisRuntimeStore } from "@/features/dashboard/store/use-kis-runtime-store"; import { Loader2 } from "lucide-react"; interface OrderFormProps { stock?: DashboardStockItem; } export function OrderForm({ stock }: OrderFormProps) { const verifiedCredentials = useKisRuntimeStore( (state) => state.verifiedCredentials, ); const { placeOrder, isLoading, error } = useOrder(); // Form State // Initial price set from stock current price if available, relying on component remount (key) for updates const [price, setPrice] = useState( stock?.currentPrice.toString() || "", ); const [quantity, setQuantity] = useState(""); const [activeTab, setActiveTab] = useState("buy"); const handleOrder = async (side: DashboardOrderSide) => { if (!stock || !verifiedCredentials) return; const priceNum = parseInt(price.replace(/,/g, ""), 10); const qtyNum = parseInt(quantity.replace(/,/g, ""), 10); if (isNaN(priceNum) || priceNum <= 0) { alert("가격을 올바르게 입력해주세요."); return; } if (isNaN(qtyNum) || qtyNum <= 0) { alert("수량을 올바르게 입력해주세요."); return; } if (!verifiedCredentials.accountNo) { alert( "계좌번호가 설정되지 않았습니다. 설정에서 계좌번호를 입력해주세요.", ); return; } const response = await placeOrder( { symbol: stock.symbol, side: side, orderType: "limit", // 지정가 고정 price: priceNum, quantity: qtyNum, accountNo: verifiedCredentials.accountNo, accountProductCode: "01", // Default to '01' (위탁) }, verifiedCredentials, ); if (response && response.orderNo) { alert(`주문 전송 완료! 주문번호: ${response.orderNo}`); setQuantity(""); } }; const totalPrice = parseInt(price.replace(/,/g, "") || "0", 10) * parseInt(quantity.replace(/,/g, "") || "0", 10); const setPercent = (pct: string) => { // Placeholder logic for percent click console.log("Percent clicked:", pct); }; const isMarketDataAvailable = !!stock; return (
매수 매도
); } function OrderInputs({ type, price, setPrice, quantity, setQuantity, totalPrice, disabled, hasError, errorMessage, }: { type: "buy" | "sell"; price: string; setPrice: (v: string) => void; quantity: string; setQuantity: (v: string) => void; totalPrice: number; disabled: boolean; hasError: boolean; errorMessage: string | null; }) { return (
주문가능 - {type === "buy" ? "KRW" : "주"}
{hasError && (
{errorMessage}
)}
{type === "buy" ? "매수가격" : "매도가격"} setPrice(e.target.value)} disabled={disabled} />
주문수량 setQuantity(e.target.value)} disabled={disabled} />
주문총액
); } function PercentButtons({ onSelect }: { onSelect: (pct: string) => void }) { return (
{["10%", "25%", "50%", "100%"].map((pct) => ( ))}
); }