From 7033b242a5836b3d6b963c61aae7fe5ebf66186d Mon Sep 17 00:00:00 2001 From: Yasmim Rosa Date: Wed, 13 Dec 2023 12:54:18 -0300 Subject: [PATCH] matricula do estudante em uma trilha --- .../LearningPathsEnrolmentController.js | 25 +++ backend/controllers/UserControllers.js | 2 +- frontend/src/App.js | 2 +- frontend/src/contexts/auth.js | 7 +- frontend/src/pages/NewEnrolmentLP/index.js | 146 ++++++++++++++++++ frontend/src/pages/NewEnrolmentLP/styles.js | 10 ++ frontend/src/pages/Signin/index.js | 6 +- frontend/src/pages/StudentHome/index.js | 4 +- frontend/src/routes/index.js | 14 +- 9 files changed, 206 insertions(+), 10 deletions(-) create mode 100644 frontend/src/pages/NewEnrolmentLP/index.js create mode 100644 frontend/src/pages/NewEnrolmentLP/styles.js diff --git a/backend/controllers/LearningPathsEnrolmentController.js b/backend/controllers/LearningPathsEnrolmentController.js index 0cfb5e69..4f22db5d 100644 --- a/backend/controllers/LearningPathsEnrolmentController.js +++ b/backend/controllers/LearningPathsEnrolmentController.js @@ -1,4 +1,6 @@ const { LearningPathsEnrolment } = require('../models/schemas'); +const { Registration } = require('../models/schemas') + exports.studentEnrolment = async (req, res) => { const { learning_path_id, student_id } = req.body; @@ -18,4 +20,27 @@ exports.studentEnrolment = async (req, res) => { }; }); }; +} + +exports.isOpenEnrolment = async() => { + let registrationsPeriod = Registration.findAll() + + for(let res of registrationsPeriod){ + let startDate = new Date(res.start) + let endDate = new Date(res.end) + const now = new Date() + + if(startDate < now){ + await Registration.update( + { isOpen: false }, + { where: { id: registrationsPeriod.id } } + ); + }else if(startDate >= now && endDate < now){ + await Registration.update( + { isOpen: true }, + { where: { id: registrationsPeriod.id } } + ); + } + } + } \ No newline at end of file diff --git a/backend/controllers/UserControllers.js b/backend/controllers/UserControllers.js index 14ca91ba..44bb3057 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 }); + res.json({ accessToken: accessToken, superuser: user.superuser, userId: user.id}); } catch (err) { console.error(err); res.status(500).json({ error: 'Erro durante o login.' }); diff --git a/frontend/src/App.js b/frontend/src/App.js index e47c615a..5054036e 100644 --- a/frontend/src/App.js +++ b/frontend/src/App.js @@ -6,7 +6,7 @@ import GlobalStyle from "./styles/global"; const App = () => ( - {/* */} + ); diff --git a/frontend/src/contexts/auth.js b/frontend/src/contexts/auth.js index 27f57317..7e036eff 100644 --- a/frontend/src/contexts/auth.js +++ b/frontend/src/contexts/auth.js @@ -27,14 +27,17 @@ export const AuthProvider = ({ children }) => { const isSuperUser = () => { const userType = sessionStorage.getItem("superuser"); - console.log(userType) return userType } + const userId = () => { + const user = sessionStorage.getItem("user_sy"); + return user + } return ( {children} diff --git a/frontend/src/pages/NewEnrolmentLP/index.js b/frontend/src/pages/NewEnrolmentLP/index.js new file mode 100644 index 00000000..7a1b9b82 --- /dev/null +++ b/frontend/src/pages/NewEnrolmentLP/index.js @@ -0,0 +1,146 @@ +import React, { useState, useEffect } from "react"; +import { useNavigate } from "react-router-dom"; +import { useToast } from "@chakra-ui/react"; +import { Container, Flex, Text, Select, FormControl, FormLabel, Input, Button, Center } from "@chakra-ui/react"; +import Header from "../../components/Header/index.js"; +import Footer from "../../components/Footer/index.js"; +import { ChakraProvider } from "@chakra-ui/react"; +import useAuth from "../../hooks/useAuth"; +import * as C from "./styles.js"; +import { useFormik } from "formik"; +import * as yup from "yup"; +import axios from "axios"; +import ButtonCadastrar from "../../components/Button"; + + +import { Link } from "react-router-dom"; + +const NewEnrolmentLP = () => { + const { userId } = useAuth(); + const user = userId() + const [showAlert, setShowAlert] = useState(false); + + const navigate = useNavigate(); + const toast = useToast(); + const [trilhas, setTrilhas] = useState([]); + + useEffect(() => { + async function fetchTrilhas() { + try { + const response = await axios.get('http://localhost:3001/learningpath/learningpath'); // Endpoint para buscar trilhas + setTrilhas(response.data); // Define as trilhas na state 'trilhas' + } catch (error) { + console.error('Erro ao buscar trilhas:', error); + } + } + fetchTrilhas(); + }, []); + + const formik = useFormik({ + initialValues: { + learning_path_id: "", + }, + validationSchema: yup.object({ + learning_path_id: yup + .string() + .required("Uma trilha deve ser selecionada!") + }), + 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) { + 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); + toast({ + title: "Erro na matrícula.", + description: "Tente novamente mais tarde.", + status: "error", + duration: 2800, + isClosable: true, + position: "top", + }); + } + } + }) + + return ( + + +
+ +
+ + + Matricule-se em uma trilha + + +
+ + + Selecione uma trilha + + + {formik.touched.learning_path_id && formik.errors.learning_path_id && ( + + {formik.errors.learning_path_id} + + )} +
+ +
+
+
+