-
Notifications
You must be signed in to change notification settings - Fork 1
PR de correção. Não precisa mergeear #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: correcao-projeto
Are you sure you want to change the base?
Changes from all commits
ecfe1d5
0d5b117
a062cc7
8f6df55
264fc23
acc5c24
e52978b
0a810c5
472465e
dca338b
f323d52
fd3ca7a
6aa33db
e9f7979
f6e000f
908a778
eda02a1
e07c21b
8830423
8e1836e
0be25f0
67a8558
e888d59
11272bd
b65526e
b54fbbe
87fe5a0
2a03149
3ce2f94
28efab4
5a7b7f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -125,3 +125,4 @@ dist | |
| npm-debug.log* | ||
| yarn-debug.log* | ||
| yarn-error.log* | ||
| package-lock.json | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1 @@ | ||
| Sejam bem vindos ao Labe-commerce, esse repositório contém um esqueleto de app React e um .gitignore. | ||
| http://mello-labe-commerce11.surge.sh/ |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,160 @@ | ||
| import React from 'react'; | ||
| import logo from './logo.svg'; | ||
| import './App.css'; | ||
|
|
||
| function App() { | ||
| return ( | ||
| <div className="App"> | ||
| <header className="App-header"> | ||
| <img src={logo} className="App-logo" alt="logo" /> | ||
| <p> | ||
| Edit <code>src/App.js</code> and save to reload. | ||
| </p> | ||
| <a | ||
| className="App-link" | ||
| href="https://reactjs.org" | ||
| target="_blank" | ||
| rel="noopener noreferrer" | ||
| > | ||
| Learn React | ||
| </a> | ||
| </header> | ||
| </div> | ||
| ); | ||
| import React from "react"; | ||
| import styled from "styled-components"; | ||
| import Filter from "./components/Filter/Filter"; | ||
| import Carrinho from "./components/Carrinho/Carrinho"; | ||
| import GridProdutos from "./components/GridProdutos/GridProdutos"; | ||
|
|
||
| const produtos = [ | ||
| { | ||
| id: 1, | ||
| nome: "produto 1", | ||
| valor: 10, | ||
| imgUrl: "https://picsum.photos/200/200?a=1", | ||
| }, | ||
| { | ||
| id: 2, | ||
| nome: "produto 2", | ||
| valor: 20, | ||
| imgUrl: "https://picsum.photos/200/200?a=2", | ||
| }, | ||
|
Comment on lines
+7
to
+19
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Boooa! Excelente prática isolar o array do estado fora do componente! |
||
| { | ||
| id: 3, | ||
| nome: "produto 3", | ||
| valor: 30, | ||
| imgUrl: "https://picsum.photos/200/200?a=3", | ||
| }, | ||
| { | ||
| id: 4, | ||
| nome: "produto 4", | ||
| valor: 40, | ||
| imgUrl: "https://picsum.photos/200/200?a=4", | ||
| }, | ||
| { | ||
| id: 5, | ||
| nome: "produto 5", | ||
| valor: 50, | ||
| imgUrl: "https://picsum.photos/200/200?a=5", | ||
| }, | ||
| { | ||
| id: 6, | ||
| nome: "produto 6", | ||
| valor: 60, | ||
| imgUrl: "https://picsum.photos/200/200?a=6", | ||
| }, | ||
| { | ||
| id: 7, | ||
| nome: "produto 7", | ||
| valor: 70, | ||
| imgUrl: "https://picsum.photos/200/200?a=7", | ||
| }, | ||
| { | ||
| id: 8, | ||
| nome: "produto 8", | ||
| valor: 80, | ||
| imgUrl: "https://picsum.photos/200/200?a=8", | ||
| }, | ||
| ]; | ||
|
|
||
|
|
||
| const MainContainer = styled.div` | ||
| display: grid; | ||
| grid-template-columns: 1fr 3fr 1fr; | ||
| `; | ||
|
|
||
|
|
||
|
|
||
|
Comment on lines
+58
to
+65
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pro código do componente não ficar tão grande, vcs podem isolar suas constantes do styled-componente em um arquivo separado, chamado styled.js, fazer toda a configuração CSS lá e apenas importar as constantes pro arquivo do componente. |
||
| class App extends React.Component { | ||
| state = { | ||
| produtos: produtos, | ||
| carrinho: [], | ||
| ordenacao: "crescente", | ||
| filtroMax: "", | ||
| filtroMin: "", | ||
| regex: "", | ||
| } | ||
|
|
||
| atualizarMaximo = (evento) => { | ||
| this.setState({ filtroMax: Number(evento.target.value) }) | ||
| } | ||
|
|
||
| atualizarMinimo = (evento) => { | ||
| this.setState({ filtroMin: Number(evento.target.value) }) | ||
| } | ||
|
|
||
| atualizarRegex = (evento) => { | ||
| this.setState({ regex: evento.target.value }) | ||
| } | ||
|
|
||
| filtrarProdutos = () => { | ||
| let filtrados = this.state.produtos; | ||
| if (this.state.filtroMax) { | ||
| filtrados = filtrados.filter(item => item.valor <= this.state.filtroMax) | ||
| } | ||
| if (this.state.filtroMin) { | ||
| filtrados = filtrados.filter(item => item.valor >= this.state.filtroMin) | ||
| } | ||
| if (this.state.regex) { | ||
| const regexp = new RegExp(this.state.regex, 'i') | ||
| filtrados = filtrados.filter(item => regexp.test(item.nome)) | ||
| } | ||
| return filtrados | ||
| } | ||
|
|
||
|
|
||
| adicionarAoCarrinho = (produto) => { | ||
| const novoCarrinho = [...this.state.carrinho, produto]; | ||
| this.setState({ carrinho: novoCarrinho }); | ||
| }; | ||
|
|
||
| cancelarCompra = (id) => { | ||
| const removido = this.state.carrinho.filter(compra => { | ||
| if (compra.id !== id) { | ||
| return true | ||
| } | ||
| return false | ||
| }); | ||
| this.setState({ | ||
| carrinho: removido | ||
| }) | ||
| } | ||
|
|
||
| mudarOrdenacao = (evento) => { | ||
| this.setState({ ordenacao: evento.target.value }); | ||
| }; | ||
|
|
||
| componentDidUpdate() { | ||
| localStorage.setItem('carrinho', JSON.stringify(this.state.carrinho)) | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| const carrinhoStr = localStorage.getItem('carrinho'); | ||
| if (carrinhoStr) { | ||
| const carrinhoObj = JSON.parse(carrinhoStr); | ||
| this.setState({ carrinho: carrinhoObj }); | ||
| } | ||
| }; | ||
|
|
||
| render() { | ||
| return ( | ||
| <MainContainer> | ||
| <Filter | ||
| funcaoMax={this.atualizarMaximo} | ||
| funcaoMin={this.atualizarMinimo} | ||
| funcaoRegex={this.atualizarRegex} | ||
| /> | ||
| <GridProdutos | ||
| produtos={this.filtrarProdutos()} | ||
| ordenacao={this.state.ordenacao} | ||
| funcaoAdicionar={this.adicionarAoCarrinho} | ||
| funcaoOrdenacao={this.mudarOrdenacao} | ||
| /> | ||
| <Carrinho | ||
| meuCarrinho={this.state.carrinho} | ||
| funcaoRemover={this.cancelarCompra} | ||
| /> | ||
| </MainContainer> | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| export default App; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import React from "react"; | ||
| import styled from "styled-components"; | ||
|
|
||
| const CardContainer = styled.div` | ||
| display: flex; | ||
| flex-direction: column; | ||
| padding: 5px; | ||
| border: 1px solid black; | ||
| margin-top: 5px; | ||
| `; | ||
| const Imagem = styled.img``; | ||
|
|
||
| const BotaoAdicionar = styled.button` | ||
| margin: 5px; | ||
| `; | ||
|
|
||
| const Descricao = styled.p` | ||
| margin: 5px; | ||
| ` | ||
|
|
||
| function CardProduto(props) { | ||
| const { produto, funcao } = props; | ||
|
|
||
| return ( | ||
| <CardContainer> | ||
| <Imagem src={produto.imgUrl} /> | ||
| <Descricao>{produto.nome}</Descricao> | ||
| <Descricao>R$: {produto.valor.toFixed(2)}</Descricao> | ||
| <BotaoAdicionar onClick={funcao}>Adicionar ao Carrinho</BotaoAdicionar> | ||
| </CardContainer> | ||
| ); | ||
| } | ||
|
|
||
| export default CardProduto; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import React from "react"; | ||
| import styled from "styled-components"; | ||
|
|
||
| const CarrinhoLateral = styled.div` | ||
| display: flex; | ||
| flex-direction: column; | ||
| font-family: Roboto; | ||
| border: 1px solid black; | ||
| margin: 30px 20px 0px 20px; | ||
| padding: 5px; | ||
| ` | ||
| const Title = styled.h3` | ||
| margin: 10px 5px; | ||
| ` | ||
| const Produtos = styled.div` | ||
| font-size: 20px; | ||
| ` | ||
|
|
||
| function Carrinho(props) { | ||
|
|
||
| const { meuCarrinho, funcaoRemover } = props; | ||
| const novoCarrinho = [] | ||
| let total = 0 | ||
|
|
||
| meuCarrinho.forEach(item => { | ||
|
|
||
| total += item.valor; | ||
|
|
||
| const estaNoArray = novoCarrinho.findIndex(index => index.id === item.id); | ||
| if (estaNoArray === -1) { | ||
| const produto = { | ||
| id: item.id, | ||
| nome: item.nome, | ||
| qtd: 1, | ||
| } | ||
| novoCarrinho.push(produto) | ||
| } else { | ||
| novoCarrinho[estaNoArray].qtd++; | ||
| } | ||
| }); | ||
|
|
||
| const carrinhoLateral = novoCarrinho.map((produto) => | ||
| <div> | ||
| <span>{produto.qtd}x - {produto.nome} </span> | ||
| <button onClick={() => funcaoRemover(produto.id)}>X</button> | ||
| <hr /> | ||
| </div> | ||
| ) | ||
|
|
||
| return <CarrinhoLateral> | ||
| <Title>CARRINHO</Title> | ||
| <Produtos> | ||
| <hr /> | ||
| {carrinhoLateral} | ||
| <b>Total:</b> {`R$ ${total.toFixed(2)}`} | ||
| </Produtos> | ||
| </CarrinhoLateral>; | ||
| } | ||
|
|
||
| export default Carrinho; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| import React from "react"; | ||
| import styled from "styled-components"; | ||
|
|
||
| const BarraLateral = styled.div` | ||
| display:flex; | ||
| flex-direction: column; | ||
| padding: 5px; | ||
| font-family: Roboto; | ||
| border: 1px solid black; | ||
| margin: 30px 20px 0px 20px; | ||
|
|
||
| ` | ||
| const SeçãoFiltro = styled.div` | ||
| margin: 10px 5px; | ||
| ` | ||
|
|
||
| const Title = styled.h3` | ||
| margin: 10px 5px; | ||
| ` | ||
|
|
||
|
|
||
| function Filter(props) { | ||
| const { funcaoMax, funcaoMin, funcaoRegex } = props; | ||
| return <BarraLateral> | ||
| <Title>FILTROS</Title> | ||
| <SeçãoFiltro> | ||
| <label>Valor Máximo</label> <br /> | ||
| <input type="number" onChange={funcaoMax} /> | ||
| </SeçãoFiltro> | ||
| <SeçãoFiltro> | ||
| <label>Valor Mínimo</label> <br /> | ||
| <input type="number" onChange={funcaoMin} /> | ||
| </SeçãoFiltro> | ||
| <SeçãoFiltro> | ||
| <label>Buscar Produtos</label> <br /> | ||
| <input onChange={funcaoRegex} /> | ||
| </SeçãoFiltro> | ||
| </BarraLateral>; | ||
| } | ||
|
|
||
| export default Filter; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excelente componentização!