diff --git a/src/pages/alunos/Aluno.tsx b/src/pages/alunos/Aluno.tsx new file mode 100644 index 0000000..5b93e02 --- /dev/null +++ b/src/pages/alunos/Aluno.tsx @@ -0,0 +1,195 @@ +import { ReactElement, useEffect, useState } from "react"; +import { Col, Container, Form, InputGroupProps, Row, Spinner } from "react-bootstrap"; +import { useNavigate, useParams } from "react-router-dom"; +import api from "../../services/api"; +import ModelAluno from "../../models/Aluno"; +import InputMask from "react-input-mask"; +import SelectEstados from "../../components/alunos/SelectEstados"; +import Botao from "../../components/common/Botao"; + +/** + * @description Página de Cadastro de Alunos. + * @since 25/11/2024 + * @author Lucas Ronchi <@lucas0headshot> & Manuella <@manuela.sventnickas> + */ +const Aluno = (): ReactElement => { + const { id } = useParams<{ id: string }>(); + const navigate = useNavigate(); + const [carregando, setCarregando] = useState(false); + const [formData, setFormData] = useState({ + nome: '', + dataNascimento: '', + cpf: '', + cpfResponsavel: '', + isAtivo: true, + telefone: '', + sexo: '', + deficiencia: '', + observacoes: '', + endereco: '', + estado: '', + cidade: '', + bairro: '', + rua: '', + numero: 0, + complemento: '', + }); + + useEffect(() => { + if (id) { + fetchAluno(); + } + }, [id]); + + const fetchAluno = async () => { + setCarregando(true); + + try { + const response = await api.get<{ content: ModelAluno }>(`/alunos/${id}`); + setFormData(response.data); + } catch (err) { + console.error('Erro ao realizar GET em aulas:', err); + } finally { + setCarregando(false); + } + }; + + const handleChange = (e: React.ChangeEvent) => { + const { name, value } = e.target; + setFormData({ + ...formData, + [name]: value, + }); + }; + + const handleSubmit = async (e: React.FormEvent) => { + e.preventDefault(); + try { + const endpoint = id ? `/alunos/${id}` : '/alunos/create'; + const method = id ? "put" : "post"; + + const response = await api[method](endpoint, formData); + + if ([200, 201].includes(response.status)) { + alert(`Aluno ${id ? "editado" : "cadastrado"} com sucesso!`); + navigate("/alunos"); + } + } catch (err) { + console.error("Erro ao realizar POST/PUT em aluno:", err); + } + }; + + return ( + +

{ id ? "Editar" : "Cadastrar" } Aluno

+ + {carregando ? ( +
+ +
+ ) : ( +
+ + + Nome + + + + + Data de Nascimento + + + + + CPF + + {(inputProps: InputGroupProps) => ( + + )} + + + + + CPF do Responsável + + {(inputProps: InputGroupProps) => ( + + )} + + + + + Telefone + + {(inputProps: InputGroupProps) => ( + + )} + + + + + Sexo + + + + + + + + + Deficiência + + + + + Observações + + + + + Endereço + + + + + + + Cidade + + + + + Bairro + + + + + + Rua + + + + + Número + + + + + Complemento + + + + + + +
+ } type="submit" className="btn-md" /> + } type="button" variant="secondary" onClick={() => navigate("alunos")} className="btn-md" /> +
+
+ )} +
+ ); +}; + +export default Aluno;