1- import { useState , useCallback , useEffect } from 'react'
1+ import { useState , useCallback } from 'react'
22import { Notification } from './types'
33import { AdminPage } from './components/AdminPage'
44import { useProducts } from './hooks/useProducts'
55import { CartPage } from './components/CartPage'
6- import { getRemainingStock } from './models/cart'
76import { useCart } from './hooks/useCart'
87import { useCoupons } from './hooks/useCoupons'
9- import { Header } from './components/ui/Header'
108import { Notifications } from './components/ui/Notifications'
119
1210const App = ( ) => {
@@ -27,80 +25,28 @@ const App = () => {
2725 useProducts ( addNotification )
2826 const {
2927 cart,
30- setCart ,
28+ completeOrder ,
3129 removeFromCart,
3230 updateQuantity,
3331 selectedCoupon,
3432 setSelectedCoupon,
3533 addToCart,
36- applyCoupon,
3734 calculateCartTotal,
38- calculateItemTotal,
35+ getRemainingStock,
36+ handleSelectCoupon,
3937 } = useCart ( addNotification )
4038 const { coupons, addCoupon, deleteCoupon } = useCoupons (
4139 addNotification ,
4240 selectedCoupon ,
4341 setSelectedCoupon ,
4442 )
4543 const [ isAdmin , setIsAdmin ] = useState ( false )
46- const [ activeTab , setActiveTab ] = useState < 'products' | 'coupons' > ( 'products' )
47- const [ searchTerm , setSearchTerm ] = useState ( '' )
48- const [ debouncedSearchTerm , setDebouncedSearchTerm ] = useState ( '' )
49- const [ totalItemCount , setTotalItemCount ] = useState ( 0 )
50-
51- useEffect ( ( ) => {
52- const count = cart . reduce ( ( sum , item ) => sum + item . quantity , 0 )
53- setTotalItemCount ( count )
54- } , [ cart ] )
55-
56- useEffect ( ( ) => {
57- const timer = setTimeout ( ( ) => {
58- setDebouncedSearchTerm ( searchTerm )
59- } , 500 )
60- return ( ) => clearTimeout ( timer )
61- } , [ searchTerm ] )
62-
63- const formatPrice = ( price : number , productId ?: string ) : string => {
64- if ( productId ) {
65- const product = products . find ( ( p ) => p . id === productId )
66- if ( product && getRemainingStock ( product , cart ) <= 0 ) {
67- return 'SOLD OUT'
68- }
69- }
70-
71- if ( isAdmin ) {
72- return `${ price . toLocaleString ( ) } 원`
73- }
74-
75- return `₩${ price . toLocaleString ( ) } `
76- }
77-
78- const completeOrder = useCallback ( ( ) => {
79- const orderNumber = `ORD-${ Date . now ( ) } `
80- addNotification (
81- `주문이 완료되었습니다. 주문번호: ${ orderNumber } ` ,
82- 'success' ,
83- )
84- setCart ( [ ] )
85- setSelectedCoupon ( null )
86- } , [ addNotification , setCart , setSelectedCoupon ] )
8744
88- const totals = calculateCartTotal ( )
89-
90- const headerState = {
45+ const adminProps = {
9146 isAdmin,
92- searchTerm,
93- cart,
94- totalItemCount,
95- setSearchTerm,
9647 setIsAdmin,
97- }
98-
99- const adminState = {
100- activeTab,
101- setActiveTab,
10248 products,
103- formatPrice ,
49+ cart ,
10450 deleteProduct,
10551 addNotification,
10652 coupons,
@@ -110,21 +56,20 @@ const App = () => {
11056 addCoupon,
11157 }
11258
113- const cartState = {
59+ const cartProps = {
60+ isAdmin,
61+ setIsAdmin,
11462 products,
11563 cart,
11664 coupons,
117- totals,
118- debouncedSearchTerm,
65+ totals : calculateCartTotal ( ) ,
11966 selectedCoupon,
12067 addToCart,
12168 removeFromCart,
122- formatPrice,
123- calculateItemTotal,
69+ handleSelectCoupon,
12470 updateQuantity,
125- applyCoupon,
126- setSelectedCoupon,
12771 completeOrder,
72+ getRemainingStock,
12873 }
12974
13075 return (
@@ -133,10 +78,7 @@ const App = () => {
13378 notifications = { notifications }
13479 setNotifications = { setNotifications }
13580 />
136- < Header { ...headerState } />
137- < main className = "max-w-7xl mx-auto px-4 py-8" >
138- { isAdmin ? < AdminPage { ...adminState } /> : < CartPage { ...cartState } /> }
139- </ main >
81+ { isAdmin ? < AdminPage { ...adminProps } /> : < CartPage { ...cartProps } /> }
14082 </ div >
14183 )
14284}
0 commit comments