Skip to content

Commit 464f3af

Browse files
committed
2-DR2-TP2.14
1 parent 6b8a1bc commit 464f3af

File tree

3 files changed

+38
-19
lines changed
  • 2-desenvolvimento-front-end/DR2-fundamentos-de-react/DR2-TP2.00-biblioteca-crud/src

3 files changed

+38
-19
lines changed

2-desenvolvimento-front-end/DR2-fundamentos-de-react/DR2-TP2.00-biblioteca-crud/src/App.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ body {
3535
color: white;
3636
}
3737

38+
.mensagem-error {
39+
background-color: #d63838;
40+
color: white;
41+
}
42+
3843
@keyframes slideUp {
3944
from {
4045
opacity: 0;

2-desenvolvimento-front-end/DR2-fundamentos-de-react/DR2-TP2.00-biblioteca-crud/src/App.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ function App() {
5555
onUpdateBook={atualizarLivro}
5656
livroEditando={livroEditando}
5757
onCancelEdit={() => setLivroEditando(null)}
58+
mostrarMensagem={mostrarMensagem}
5859
/>
5960
<BookList
6061
livros={livros}

2-desenvolvimento-front-end/DR2-fundamentos-de-react/DR2-TP2.00-biblioteca-crud/src/components/bookform/index.jsx

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useState, useEffect } from "react";
22
import "./styles.css";
33

4-
export default function BookForm({ onAddBook, onUpdateBook, livroEditando, onCancelEdit }) {
4+
export default function BookForm({ onAddBook, onUpdateBook, livroEditando, onCancelEdit, mostrarMensagem }) {
55
const [titulo, setTitulo] = useState("");
66
const [autor, setAutor] = useState("");
77
const [ano, setAno] = useState("");
@@ -17,25 +17,38 @@ export default function BookForm({ onAddBook, onUpdateBook, livroEditando, onCan
1717
const handleSubmit = (e) => {
1818
e.preventDefault();
1919

20-
if (titulo && autor && ano) {
21-
if (livroEditando) {
22-
onUpdateBook({
23-
titulo,
24-
autor,
25-
ano: Number(ano)
26-
});
27-
} else {
28-
onAddBook({
29-
titulo,
30-
autor,
31-
ano: Number(ano)
32-
});
33-
}
34-
35-
setTitulo("");
36-
setAutor("");
37-
setAno("");
20+
if (!titulo.trim()) {
21+
mostrarMensagem('Por favor, preencha o título do livro', 'error');
22+
return;
23+
}
24+
25+
if (!autor.trim()) {
26+
mostrarMensagem('Por favor, preencha o autor do livro', 'error');
27+
return;
28+
}
29+
30+
if (!ano || ano <= 0) {
31+
mostrarMensagem('Por favor, preencha um ano válido', 'error');
32+
return;
33+
}
34+
35+
if (livroEditando) {
36+
onUpdateBook({
37+
titulo: titulo.trim(),
38+
autor: autor.trim(),
39+
ano: Number(ano)
40+
});
41+
} else {
42+
onAddBook({
43+
titulo: titulo.trim(),
44+
autor: autor.trim(),
45+
ano: Number(ano)
46+
});
3847
}
48+
49+
setTitulo("");
50+
setAutor("");
51+
setAno("");
3952
};
4053

4154
const handleCancel = () => {

0 commit comments

Comments
 (0)