-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.jsx
More file actions
87 lines (77 loc) · 2.66 KB
/
index.jsx
File metadata and controls
87 lines (77 loc) · 2.66 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
import React, { useState } from "react";
import {
TextField,
Dialog,
DialogTitle,
DialogContent,
DialogActions,
Button,
} from "@mui/material";
import { toast } from "react-toastify";
const ModalAdicionarFornecedor = ({
open,
onClose,
title,
}) => {
const [novoFornecedor, setNovoFornecedor] = useState('');
const adicionarNovoFornecedor = async () => {
if (!novoFornecedor.trim()) {
toast.error("O nome do fornecedor não pode ser vazio");
return;
}
console.log("Objeto enviado para API:", novoFornecedor.trim());
const novoFornecedorObj = JSON.parse(JSON.stringify({ nome: novoFornecedor.trim() }));
console.log("JSON final enviado:", JSON.stringify(novoFornecedorObj));
const token = sessionStorage.getItem('token');
if (!token) {
toast.error("Erro de autenticação");
return;
}
try {
const response = await fetch('http://50.19.70.8:80/api/fornecedores', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`
},
body: JSON.stringify(novoFornecedorObj)
});
if (!response.ok) {
const errorData = await response.json();
console.error("Erro do servidor:", errorData);
throw new Error(`Erro ao adicionar fornecedor: ${errorData.message || 'Erro desconhecido'}`);
}
onClose()
toast.success("Fornecedor adicionado com sucesso!");
} catch (error) {
console.error("Erro na requisição:", error);
toast.error("Erro ao adicionar fornecedor. Tente novamente.");
}
};
return (
<Dialog
open={open}
onClose={onClose}>
<DialogTitle>{title}</DialogTitle>
<DialogContent>
<TextField
autoFocus
margin="dense"
label="Nome da Marca"
fullWidth
value={novoFornecedor}
onChange={(e) => setNovoFornecedor(e.target.value)}
/>
</DialogContent>
<DialogActions>
<Button className="botaoModal" onClick={onClose}>
Cancelar
</Button>
<Button className="botaoModal" onClick={adicionarNovoFornecedor}>
Adicionar
</Button>
</DialogActions>
</Dialog>
);
};
export default ModalAdicionarFornecedor;