diff --git a/backend/controllers/UserControllers.js b/backend/controllers/UserControllers.js index 44bb3057..18d03ef6 100644 --- a/backend/controllers/UserControllers.js +++ b/backend/controllers/UserControllers.js @@ -70,7 +70,7 @@ exports.userLogin = async (req, res) => { secure: true, }); - res.json({ accessToken: accessToken, superuser: user.superuser, userId: user.id}); + res.json({ accessToken: accessToken, superuser: user.superuser, userId: user.id, sy: user.school_year}); } catch (err) { console.error(err); res.status(500).json({ error: 'Erro durante o login.' }); diff --git a/frontend/src/contexts/auth.js b/frontend/src/contexts/auth.js index 7e036eff..d7248d64 100644 --- a/frontend/src/contexts/auth.js +++ b/frontend/src/contexts/auth.js @@ -35,9 +35,14 @@ export const AuthProvider = ({ children }) => { return user } + const userSy = () => { + const user_sy = sessionStorage.getItem("sy"); + return user_sy + } + return ( {children} diff --git a/frontend/src/pages/NewEnrolmentLP/index.js b/frontend/src/pages/NewEnrolmentLP/index.js index 7a1b9b82..86b396c8 100644 --- a/frontend/src/pages/NewEnrolmentLP/index.js +++ b/frontend/src/pages/NewEnrolmentLP/index.js @@ -16,8 +16,9 @@ import ButtonCadastrar from "../../components/Button"; import { Link } from "react-router-dom"; const NewEnrolmentLP = () => { - const { userId } = useAuth(); + const { userId, userSy } = useAuth(); const user = userId() + const schoolYear = userSy() const [showAlert, setShowAlert] = useState(false); const navigate = useNavigate(); @@ -47,17 +48,11 @@ const NewEnrolmentLP = () => { }), onSubmit: async(values) => { try{ - const response = await axios.post("http://localhost:3001/learningpathenrolment/studentenrolment", - { - student_id: parseInt(user), - learning_path_id: values.learning_path_id - } - ) - if (response.status === 201) { + if(schoolYear === '1'){ toast({ - title: "Matrícula realizada.", - description: "Matrícula realizada com sucesso!", - status: "success", + title: " Sua matrícula não pode ser realizada.", + description: "Só alunos do segundo e terceiro ano podem se matricular!", + status: "error", duration: 2800, isClosable: true, position: "top", @@ -68,15 +63,39 @@ const NewEnrolmentLP = () => { setTimeout(() => { navigate("/home-student"); }, 1000); - } else { - toast({ - title: "Erro na matrícula.", - description: response.data.message || "Erro desconhecido.", - status: "error", - duration: 2800, - isClosable: true, - position: "top", - }); + }else{ + const response = await axios.post("http://localhost:3001/learningpathenrolment/studentenrolment", + { + student_id: parseInt(user), + learning_path_id: values.learning_path_id + } + ) + + if (response.status === 201) { + toast({ + title: "Matrícula realizada.", + description: "Matrícula realizada com sucesso!", + status: "success", + duration: 2800, + isClosable: true, + position: "top", + }); + + + setShowAlert(true); + setTimeout(() => { + navigate("/home-student"); + }, 1000); + } else { + toast({ + title: "Erro na matrícula.", + description: response.data.message || "Erro desconhecido.", + status: "error", + duration: 2800, + isClosable: true, + position: "top", + }); + } } }catch(error){ console.error("Erro ao cadastrar:", error); @@ -120,7 +139,7 @@ const NewEnrolmentLP = () => { {...formik.getFieldProps("learning_path_id")} > {trilhas.map((op) => ( - + ))} {formik.touched.learning_path_id && formik.errors.learning_path_id && ( diff --git a/frontend/src/pages/Signin/index.js b/frontend/src/pages/Signin/index.js index 892cdfb6..d11720d9 100644 --- a/frontend/src/pages/Signin/index.js +++ b/frontend/src/pages/Signin/index.js @@ -30,6 +30,7 @@ const Signin = () => { sessionStorage.setItem("accessToken", response.data.accessToken); sessionStorage.setItem("superuser", response.data.superuser); sessionStorage.setItem("user_id", response.data.userId); + sessionStorage.setItem("sy", response.data.sy); signin(); let typeUser = isSuperUser()