Skip to content

Commit

Permalink
matricula do estudante em uma trilha
Browse files Browse the repository at this point in the history
  • Loading branch information
yaskisoba committed Dec 13, 2023
1 parent 431042b commit 7033b24
Show file tree
Hide file tree
Showing 9 changed files with 206 additions and 10 deletions.
25 changes: 25 additions & 0 deletions backend/controllers/LearningPathsEnrolmentController.js
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 } }
);
}
}

}
2 changes: 1 addition & 1 deletion backend/controllers/UserControllers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.' });
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import GlobalStyle from "./styles/global";
const App = () => (
<AuthProvider>
<RoutesApp />
{/* <GlobalStyle /> */}
<GlobalStyle />
</AuthProvider>
);

Expand Down
7 changes: 5 additions & 2 deletions frontend/src/contexts/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<AuthContext.Provider
value={{signin, isAuthenticated, isSuperUser}}
value={{signin, isAuthenticated, isSuperUser, userId}}
>
{children}
</AuthContext.Provider>
Expand Down
146 changes: 146 additions & 0 deletions frontend/src/pages/NewEnrolmentLP/index.js
Original file line number Diff line number Diff line change
@@ -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 (
<ChakraProvider>
<Flex direction="column" minH="100vh">
<Header />
<Container flex="1" marginTop='5vh'>
<Center>
<C.titulo>
<Text
textAlign={"center"}
fontSize={"3xl"}
color={"#243A69"}
as={"b"}
>
Matricule-se em uma trilha
</Text>
</C.titulo>
</Center>
<FormControl marginTop="2vh">
<FormLabel color="#243A69">
Selecione uma trilha
</FormLabel>
<Select
type="text"
placeholder="Selecione a trilha"
_placeholder={{ opacity: 1, color: "#243A69" }} isRequired
{...formik.getFieldProps("learning_path_id")}
>
{trilhas.map((op) => (
<option value={op.id}>{op.name}</option>
))}
</Select>
{formik.touched.learning_path_id && formik.errors.learning_path_id && (
<Text color="red.500" fontSize="sm">
{formik.errors.learning_path_id}
</Text>
)}
<Center paddingBottom={5} marginTop="3vh">
<ButtonCadastrar
Text="Cadastrar"
onClick={formik.handleSubmit}
></ButtonCadastrar>
</Center>
</FormControl>
</Container>
<Footer />
</Flex>
</ChakraProvider>

);
};

export default NewEnrolmentLP;
10 changes: 10 additions & 0 deletions frontend/src/pages/NewEnrolmentLP/styles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import styled from "styled-components";

export const titulo = styled.div`
font-size: 150%;
font-weight: 600;
color: #243A69;
margin-left: 20px;
margin-top: 20px;
align: center;
`;
6 changes: 4 additions & 2 deletions frontend/src/pages/Signin/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { Text } from '@chakra-ui/react';
import axios from 'axios';

const Signin = () => {
const { signin, isSuperUser } = useAuth();
const { signin, isSuperUser, userId} = useAuth();
const navigate = useNavigate();

const [email, setEmail] = useState("");
Expand All @@ -29,10 +29,12 @@ const Signin = () => {
if (response.data.accessToken) {
sessionStorage.setItem("accessToken", response.data.accessToken);
sessionStorage.setItem("superuser", response.data.superuser);
sessionStorage.setItem("user_id", response.data.userId);

signin();
let typeUser = isSuperUser()

let user = userId()

if(typeUser == "true"){
console.log("é adm ")
navigate("/home");
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/StudentHome/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ const StudentHome = () => {
<Center>
<HStack marginTop='2vh'>
<Button colorScheme='facebook'>
<Link to='/eletivas'>Nova matrícula</Link>
<Link to='/matricula-eletiva'>Nova matrícula em eletiva</Link>
</Button>
<Button colorScheme='facebook'>
<Link to='/trilhas'>Visualizar grade</Link>
<Link to='/matricula-trilha'>Nova matrícula em trilha</Link>
</Button>
</HStack>
</Center>
Expand Down
14 changes: 12 additions & 2 deletions frontend/src/routes/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import Recommendations from "../pages/Recommendations";
import SendStudent from "../pages/SendStudents";
import RegistrationPeriod from "../pages/RegistrationPeriod"
import StudentHome from "../pages/StudentHome"
import NewEnrolmentLP from "../pages/NewEnrolmentLP"

const RoutesApp = () => {
const { isAuthenticated, isSuperUser } = useAuth();
Expand All @@ -27,7 +28,7 @@ const RoutesApp = () => {
<Route path="/signup" element={<Signup />} />
<Route path="/" element={<Navigate to="/signin" />} />

{typeUser == 'true' ? (
{typeUser === 'true' ? (
<>
<Route
path="/home"
Expand Down Expand Up @@ -79,12 +80,21 @@ const RoutesApp = () => {
/>
</>
) : (
<Route
<>
<Route
path="/home-student"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <StudentHome />
}
/>
<Route
path="/matricula-trilha"
element={
!isAuthenticated() ? <Navigate to="/signin" /> : <NewEnrolmentLP />
}
/>
</>

)}
</Routes>
</BrowserRouter>
Expand Down

0 comments on commit 7033b24

Please sign in to comment.