-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathindex.jsx
More file actions
142 lines (129 loc) · 4.01 KB
/
index.jsx
File metadata and controls
142 lines (129 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import React, { useEffect, useState } from "react";
import { Button } from "vtex.styleguide";
import { NumericStepper } from "vtex.styleguide";
import styles from "../../styles/components/ButtonAddtoCart/styles.css";
import { useOrderItems } from "vtex.order-items/OrderItems";
const ButtonAddtoCart = ({ valor }) => {
const [addItem, setAddItem] = useState({ isLoading2: false });
const [numericStepper, setNumericStepper] = useState({
quantityNumericStepper: 1,
});
const [showNumeriStepper, setShowNumericStepper] = useState(false);
const [showButton, setShowButton] = useState(true);
const [label, setLabel] = useState({ labelText: `Item` });
const [productDetailsItem, setProductDetailsItem] = useState({});
console.log("🚀 ~ ButtonAddtoCart ~ productDetailsItem:", productDetailsItem);
const selectedItemQuantity = numericStepper.quantityNumericStepper;
useEffect(() => {
if (showNumeriStepper === true) {
setAddItem({ isLoading2: false });
setShowButton(false);
}
}, [showNumeriStepper]);
// controle de exibição do btn add
useEffect(() => {
if (numericStepper.quantityNumericStepper === 0) {
setShowButton(true);
setShowNumericStepper(false);
setNumericStepper({ quantityNumericStepper: 1 });
}
}, [numericStepper.quantityNumericStepper]);
// modifica texto do label ao add quantidade de item > 1
useEffect(() => {
const itemsText = "Items";
if (numericStepper.quantityNumericStepper > 1) {
setLabel({ labelText: `${itemsText}` });
} else if (numericStepper.quantityNumericStepper <= 0) {
setLabel({ labelText: `Item` });
}
}, [numericStepper.quantityNumericStepper]);
// add item
const { addItems } = useOrderItems();
const skuItem = [
{
id: valor[0]?.itemId,
seller: "1",
quantity: 1,
},
];
const addToCartItem = async () => {
try {
await addItems(skuItem);
} catch (error) {
console.error("erro ao add produti", error);
}
};
// remover item
const skuItemRemove = [
{
id: valor[0]?.itemId,
seller: "1",
quantity: -1,
},
];
const removerCartItem = async () => {
try {
await addItems(skuItemRemove);
} catch (error) {
console.error("erro ao add produti", error);
}
};
// precisa tratar update quantity
const skuItemUpdate = [
{
id: valor[0]?.itemId,
seller: "1",
quantity: +1,
},
];
const updateCartItems = async() => {
try{
await addItem(skuItemUpdate)
}catch (error) {
console.error("erro ao add produti", error);
}
}
return (
<>
<div className="col-xs-12 col-sm-6 col-md-8">
<div className={styles.content_btnAddToCart_custom}>
{showButton && (
<div className="mt4">
<Button
variation="primary"
onClick={() => {
setAddItem({ isLoading2: !addItem.isLoading2 });
setShowNumericStepper(true);
setProductDetailsItem({ ...valor, selectedItemQuantity });
addToCartItem();
}}
isLoading={addItem.isLoading2}
>
ADICIONAR AO CARRINHO
</Button>
</div>
)}
</div>
</div>
{showNumeriStepper && (
<div className="mb5">
<div className={styles.content_btnAddToCart_custom}>
<NumericStepper
label={`${label.labelText} add ao carrinho ${numericStepper.quantityNumericStepper}`}
// minValue={1}
defaultValue={0}
maxValue={50}
lean
value={numericStepper.quantityNumericStepper}
onChange={(event) =>
setNumericStepper({ quantityNumericStepper: event.value })
}
onClick={numericStepper.quantityNumericStepper < 1 ? removerCartItem() : updateCartItems()}
/>
</div>
</div>
)}
</>
);
};
export default ButtonAddtoCart;