Skip to content

Commit f16074d

Browse files
authored
Merge pull request #5 from KDT5TeamWink/KimHaEun
[230615]2차PR-김하은
2 parents b086e45 + 5766416 commit f16074d

File tree

5 files changed

+384
-278
lines changed

5 files changed

+384
-278
lines changed

src/Components/Views/CartPage/CartItems/CartItems.scss

+26-54
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,12 @@
2828
.ProductNameBox {
2929
@include CartNameStyle;
3030
width: 60px;
31-
left: 300px;
31+
left: 400px;
3232
}
3333

3434
.PriceNameBox{
3535
@include CartNameStyle;
36-
right: 450px;
36+
right: 400px;
3737
}
3838

3939
.NumberBox{
@@ -43,7 +43,7 @@
4343

4444
.DeleteNameBox{
4545
@include CartNameStyle;
46-
right: 100px;
46+
right: 120px;
4747
}
4848

4949

@@ -56,8 +56,8 @@
5656
overflow-y: auto; /* 세로 스크롤 생성 */
5757

5858
.ItemContainer {
59-
width: 1180px;
60-
height: 90px;
59+
width: 1182px;
60+
padding-bottom: 5px;
6161
position: relative;
6262
margin-top: 20px;
6363
border-bottom: 2px solid rgb(208, 203, 185);
@@ -66,34 +66,34 @@
6666
width: 60px;
6767
height: 40px;
6868
position: absolute;
69-
top: 12px;
69+
top: 40px;
7070
left: 30px;
7171
}
7272

7373
.ImageBox{
7474
width: 100px;
75-
height: 100px;
76-
margin-left: 150px;
75+
//height: 100px;
76+
position: relative;
77+
bottom: 10px;
78+
margin-left: 120px;
79+
background-color: rebeccapurple;
7780

7881
img{
79-
position: absolute;
82+
//position: absolute;
8083
width: inherit;
81-
height: 80px;
84+
background-color: orange;
85+
height: auto;
8286
}
8387
}
8488

8589
.TextInner {
86-
width: 400px;
87-
height: 100px;
90+
width: 450px;
91+
padding-bottom: 30px;
8892
position: absolute;
89-
top: 1px;
93+
top: 50px;
9094
left: 260px;
91-
font-size: 20px;
92-
95+
9396
span:nth-child(1) {
94-
width: 400px;
95-
height: 20px;
96-
position: absolute;
9797
font-size: 20px;
9898
}
9999

@@ -107,57 +107,29 @@
107107
}
108108

109109
.PriceInner{
110-
width: 150px;
111-
height: 90px;
110+
width: auto;
111+
padding-bottom: 10px;
112+
top: 50px;
112113
position: absolute;
113-
top: -10px;
114-
right: 350px;
115-
//background-color: olivedrab;
116-
display: table-column;
114+
right: 370px;
115+
background-color: olivedrab;
117116
font-size: 15px;
118117

119-
120-
span:nth-child(1){
121-
width: 100px;
122-
height: 20px;
123-
position: absolute;
124-
}
125-
126-
span:nth-child(2){
127-
height: 30px;
128-
position: absolute;
129-
margin-top: 30px;
130-
}
131-
132-
span:nth-child(3){
133-
height: 30px;
134-
position: absolute;
135-
margin-top: 60px;
136-
}
137-
}
138-
139-
.ChooseNumber{
140-
width: 90px;
141-
height: 50px;
142-
//background-color: brown;
143-
position: absolute;
144-
right: 220px;
145-
top: 15px;
118+
146119
}
147120

148121
.CartButtonBox{
149122
width: 60px;
150-
height: 60px;
151123
position: absolute;
152-
top: 15px;
124+
top: 30px;
153125
right: 100px;
154126
//background-color: burlywood;
155127

156128
button{
157129
display: block;
158130
width: 50px;
159131
height: 30px;
160-
margin-top: 5px;
132+
margin-top: 10px;
161133
}
162134
}
163135
}

