From 9f97751c3db47f1d009ff12017d4e09096eb2397 Mon Sep 17 00:00:00 2001 From: Minjae Date: Mon, 16 Jun 2025 21:11:30 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20=EA=B3=BC=EC=A0=9C=20=EC=A0=9C=EC=B6=9C?= =?UTF-8?q?=ED=95=A9=EB=8B=88=EB=8B=A4.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package-lock.json | 4 +- src/app/checkout/page.tsx | 52 +++++++++++++++++++++++++- src/app/layout.tsx | 3 +- src/app/mypage/page.tsx | 23 +++++++++++- src/app/search/page.tsx | 8 +++- src/component/search/SearchInput.tsx | 11 +++++- src/component/shopping/CartList.tsx | 17 ++++++++- src/component/shopping/ProductCart.tsx | 24 +++++++++++- src/context/UserContext.tsx | 12 +++++- tsconfig.json | 21 +++++++++-- 10 files changed, 159 insertions(+), 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 22c80ad..0b88957 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,11 +1,11 @@ { - "name": "cnu-next-week02", + "name": "cnu-next", "version": "0.1.0", "lockfileVersion": 3, "requires": true, "packages": { "": { - "name": "cnu-next-week02", + "name": "cnu-next", "version": "0.1.0", "dependencies": { "next": "15.3.3", diff --git a/src/app/checkout/page.tsx b/src/app/checkout/page.tsx index 0d40153..238e20d 100644 --- a/src/app/checkout/page.tsx +++ b/src/app/checkout/page.tsx @@ -1,5 +1,7 @@ // CheckoutPage -import { useState } from "react"; +"use client"; +import { useEffect, useState } from "react"; +import { useRouter } from "next/navigation"; import { ProductItem } from "@/types/Product"; interface CheckoutItem { @@ -9,13 +11,61 @@ interface CheckoutItem { // 과제 3 export default function CheckoutPage() { const [items, setItems] = useState([]); + const router = useRouter(); + + useEffect(() => { + const data = localStorage.getItem("checkoutItems"); + if (data) { + const parsed: CheckoutItem[] = JSON.parse(data); + setItems(parsed); + localStorage.removeItem("checkoutItems"); + } + }, []); + + const total = items.reduce( + (sum, item) => sum + Number(item.product.lprice) * item.quantity, + 0 + ); + // 3.1. 결제하기 구현 return (

✅ 결제가 완료되었습니다!

{/* 3.1. 결제하기 구현 */} + {items.length === 0 ? ( +

결제된 아이템이 없습니다.

+ ) : ( + <> +
    + {items.map((item, index) => ( +
  • +
    +

    +

    수량: {item.quantity}

    +
    +
    + {( + Number(item.product.lprice) * item.quantity + ).toLocaleString()} + 원 +
    +
  • + ))} +
+ +
+ 총 합계: {total.toLocaleString()}원 +
+ + )}
{/* 3.2. 홈으로 가기 버튼 구현 */} +
); } diff --git a/src/app/layout.tsx b/src/app/layout.tsx index f7fa87e..3725a53 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -1,6 +1,7 @@ import type { Metadata } from "next"; import { Geist, Geist_Mono } from "next/font/google"; import "./globals.css"; +import { UserProvider } from "@/context/UserContext"; const geistSans = Geist({ variable: "--font-geist-sans", @@ -27,7 +28,7 @@ export default function RootLayout({ - {children} + {children} ); diff --git a/src/app/mypage/page.tsx b/src/app/mypage/page.tsx index 93b3ba9..fe24eb7 100644 --- a/src/app/mypage/page.tsx +++ b/src/app/mypage/page.tsx @@ -1,14 +1,33 @@ +"use client"; + +import Header from "@/component/layout/Header"; +import { useUser } from "@/context/UserContext"; +import Link from "next/link"; + // 과제 1: 마이페이지 구현 export default function MyPage() { // 1.1. UserContext를 활용한 Mypage 구현 (UserContext에 아이디(userId: string), 나이(age: number), 핸드폰번호(phoneNumber: string) 추가) + const { user } = useUser(); return (
{/* 1.2. Header Component를 재활용하여 Mypage Header 표기 (title: 마이페이지) */} -

마이페이지

+ +
{/* Mypage 정보를 UserContext 활용하여 표시 (이름, 아이디, 나이, 핸드폰번호 모두 포함) */} +
+

회원 정보

+

이름: {user.name}

+

아이디: {user.userId}

+

나이: {user.age}

+

전화번호: {user.phoneNumber}

+
{/* 1.3. 홈으로 가기 버튼 구현(Link or Router 활용) */} + + 홈으로 가기 +
); -} +} \ No newline at end of file diff --git a/src/app/search/page.tsx b/src/app/search/page.tsx index c3b6212..47992f4 100644 --- a/src/app/search/page.tsx +++ b/src/app/search/page.tsx @@ -15,7 +15,13 @@ export default function SearchHome() { // 페이지 최초 렌더링 될 때, setUser로 이름 설정 useEffect(() => { // 학번 + 이름 형태로 작성 (ex. 2025***** 내이름 ) - setUser({ name: "" }); + setUser({ + name: "202303194 김민재", + age: 25, + userId: "ashclothes01", + email: "202303194@o.cnu.ac.kr", + phoneNumber: "010-1234-5678", + }); }, []); return ( diff --git a/src/component/search/SearchInput.tsx b/src/component/search/SearchInput.tsx index aea7294..976054f 100644 --- a/src/component/search/SearchInput.tsx +++ b/src/component/search/SearchInput.tsx @@ -1,8 +1,14 @@ "use client"; import { useSearch } from "@/context/SearchContext"; +import { useRef, useEffect } from "react"; export default function SearchInput() { const { query, setQuery, setResult } = useSearch(); + const inputRef = useRef(null); + + useEffect(() => { + inputRef.current?.focus(); + }, []); // 검색 기능 const search = async () => { @@ -19,13 +25,16 @@ export default function SearchInput() { }; // 2.2. SearchInput 컴포넌트가 최초 렌더링 될 때, input tag에 포커스 되는 기능 - const handleInputChange = () => {}; + const handleInputChange = (e: React.ChangeEvent) => { + setQuery(e.target.value); + }; // 과제 1-2-3: 페이지 최초 렌더링 시, input에 포커스 되는 기능 (useRef) return (
{}; + const handleCheckout = () => { + const checkoutItems = cartItems.map((item) => ({ + product: { + productId: item.productId, + title: item.title, + lprice: Number(item.lprice), + }, + quantity: item.quantity, + })); + + localStorage.setItem("checkoutItems", JSON.stringify(checkoutItems)); + router.push("/checkout"); + }; return (

🛒 장바구니

diff --git a/src/component/shopping/ProductCart.tsx b/src/component/shopping/ProductCart.tsx index a66c2b3..d6d415d 100644 --- a/src/component/shopping/ProductCart.tsx +++ b/src/component/shopping/ProductCart.tsx @@ -18,9 +18,23 @@ export default function ProductCart({ items }: { items: ProductItem[] }) { localStorage.setItem(item.productId, quantity + ""); localStorage.getItem(item.productId); }; + useEffect(() => { + const hasItems = Object.keys(cart).length > 0; + setShowCart(hasItems); + }, [cart]); + /* 과제 2-3: Cart 아이템 지우기 */ - const handleRemoveFromCart = () => {}; + const handleRemoveFromCart = (productId: string) => { + setCart((prev) => { + const updated = Object.fromEntries( + Object.entries(prev).filter(([id]) => id !== productId) + ); + return updated; + }); + + localStorage.removeItem(productId); + }; return (
@@ -28,7 +42,13 @@ export default function ProductCart({ items }: { items: ProductItem[] }) { {/* 장바구니 */} {/* 2.1. 조건부 카트 보이기: 카트에 담긴 상품이 없으면 카트가 보이지 않고, 카트에 담긴 물건이 있으면 카트가 보인다 */} - + {showCart && ( + + )}
); } diff --git a/src/context/UserContext.tsx b/src/context/UserContext.tsx index e5d3f14..9bc9e09 100644 --- a/src/context/UserContext.tsx +++ b/src/context/UserContext.tsx @@ -8,6 +8,10 @@ interface User { name: string; // age: number // 추가하고 싶은 속성들 ... + age: number; + userId: string; + email: string; + phoneNumber: string; } // UserContextType interface UserContextType { @@ -22,7 +26,13 @@ export const UserContext = createContext( // 2. Provider 생성 export const UserProvider = ({ children }: { children: ReactNode }) => { - const [user, setUser] = useState({ name: "" }); + const [user, setUser] = useState({ + name: "202303194 김민재", + age: 25, + userId: "ashclothes01", + email: "202303194@o.cnu.ac.kr", + phoneNumber: "010-1234-5678", + }); return ( {children} diff --git a/tsconfig.json b/tsconfig.json index 49e8453..f776024 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,7 +1,11 @@ { "compilerOptions": { "target": "ES2017", - "lib": ["dom", "dom.iterable", "esnext"], + "lib": [ + "dom", + "dom.iterable", + "esnext" + ], "allowJs": true, "skipLibCheck": true, "strict": true, @@ -19,11 +23,20 @@ } ], "paths": { - "@/*": ["./src/*"] + "@/*": [ + "./src/*" + ] }, "noUnusedLocals": false, "noUnusedParameters": false }, - "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], - "exclude": ["node_modules"] + "include": [ + "next-env.d.ts", + "**/*.ts", + "**/*.tsx", + ".next/types/**/*.ts" + ], + "exclude": [ + "node_modules" + ] }