Skip to content

Commit ee0a503

Browse files
authored
Merge pull request #2 from future4code/erick-branchh
Projeto, com Parte do carrinho, estrutura, e funções, faltando terminar
2 parents 16e5c9f + f65a78e commit ee0a503

7 files changed

Lines changed: 170 additions & 2 deletions

File tree

src/App.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
.button-carrinho {
2+
border: none;
3+
cursor: pointer;
4+
5+
6+
}
7+
8+
9+
110
.App {
211
text-align: center;
312
}

src/App.js

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
import React from 'react';
22
import Home from './components/Home.js';
33
import './App.css';
4+
import Carrinho from './components/Carrinho';
45
import Filtro from './components/Filtro.js';
56

67
export default class App extends React.Component {
78

89
state = {
10+
carrinho: false,
11+
carrinhoItem: [],
12+
total: 0,
913
produtos: [
1014
{
1115
id: '1',
@@ -63,14 +67,54 @@ export default class App extends React.Component {
6367
inputValueText:'',
6468
filtroInput:false
6569
}
70+
71+
abreFechaCarrinho = () => {
72+
this.setState((prevState) => ({
73+
carrinho: !prevState.carrinho,
74+
})
75+
)
76+
}
77+
78+
addAcarrinho = (produto) => {
79+
const produtoAddNovo = {
80+
id: Date.now(),
81+
name: produto.name,
82+
value: produto.value,
83+
imgUrl: produto.imgUrl
84+
}
85+
86+
const totalCarrinho = this.state.total + produtoAddNovo.value;
87+
88+
const produtoAddLista = [produtoAddNovo, ...this.state.carrinhoItem]
89+
90+
this.setState({total: totalCarrinho})
91+
this.setState({carrinhoItem: produtoAddLista})
92+
}
93+
94+
removerDoCarrinho = (produtoID) => {
95+
const atualizaNovoCarrinho = this.state.carrinhoItem.filter((produto) => {
96+
return produtoID !== produto;
97+
})
98+
99+
let calculaNovoTotal = 0;
100+
calculaNovoTotal = this.state.total - produtoID.value
101+
102+
this.setState({total: calculaNovoTotal })
103+
this.setState({carrinhoItem: atualizaNovoCarrinho})
104+
105+
}
106+
66107
render(){
67108

68109
return (
69110
<div>
70111
<Home />
71112
<Filtro produtos={this.state.produtos} min={this.state.produtos.inputValueMin} max={this.state.produtos.inputValueMax} />
113+
<button class='button-carrinho' onClick={this.abreFechaCarrinho}>Carrinho</button>
114+
<div>
115+
{this.state.carrinho ? <Carrinho /> : ''}
116+
</div>
72117
</div>
73118
);
74119
}
75120
}
76-

src/components/Carrinho.css

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
.containerCarrinho{
2+
display: flex;
3+
flex-direction: column;
4+
width:200px;
5+
min-width: 100px;
6+
border: 1px solid black;
7+
padding: 10px;
8+
}
9+

src/components/Carrinho.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import React from 'react';
2+
import CarrinhoItem from './CarrinhoItem'
3+
import './Carrinho.css';
4+
5+
export default class Carrinho extends React.Component {
6+
state = {
7+
produtos: [
8+
{
9+
id: '1',
10+
name: "Nome do Produto",
11+
value: "3333",
12+
imageUrl: "https://picsum.photos/200/300?random=1"
13+
},
14+
{
15+
id: '2',
16+
name: "Nome do Produto",
17+
value: "2222",
18+
imageUrl: "https://picsum.photos/200/300?random=2"
19+
},
20+
{
21+
id: '3',
22+
name: "Nome do Produto",
23+
value: "1111",
24+
imageUrl: "https://picsum.photos/200/300?random=3"
25+
},
26+
{
27+
id: '4',
28+
name: "Nome do Produto",
29+
value: "4444",
30+
imageUrl: "https://picsum.photos/200/300?random=4"
31+
},
32+
{
33+
id: '5',
34+
name: "Nome do Produto",
35+
value: "8888",
36+
imageUrl: "https://picsum.photos/200/300?random=5"
37+
},
38+
{
39+
id: '6',
40+
name: "Nome do Produto",
41+
value: "6666",
42+
imageUrl: "https://picsum.photos/200/300?random=6"
43+
},
44+
{
45+
id: '7',
46+
name: "Nome do Produto",
47+
value: "5555",
48+
imageUrl: "https://picsum.photos/200/300?random=7"
49+
},
50+
{
51+
id: '8',
52+
name: "Nome do Produto",
53+
value: "7777",
54+
imageUrl: "https://picsum.photos/200/300?random=8"
55+
},
56+
],
57+
total: 0,
58+
}
59+
60+
61+
render() {
62+
63+
return (
64+
<div class='containerCarrinho'>
65+
66+
<h2> Carrinho: </h2>
67+
<div>
68+
<ul>
69+
<li>
70+
{this.state.produtos.map((produtoID) => (
71+
<CarrinhoItem key={produtoID.id} name={produtoID.name} value={produtoID.value}>
72+
{produtoID.name}
73+
{produtoID.value}
74+
75+
</CarrinhoItem>
76+
77+
),
78+
)}
79+
80+
</li>
81+
</ul>
82+
<p> Total: <b>R$ {this.state.total}</b> </p>
83+
</div>
84+
85+
</div>
86+
);
87+
}
88+
89+
90+
}

src/components/CarrinhoItem.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import React from 'react';
2+
3+
const CarrinhoItem = (props) => {
4+
return (
5+
<li>
6+
<span>
7+
<p>{props.name}</p>
8+
<p>{props.value}</p>
9+
</span>
10+
11+
<button>X</button>
12+
</li>
13+
)
14+
}
15+
16+
export default CarrinhoItem;

src/components/Filtro.css

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,5 @@
99

1010

1111
.inputFiltro{
12-
1312
margin:5px;
1413
}

src/components/Produtos.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ function Produtos(props) {
88
<h4>{ props.name }</h4>
99
<p>R${ props.value }</p>
1010

11+
{/* <button onClick={() => this.addAcarrinho(produto)}>Adicionar ao carrinho</button> */}
1112
<button>Adicionar ao carrinho</button>
1213
</div>
1314
);

0 commit comments

Comments
 (0)