Skip to content

Commit

Permalink
abrir período de matrícula back e front
Browse files Browse the repository at this point in the history
  • Loading branch information
yaskisoba committed Dec 13, 2023
1 parent aa46648 commit 2c4d119
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 14 deletions.
8 changes: 3 additions & 5 deletions backend/controllers/SuperuserController.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
const Register = require('../models/schemas/RegistrationPeriod')
const { Registration } = require('../models/schemas')

exports.openRegistrationPeriod = async (req, res) => {
const { name, isOpen, start, end } = req.body;
const { start, end } = req.body;

await Register.create({
name: name,
isOpen: isOpen,
await Registration.create({
start: start,
end: end,
}).then(() => {
Expand Down
4 changes: 2 additions & 2 deletions backend/models/schemas/RegistrationPeriod.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = (sequelize, DataTypes) => {
const RegistrationPeriod = sequelize.define("Registration", {
const Registration = sequelize.define("Registration", {
id: {
type: DataTypes.INTEGER,
field: "co_registration_period",
Expand All @@ -24,5 +24,5 @@ module.exports = (sequelize, DataTypes) => {
}
});

return RegistrationPeriod;
return Registration;
};
125 changes: 118 additions & 7 deletions frontend/src/pages/RegistrationPeriod/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React from "react";
import React, { useState, useEffect } from "react";
import { useNavigate } from "react-router-dom";
import { Container, Flex, FormControl, FormLabel, Input, FormHelperText, Text, Center} from "@chakra-ui/react";
import ButtonCadastrar from "../../components/Button";
import { useFormik } from "formik";
Expand All @@ -9,7 +10,6 @@ import * as C from "./styles";
import Header from "../../components/Header/index.js";
import Footer from "../../components/Footer/index.js";
import { ChakraProvider } from "@chakra-ui/react";

import { Link } from "react-router-dom";


Expand All @@ -21,6 +21,87 @@ const RegistrationPeriod = () => {
const [showAlert, setShowAlert] = useState(false);
const [isLoading, setIsLoading] = useState(true);

const tenYearsFromNow = new Date();
tenYearsFromNow.setFullYear(tenYearsFromNow.getFullYear() + 10);


const formik = useFormik({
initialValues: {
startDate: "",
startTime: "",
endDate: "",
endTime: "",
},
validationSchema: yup.object({
startDate: yup
.date()
.required("A data de ínicio é obrigatória")
.min(new Date(new Date().setDate(new Date().getDate() + 1)), "A data de início deve ser pelo menos um dia após hoje")
.max(tenYearsFromNow, "A data de início não pode ser mais do que 10 anos a partir de hoje"),
startTime: yup
.string()
.required("A hora de ínicio é obrigatória"),
endDate: yup
.date()
.required("A data de término é obrigatória")
.min(
yup.ref("startDate"),
"A data de término deve ser igual ou posterior à data de início"
)
.max(tenYearsFromNow, "A data de início não pode ser mais do que 10 anos a partir de hoje"),
endTime: yup
.string()
.required("A hora de término é obrigatória")
}),
onSubmit: async (values) => {
console.log(values);
try {
const response = await axios.post(
"http://localhost:3001/registration-period/create",
{
start: values.startDate + " " + values.startTime,
end: values.endDate + " " + values.endTime,
}
);

if (response.status === 201) {
toast({
title: "Período de Matrícula Criado.",
description: "Período de matrícula criado com sucesso!",
status: "success",
duration: 2800,
isClosable: true,
position: "top",
});


setShowAlert(true);
setTimeout(() => {
navigate("/home");
}, 1000);
} else {
toast({
title: "Erro ao criar período de matrícula.",
description: response.data.message || "Erro desconhecido.",
status: "error",
duration: 2800,
isClosable: true,
position: "top",
});
}
} catch (error) {
console.error("Erro ao criar:", error);
toast({
title: "Erro ao criar período de matrícula.",
description: "Tente novamente mais tarde.",
status: "error",
duration: 2800,
isClosable: true,
position: "top",
});
}
},
});

return (
<ChakraProvider>
Expand Down Expand Up @@ -48,7 +129,14 @@ const RegistrationPeriod = () => {
isRequired
color='#243A69'
placeholder='Data de Início'
{...formik.getFieldProps("startDate")}
/>
{formik.touched.startDate &&
formik.errors.startDate && (
<Text color="red.500" fontSize="sm">
{formik.errors.startDate}
</Text>
)}

<FormLabel color= '#243A69'>Hora de Ínicio</FormLabel>
<Input
Expand All @@ -58,33 +146,56 @@ const RegistrationPeriod = () => {
isRequired
color='#243A69'
placeholder='Hora de Ínicio'
{...formik.getFieldProps("startTime")}
/>
{formik.touched.startTime &&
formik.errors.startTime && (
<Text color="red.500" fontSize="sm">
{formik.errors.startTime}
</Text>
)}

<FormLabel color= '#243A69'>Data de Fim</FormLabel>
<FormLabel color= '#243A69'>Data de Término</FormLabel>
<Input
maxLength={40}
type='date'
size='lg'
isRequired
color='#243A69'
placeholder='Data de Fim'
/>
placeholder='Data de Término'
{...formik.getFieldProps("endDate")}
/>
{formik.touched.endDate &&
formik.errors.endDate && (
<Text color="red.500" fontSize="sm">
{formik.errors.endDate}
</Text>
)}

<FormLabel color= '#243A69'>Hora de Fim</FormLabel>
<FormLabel color= '#243A69'>Hora de Término</FormLabel>
<Input
maxLength={40}
type='time'
size='lg'
isRequired
color='#243A69'
placeholder='Hora de Fim'
placeholder='Hora de Término'
{...formik.getFieldProps("endTime")}
/>
{formik.touched.endTime &&
formik.errors.endTime && (
<Text color="red.500" fontSize="sm">
{formik.errors.endTime}
</Text>

)}


</FormControl>
<Center paddingBottom={5}>
<ButtonCadastrar
Text="Cadastrar"
onClick={formik.handleSubmit}
></ButtonCadastrar>
</Center>

Expand Down

0 comments on commit 2c4d119

Please sign in to comment.