src/Components/Views/CartPage/CartItems/CartItems.tsx

+74-83
Original file line numberDiff line numberDiff line change
@@ -1,104 +1,81 @@
1-
import React from 'react';
1+
import React, { useEffect } from 'react';
22
import { useState } from 'react';
33
import Box from '@mui/material/Box';
44
import Checkbox from '@mui/material/Checkbox';
55
import FormControlLabel from '@mui/material/FormControlLabel';
66
import YearBox from 'Common/section/number';
77
import './CartItems.scss';
88

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+
}
1922

23+
const CartItems = ({ check, pitem, setItems }: CartItemsProps) => {
2024
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+
}
6538

6639
const handleChange1 = (event: React.ChangeEvent<HTMLInputElement>) => {
6740
const checkedValue = event.target.checked;
6841
const updatedCheckedItems = checkedValue
69-
? items.map((item) => item.id)
42+
? buyItem.map((item) => item.id)
7043
: [];
71-
setCheckedItems(updatedCheckedItems);
44+
setCheckedItems(updatedCheckedItems);
7245
};
7346

74-
const handleChange2 = (event: React.ChangeEvent<HTMLInputElement>) => {
47+
const handleChange2 = (el: any) => (event: React.ChangeEvent<HTMLInputElement>) => {
48+
console.log(el);
7549
const itemId = parseInt(event.target.name);
7650
let updatedCheckedItems: number[] = [];
77-
51+
let updatedItems: any[] = [];
7852
if (event.target.checked) {
7953
updatedCheckedItems = [...checkedItems, itemId];
54+
updatedItems = [...pitem , el];
8055
} else {
8156
updatedCheckedItems = checkedItems.filter((id) => id !== itemId);
82-
}
57+
updatedItems = pitem.filter((key) => key.product_no !== el.product_no);
58+
}
59+
setItems(updatedItems);
8360
setCheckedItems(updatedCheckedItems);
61+
setCheck(updatedCheckedItems);
8462
};
8563

86-
const children = (itemId: number) => (
64+
const children = (el:any ,index:number) => (
8765
<Box
8866
sx={{
8967
display: 'flex',
9068
flexDirection: 'column',
9169
ml: 3,
9270
fontSize: 'large',
93-
}}
94-
>
71+
}}>
9572
<FormControlLabel
9673
label=""
9774
control={
9875
<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}
10279
/>
10380
}
10481
/>
@@ -113,6 +90,26 @@ function CartItems() {
11390
});
11491
};
11592

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+
116113
return (
117114
<>
118115
<div className="CartPageTable__Buy">
@@ -122,20 +119,16 @@ function CartItems() {
122119
<div className="LableBox">
123120
<Checkbox
124121
size="large"
125-
checked={checkedItems.length === items.length}
122+
checked={checkedItems.length === buyItem.length}
126123
indeterminate={
127-
checkedItems.length > 0 && checkedItems.length < items.length
124+
checkedItems.length > 0 && checkedItems.length < buyItem.length
128125
}
129126
onChange={handleChange1}
130127
/>
131128
</div>
132129
}
133130
/>
134131

135-
{/* <div className="Product-Buy">
136-
<span>구매</span>
137-
</div> */}
138-
139132
<div className="ProductNameBox">
140133
<span>상품명</span>
141134
</div>
@@ -150,25 +143,23 @@ function CartItems() {
150143
</div>
151144

152145
<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" />
158152
</div>
159153
<div className="TextInner">
160-
<span>{item.name}</span>
161-
<span>{item.text}</span>
154+
<span>{el.product_name}</span>
162155
</div>
163156

164157
<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>
168159
</div>
169160

170161
<div className="CartButtonBox">
171-
<button>삭제</button>
162+
<button onClick={() => RemoveBuyItem(index, el.product_no)}>삭제</button>
172163
</div>
173164
</div>
174165
))}

0 commit comments

Comments
 (0)