Skip to content

Commit 834334b

Browse files
committed
2-DR2-AT.06
1 parent 742d1cf commit 834334b

File tree

4 files changed

+115
-8
lines changed

4 files changed

+115
-8
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif
3+
}
Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,55 @@
1+
import { useState, useEffect } from 'react';
2+
import { useParams } from 'react-router-dom';
3+
import axios from 'axios';
14
import style from './style.module.css';
25

36
export default function ProductDetail() {
7+
const { id } = useParams();
8+
const [produto, setProduto] = useState(null);
9+
const [carregando, setCarregando] = useState(true);
10+
11+
useEffect(() => {
12+
const buscarProduto = async () => {
13+
try {
14+
setCarregando(true);
15+
const response = await axios.get(`https://dummyjson.com/products/${id}`);
16+
setProduto(response.data);
17+
} catch (error) {
18+
new Error(error);
19+
} finally {
20+
setCarregando(false);
21+
}
22+
};
23+
buscarProduto();
24+
}, [id]);
25+
26+
if (carregando) {
27+
return <div className={style.carregando}>Carregando produto...</div>;
28+
}
29+
430
return (
5-
<div>ProductDetail</div>
31+
<div className={style.container}>
32+
<h1>{produto.title}</h1>
33+
34+
<div className={style.imagens}>
35+
{produto.images?.map((imagem, index) => (
36+
<img
37+
key={index}
38+
src={imagem}
39+
alt={`${produto.title} - ${index + 1}`}
40+
className={style.imagem}
41+
/>
42+
))}
43+
</div>
44+
45+
<div className={style.info}>
46+
<p className={style.descricao}>{produto.description}</p>
47+
<p className={style.preco}>Preço: R$ {produto.price?.toString().replace(".", ",")}</p>
48+
<p>Marca: {produto.brand}</p>
49+
<p>Categoria: {produto.category}</p>
50+
<p>Avaliação: {produto.rating}</p>
51+
<p>Em estoque: {produto.stock} unidades</p>
52+
</div>
53+
</div>
654
);
755
}

2-desenvolvimento-front-end/DR2-fundamentos-de-react/DR2-AT-ecommerce-dummy-crud/src/pages/product-list/index.jsx

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
import { useState, useEffect } from 'react';
2+
import { Link } from 'react-router-dom';
23
import axios from 'axios';
34
import style from './style.module.css';
45

56
export default function ProductList() {
67
const [produtos, setProdutos] = useState([]);
8+
const [carregando, setCarregando] = useState(true);
79

810
useEffect(() => {
911
const buscaProdutos = async () => {
1012
try {
1113
const response = await axios.get('https://dummyjson.com/products');
12-
setProdutos(response.data.products);
14+
setProdutos(response.data.products);
1315
} catch (error) {
1416
new Error(error);
17+
} finally {
18+
setCarregando(false);
1519
}
1620
};
1721
buscaProdutos();
1822
}, []);
1923

24+
if (carregando) {
25+
return <div className={style.carregando}>Carregando produtos...</div>;
26+
}
27+
2028
return (
2129
<div className={style.lista}>
22-
{ produtos.length
23-
? produtos.map((produto) => (
30+
{ produtos.map((produto) => (
2431
<div key={produto.id} className={style.produto}>
25-
<h3>{produto.title}</h3>
26-
<p>{produto.description}</p>
27-
<p>Preço: R$ {produto.price}</p>
32+
<img src={produto.thumbnail} alt={produto.title} className={style.thumbnail} />
33+
<h3>
34+
<Link to={`/produtos/${produto.id}`}>{produto.title}</Link>
35+
</h3>
36+
<p className={style.preco}>Preço: R$ {produto.price.toString().replace(".", ",")}</p>
2837
</div>
2938
))
30-
: <p>Carregando produtos...</p>
3139
}
3240
</div>
3341
);
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
.lista {
2+
display: grid;
3+
grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
4+
gap: 20px;
5+
padding: 20px;
6+
}
7+
8+
.produto {
9+
background: white;
10+
border: 1px solid #e0e0e0;
11+
border-radius: 8px;
12+
padding: 16px;
13+
transition: transform 0.2s, box-shadow 0.2s;
14+
display: flex;
15+
flex-direction: column;
16+
gap: 8px;
17+
}
18+
19+
.thumbnail {
20+
width: 100%;
21+
height: 200px;
22+
object-fit: cover;
23+
border-radius: 4px;
24+
margin-bottom: 12px;
25+
}
26+
27+
.produto {
28+
h3 {
29+
font-size: 1.2rem;
30+
color: #333;
31+
}
32+
33+
p {
34+
font-size: 1rem;
35+
}
36+
37+
.preco {
38+
font-weight: bold;
39+
font-size: 0.9rem;
40+
margin-top: auto;
41+
}
42+
}
43+
44+
.carregando {
45+
grid-column: 1 / -1;
46+
text-align: center;
47+
padding: 20px;
48+
}

0 commit comments

Comments
 (0)