1
- import React from 'react' ;
1
+ import React , { useEffect } from 'react' ;
2
2
import { useState } from 'react' ;
3
3
import Box from '@mui/material/Box' ;
4
4
import Checkbox from '@mui/material/Checkbox' ;
5
5
import FormControlLabel from '@mui/material/FormControlLabel' ;
6
6
import YearBox from 'Common/section/number' ;
7
7
import './CartItems.scss' ;
8
8
9
- function CartItems ( ) {
10
- interface Item {
11
- id : number ;
12
- // image:string;
13
- name : string ;
14
- text : string ;
15
- price : string ;
16
- sale : string ;
17
- malize : string ;
18
- }
9
+ interface BuyItem {
10
+ id : number ;
11
+ product_name : string ;
12
+ price : number ;
13
+ detail_image : string ;
14
+ }
15
+
16
+ interface CartItemsProps {
17
+ check : number [ ] ;
18
+ pitem : any ;
19
+ setItems :any ;
20
+
21
+ }
19
22
23
+ const CartItems = ( { check, pitem, setItems } : CartItemsProps ) => {
20
24
const [ info , setInfo ] = useState ( [ ] ) ;
21
- const [ checked , setChecked ] = useState ( [ true , false ] ) ;
22
- const [ items , setItems ] = useState < Item [ ] > ( [
23
- {
24
- id : 1 ,
25
- name : '[국내도서]시작하세요! C# 10프로그래밍' ,
26
- text : '*밤 11시 잠들기전 배송' ,
27
- price : '정가: 36000' ,
28
- sale : '판매가:32,400' ,
29
- malize : '마일리지: 1,800원' ,
30
- } ,
31
- {
32
- id : 2 ,
33
- name : '도서명2' ,
34
- text : '*밤 11시 잠들기전 배송' ,
35
- price : '정가: 36000' ,
36
- sale : '판매가:32,400' ,
37
- malize : '마일리지: 1,800원' ,
38
- } ,
39
- {
40
- id : 3 ,
41
- name : '도서명3' ,
42
- text : '*밤 11시 잠들기전 배송' ,
43
- price : '정가: 36000' ,
44
- sale : '판매가:32,400' ,
45
- malize : '마일리지: 1,800원' ,
46
- } ,
47
- {
48
- id : 4 ,
49
- name : '도서명3' ,
50
- text : '*밤 11시 잠들기전 배송' ,
51
- price : '정가: 36000' ,
52
- sale : '판매가:32,400' ,
53
- malize : '마일리지: 1,800원' ,
54
- } ,
55
- {
56
- id : 5 ,
57
- name : '도서명3' ,
58
- text : '*밤 11시 잠들기전 배송' ,
59
- price : '정가: 36000' ,
60
- sale : '판매가:32,400' ,
61
- malize : '마일리지: 1,800원' ,
62
- } ,
63
- ] ) ;
64
- const [ checkedItems , setCheckedItems ] = useState < number [ ] > ( [ ] ) ;
25
+ const [ buyItem , setbuyItem ] = useState < BuyItem [ ] > ( [ ] ) ;
26
+ const [ checkedItems , setCheckedItems ] = useState < number [ ] > ( check ) ;
27
+
28
+
29
+ useEffect ( ( ) => {
30
+ BuyCart ( ) ;
31
+ } , [ ] ) ;
32
+
33
+ const BuyCart = ( ) => {
34
+ const BuyItems = JSON . parse ( localStorage . getItem ( "cart" ) ) || [ ] ;
35
+ setbuyItem ( BuyItems ) ;
36
+ console . log ( BuyItems )
37
+ }
65
38
66
39
const handleChange1 = ( event : React . ChangeEvent < HTMLInputElement > ) => {
67
40
const checkedValue = event . target . checked ;
68
41
const updatedCheckedItems = checkedValue
69
- ? items . map ( ( item ) => item . id )
42
+ ? buyItem . map ( ( item ) => item . id )
70
43
: [ ] ;
71
- setCheckedItems ( updatedCheckedItems ) ;
44
+ setCheckedItems ( updatedCheckedItems ) ;
72
45
} ;
73
46
74
- const handleChange2 = ( event : React . ChangeEvent < HTMLInputElement > ) => {
47
+ const handleChange2 = ( el : any ) => ( event : React . ChangeEvent < HTMLInputElement > ) => {
48
+ console . log ( el ) ;
75
49
const itemId = parseInt ( event . target . name ) ;
76
50
let updatedCheckedItems : number [ ] = [ ] ;
77
-
51
+ let updatedItems : any [ ] = [ ] ;
78
52
if ( event . target . checked ) {
79
53
updatedCheckedItems = [ ...checkedItems , itemId ] ;
54
+ updatedItems = [ ...pitem , el ] ;
80
55
} else {
81
56
updatedCheckedItems = checkedItems . filter ( ( id ) => id !== itemId ) ;
82
- }
57
+ updatedItems = pitem . filter ( ( key ) => key . product_no !== el . product_no ) ;
58
+ }
59
+ setItems ( updatedItems ) ;
83
60
setCheckedItems ( updatedCheckedItems ) ;
61
+ setCheck ( updatedCheckedItems ) ;
84
62
} ;
85
63
86
- const children = ( itemId : number ) => (
64
+ const children = ( el : any , index : number ) => (
87
65
< Box
88
66
sx = { {
89
67
display : 'flex' ,
90
68
flexDirection : 'column' ,
91
69
ml : 3 ,
92
70
fontSize : 'large' ,
93
- } }
94
- >
71
+ } } >
95
72
< FormControlLabel
96
73
label = ""
97
74
control = {
98
75
< Checkbox
99
- checked = { checkedItems . includes ( itemId ) }
100
- onChange = { handleChange2 }
101
- name = { itemId . toString ( ) }
76
+ checked = { checkedItems . includes ( index ) }
77
+ onChange = { handleChange2 ( el ) }
78
+ name = { index }
102
79
/>
103
80
}
104
81
/>
@@ -113,6 +90,26 @@ function CartItems() {
113
90
} ) ;
114
91
} ;
115
92
93
+ const RemoveBuyItem = ( index : number , key :any ) => {
94
+ console . log ( key ) ;
95
+ const confirmation = window . confirm ( '삭제하시겠습니까?' ) ;
96
+ if ( confirmation ) {
97
+
98
+ const updatedCartData = JSON . parse ( localStorage . getItem ( 'cart' ) ) ;
99
+ localStorage . setItem ( 'cart' , JSON . stringify ( updatedCartData . filter ( ( item ) => item . product_no !== key ) ) ) ;
100
+
101
+ const updatedBuyItem = [ ...buyItem ] ;
102
+ //선택한 index 1개를 buyItem 배열에서 제거.
103
+ updatedBuyItem . splice ( index , 1 ) ;
104
+ // 제거한후의 값 setState 갑에 담아줌.
105
+ setbuyItem ( updatedBuyItem ) ;
106
+ // cart 키 값으로 로컬에서 데이터 찾아옴
107
+ alert ( '삭제되었습니다.' ) ;
108
+ } else {
109
+ alert ( '취소되었습니다.' ) ;
110
+ }
111
+ } ;
112
+
116
113
return (
117
114
< >
118
115
< div className = "CartPageTable__Buy" >
@@ -122,20 +119,16 @@ function CartItems() {
122
119
< div className = "LableBox" >
123
120
< Checkbox
124
121
size = "large"
125
- checked = { checkedItems . length === items . length }
122
+ checked = { checkedItems . length === buyItem . length }
126
123
indeterminate = {
127
- checkedItems . length > 0 && checkedItems . length < items . length
124
+ checkedItems . length > 0 && checkedItems . length < buyItem . length
128
125
}
129
126
onChange = { handleChange1 }
130
127
/>
131
128
</ div >
132
129
}
133
130
/>
134
131
135
- { /* <div className="Product-Buy">
136
- <span>구매</span>
137
- </div> */ }
138
-
139
132
< div className = "ProductNameBox" >
140
133
< span > 상품명</ span >
141
134
</ div >
@@ -150,25 +143,23 @@ function CartItems() {
150
143
</ div >
151
144
152
145
< div className = "ItemsContainer" >
153
- { items . map ( ( item ) => (
154
- < div className = "ItemContainer" >
155
- < div className = "CheckContainer" > { children ( item . id ) } </ div >
156
- < div key = { item . id } className = "ImageBox" >
157
- < img src = "/images/cartbookimage.jpg" alt = "cartbookimage" />
146
+ { buyItem . map ( ( el , index ) => (
147
+ < div className = "ItemContainer" key = { index } >
148
+ { /* 아이템 체크 하는 부분 */ }
149
+ < div className = "CheckContainer" > { children ( el , index ) } </ div >
150
+ < div key = { el . id } className = "ImageBox" >
151
+ < img src = { el . detail_image } alt = "cartbookimage" />
158
152
</ div >
159
153
< div className = "TextInner" >
160
- < span > { item . name } </ span >
161
- < span > { item . text } </ span >
154
+ < span > { el . product_name } </ span >
162
155
</ div >
163
156
164
157
< div className = "PriceInner" >
165
- < span > { item . price } </ span >
166
- < span > { item . sale } </ span >
167
- < span > { item . malize } </ span >
158
+ < span > { Number ( el . price ) . toFixed ( 0 ) } 원</ span >
168
159
</ div >
169
160
170
161
< div className = "CartButtonBox" >
171
- < button > 삭제</ button >
162
+ < button onClick = { ( ) => RemoveBuyItem ( index , el . product_no ) } > 삭제</ button >
172
163
</ div >
173
164
</ div >
174
165
) ) }
0 commit comments