Select Git revision
LoginModal.js
-
Lucas Eduardo Schoenfelder authoredLucas Eduardo Schoenfelder authored
LoginModal.js 3.50 KiB
/*Copyright (C) 2019 Centro de Computacao Cientifica e Software Livre
Departamento de Informatica - Universidade Federal do Parana
This file is part of Plataforma Integrada MEC.
Plataforma Integrada MEC is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Plataforma Integrada MEC is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with Plataforma Integrada MEC. If not, see <http://www.gnu.org/licenses/>.*/
import React, {useContext, useState} from 'react';
import Modal from '@material-ui/core/Modal';
import Backdrop from '@material-ui/core/Backdrop';
import Zoom from '@material-ui/core/Fade';
import styled from 'styled-components'
import LoginContainer from './LoginContainerFunction.js'
import {Store} from '../Store.js'
import Snackbar from '@material-ui/core/Snackbar';
import MuiAlert from '@material-ui/lab/Alert';
import {authentication} from './HelperFunctions/getAxiosConfig'
const StyledLogin = styled(Modal)`
margin : 0 !important;
margin-left : 0 !important;
display : flex;
align-items: center;
justify-content : center;
text-align : center;
padding : 10px !important;
border-radius : 4px;
`
export function Alert(props) {
return <MuiAlert elevation={6} variant="filled" {...props} />;
}
export default function LoginModal (props){
const {state, dispatch} = useContext(Store)
const [snackbarOpened, handleSnackbar] = useState(false)
const handleCloseSnackbar = (event, reason) => {
if (reason === 'clickaway') {
return;
}
handleSnackbar(false);
}
function handleSuccess (data) {
dispatch ({
type: "USER_LOGGED_IN",
userLoggedIn: !state.userIsLoggedIn,
login: data.data
}
)
props.handleClose();
props.openSnackbar();
}
function handleError (error) {
handleSnackbar(true)
}
const handleLoginInfo = (login) => {
const url = `/auth/sign_in`
const payload = {
email : login.email,
password : login.senha
}
authentication(url, payload, handleSuccess, handleError)
}
return (
<>
<Snackbar open={snackbarOpened} autoHideDuration={1000} onClose={handleCloseSnackbar}
anchorOrigin = {{ vertical:'top', horizontal:'right' }}
>
<Alert severity="error">Ocorreu um erro ao se conectar!</Alert>
</Snackbar>
<StyledLogin
aria-labelledby="transition-modal-title"
aria-describedby="transition-modal-description"
open={props.open}
centered="true"
onClose={props.handleClose}
closeAfterTransition
BackdropComponent={Backdrop}
BackdropProps={{
timeout: 500,
}}
>
<Zoom in={props.open} style={{ transitionDelay :"0.2ms"}}>
<LoginContainer handleClose={props.handleClose}
openSignUp={props.openSignUp}
handleLoginInfo={handleLoginInfo}
/>
</Zoom>
</StyledLogin>
</>
)
}