1+ import { useCallback , useState , useReducer } from 'react'
12import { ConfigProvider } from 'antd'
3+ 24import Meals from './components/meals'
35import Search from './components/search'
46import Cart from './components/cart'
57import cartContext from './store/cart'
6- import { useCallback , useState } from 'react'
7- 8- // 模拟一组食物数据
9- const MEALS_DATA = [
10- {
11- id : '1' ,
12- title : '汉堡包' ,
13- desc : '百分百纯牛肉配搭爽脆酸瓜洋葱粒与美味番茄酱经典滋味让你无法抵挡!' ,
14- price : 12 ,
15- img : '/img/hamburger1.png'
16- } ,
17- {
18- id : '2' ,
19- title : '双层吉士汉堡' ,
20- desc : '百分百纯牛肉与双层香软芝,加上松软面包及美味酱料,诱惑无人能挡!' ,
21- price : 20 ,
22- img : '/img/hamburger2.png'
23- } ,
24- {
25- id : '3' ,
26- title : '巨无霸' ,
27- desc : '两块百分百纯牛肉,搭配生菜、洋葱等新鲜食材,口感丰富,极致美味!' ,
28- price : 24 ,
29- img : '/img/hamburger3.png'
30- } , {
31- id : '4' ,
32- title : '麦辣鸡腿汉堡' ,
33- desc : '金黄脆辣的外皮,鲜嫩幼滑的鸡腿肉,多重滋味,一次打动您挑剔的味蕾!' ,
34- price : 21 ,
35- img : '/img/hamburger1.png'
36- } , {
37- id : '5' ,
38- title : '板烧鸡腿堡' ,
39- desc : '原块去骨鸡排嫩滑多汁,与翠绿新鲜的生菜和香浓烧鸡酱搭配,口感丰富!' ,
40- price : 22 ,
41- img : '/img/hamburger2.png'
42- } , {
43- id : '6' ,
44- title : '麦香鸡' ,
45- desc : '清脆爽口的生菜,金黄酥脆的鸡肉。营养配搭,好滋味的健康选择!' ,
46- price : 14 ,
47- img : '/img/hamburger3.png'
48- } , {
49- id : '7' ,
50- title : '吉士汉堡包' ,
51- desc : '百分百纯牛肉与香软芝士融为一体配合美味番茄醬丰富口感一咬即刻涌现!' ,
52- price : 12 ,
53- img : '/img/hamburger1.png'
8+ import { MEALS_DATA } from './mock'
9+ 10+ const reduce = ( state , action ) => {
11+ let newMeal = { ...state }
12+ let { type, item } = action
13+ switch ( type ) {
14+ case 'ADD' :
15+ newMeal . amount ++
16+ newMeal . totalPrice += item ?. price
17+ if ( newMeal ?. items ?. indexOf ( item ) === - 1 ) {
18+ newMeal ?. items ?. push ( item )
19+ item . count = 1
20+ 21+ } else {
22+ item . count ++
23+ }
24+ break
25+ 26+ case "REMOVE" :
27+ newMeal . totalPrice -= item ?. price
28+ newMeal . amount --
29+ item . count --
30+ if ( item . count === 0 ) {
31+ newMeal ?. items ?. splice ( newMeal . items . indexOf ( item ) , 1 )
32+ }
33+ break
34+ 35+ case "CLEAR" :
36+ newMeal ?. items ?. forEach ( meal => delete meal . count )
37+ newMeal . items = [ ]
38+ newMeal . amount = 0
39+ newMeal . totalPrice = 0
40+ break
41+ 42+ default :
43+ newMeal = state
44+ break
5445 }
55- ]
46+ return newMeal
47+ }
48+ 5649
5750function App ( ) {
5851 const [ mealsData , setMealsData ] = useState ( MEALS_DATA )
59- const [ cartData , setCartData ] = useState ( {
52+ const [ cartData , cartDispatch ] = useReducer ( reduce , {
6053 items : [ ] ,
6154 totalPrice : 0 ,
6255 amount : 0
6356 } )
6457
6558 // 添加商品
66- const addItem = ( item ) => {
67- let newMeal = { ...cartData }
68- newMeal . amount ++
69- newMeal . totalPrice += item ?. price
70- if ( newMeal . items . indexOf ( item ) === - 1 ) {
71- newMeal . items . push ( item )
72- item . count = 1
59+ // const addItem = (item) => {
60+ // let newMeal = { ...cartData }
61+ // newMeal.amount++
62+ // newMeal.totalPrice += item?.price
63+ // if (newMeal.items.indexOf(item) === -1) {
64+ // newMeal.items.push(item)
65+ // item.count = 1
7366
74- } else {
75- item . count ++
76- }
77- setCartData ( newMeal )
78- }
67+ // } else {
68+ // item.count++
69+ // }
70+ // setCartData(newMeal)
71+ // }
7972
8073 // 删除商品
81- const removeItem = ( item ) => {
82- let newMeal = { ...cartData }
83- newMeal . totalPrice -= item ?. price
84- newMeal . amount --
85- item . count --
86- if ( item . count === 0 ) {
87- newMeal . items . splice ( newMeal . items . indexOf ( item ) , 1 )
88- }
89- setCartData ( newMeal )
90- }
74+ // const removeItem = (item) => {
75+ // let newMeal = { ...cartData }
76+ // newMeal.totalPrice -= item?.price
77+ // newMeal.amount--
78+ // item.count--
79+ // if (item.count === 0) {
80+ // newMeal.items.splice(newMeal.items.indexOf(item), 1)
81+ // }
82+ // setCartData(newMeal)
83+ // }
9184
9285 // 清空购物车
93- const clearCart = ( ) => {
94- let cart = {
95- items : [ ] ,
96- amount : 0 ,
97- totalPrice : 0
98- }
99- let meals = [ ...mealsData ]
100- meals . forEach ( meal => delete meal . count )
101- setCartData ( cart )
102- }
86+ // const clearCart = () => {
87+ // let cart = {
88+ // items: [],
89+ // amount: 0,
90+ // totalPrice: 0
91+ // }
92+ // let meals = [...mealsData]
93+ // meals.forEach(meal => delete meal.count)
94+ // setCartData(cart)
95+ // }
10396
10497 // 搜索
10598 const filterData = useCallback ( ( keyWord ) => {
@@ -116,7 +109,7 @@ function App () {
116109 } , [ setMealsData ] )
117110
118111 return (
119- < cartContext . Provider value = { { ...cartData , addItem , removeItem , clearCart } } >
112+ < cartContext . Provider value = { { ...cartData , cartDispatch } } >
120113 < ConfigProvider theme = { { token : { colorPrimary : '#ffcd00' } } } >
121114 < div style = { { width : '750rem' , height : '100vh' , overflow : 'hidden' , position : 'relative' } } >
122115 < Search filterFn = { filterData } > </ Search >
0 commit comments