-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
276 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
let electives = { schema: 'ElectivesStudents', tableName: 'tb__student_electives'} | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
const transaction = await queryInterface.sequelize.transaction(); | ||
|
||
try { | ||
await queryInterface.createTable(electives, { | ||
co_elective_student: { type: Sequelize.INTEGER, autoIncrement: true, primaryKey: true }, | ||
ds_student_id: { type: Sequelize.INTEGER, allowNull: false}, | ||
ds_name: { type: Sequelize.TEXT, allowNull: false }, | ||
}) | ||
|
||
await transaction.commit() | ||
}catch (e) { | ||
await transaction.rollback() | ||
throw e | ||
} | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
await queryInterface.dropTable(electives) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
module.exports = (sequelize, DataTypes) => { | ||
const ElectivesStudents = sequelize.define("ElectivesStudents", { | ||
id: { | ||
type: DataTypes.INTEGER, | ||
field: "co_elective_student", | ||
primaryKey: true, | ||
autoIncrement: true | ||
}, | ||
student_id: { | ||
type: DataTypes.INTEGER, | ||
field: "ds_student_id", | ||
allowNull: false | ||
}, | ||
names: { | ||
type: DataTypes.TEXT, | ||
field: "ds_name", | ||
allowNull: false | ||
} | ||
}) | ||
|
||
return ElectivesStudents; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
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, Checkbox, Button, Center, Stack, CheckboxGroup } 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, userSy } = useAuth(); | ||
const user = userId() | ||
const schoolYear = userSy() | ||
const [showAlert, setShowAlert] = useState(false); | ||
const [numElectives, setNumElectives] = useState(false); | ||
|
||
const navigate = useNavigate(); | ||
const toast = useToast(); | ||
const [eletivas, setEletivas] = useState([]); | ||
|
||
useEffect(() => { | ||
const fetchEletivas = async () => { | ||
try { | ||
const response = await axios.get( | ||
"http://localhost:3001/elective/electives" | ||
); | ||
setEletivas(response.data); | ||
} catch (error) { | ||
console.error("Erro ao buscar eletivas:", error); | ||
} | ||
}; | ||
|
||
const numElectives = async () => { | ||
if (parseInt(schoolYear) === 1) { | ||
setNumElectives(6) | ||
} else if (parseInt(schoolYear) === 2 || parseInt(schoolYear) === 3) { | ||
setNumElectives(4) | ||
} | ||
} | ||
|
||
numElectives(); | ||
fetchEletivas(); | ||
}, []); | ||
|
||
const formik = useFormik({ | ||
initialValues: { | ||
eletivas: [], | ||
}, | ||
validationSchema: yup.object({ | ||
eletivas: yup.array().test('conditionalExactCount', `É necessário selecionar exatamente ${numElectives} eletiva(s)`, function (value) { | ||
let requiredCount; | ||
|
||
if (parseInt(schoolYear) === 1) { | ||
requiredCount = 6; | ||
} else if (parseInt(schoolYear) === 2 || parseInt(schoolYear) === 3) { | ||
requiredCount = 4; | ||
} | ||
|
||
return value.length === requiredCount; | ||
}), | ||
}), | ||
onSubmit: async(values) => { | ||
try{ | ||
const response = await axios.post("http://localhost:3001/elective/matricula-eletivas", | ||
{ | ||
names: JSON.stringify(values.eletivas), | ||
student_id: parseInt(user), | ||
} | ||
) | ||
|
||
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 if(response.status === 200){ | ||
toast({ | ||
title: "Matrícula já realizada.", | ||
description: "Sua matrícula já foi realizada!", | ||
status: "error", | ||
duration: 2800, | ||
isClosable: true, | ||
position: "top", | ||
}); | ||
|
||
setShowAlert(true); | ||
setTimeout(() => { | ||
navigate("/home-student"); | ||
}, 2000); | ||
} | ||
}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 eletivas | ||
</Text> | ||
</C.titulo> | ||
</Center> | ||
<FormControl marginTop="2vh"> | ||
<FormLabel color="#243A69" fontWeight='bold'> | ||
Selecione as eletivas | ||
</FormLabel> | ||
|
||
<CheckboxGroup value={formik.values.eletivas} onChange={(values) => formik.setFieldValue("eletivas", values) | ||
}> | ||
{eletivas.map((eletiva) => | ||
<Checkbox isChecked={formik.values.eletivas.includes(eletiva.name)} color="#243A69" marginLeft="1vh" value={eletiva.name} >{eletiva.name} </Checkbox> | ||
)} | ||
</CheckboxGroup> | ||
{formik.touched.eletivas && | ||
formik.errors.eletivas && ( | ||
<Text color="red.500" fontSize="sm"> | ||
{formik.errors.eletivas} | ||
</Text> | ||
)} | ||
|
||
<Center paddingBottom={5} marginTop="3vh"> | ||
<ButtonCadastrar | ||
Text="Cadastrar" | ||
onClick={formik.handleSubmit} | ||
></ButtonCadastrar> | ||
</Center> | ||
</FormControl> | ||
</Container> | ||
<Footer /> | ||
</Flex> | ||
</ChakraProvider> | ||
|
||
); | ||
}; | ||
|
||
export default NewEnrolmentLP; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
`; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters