Files
pawol.nu/components/soumet/koneksyon.js
T

166 lines
4.4 KiB
JavaScript
Raw Normal View History

2021-05-22 23:38:56 +02:00
import {useEffect, useState} from 'react'
import {signIn} from 'next-auth/client'
import {useRouter} from 'next/router'
2021-05-24 13:34:56 +02:00
import Link from 'next/link'
2021-05-22 23:38:56 +02:00
import {
2021-05-24 13:34:56 +02:00
Box,
2021-05-22 23:38:56 +02:00
Button,
Container,
FormControl,
IconButton,
Input,
InputAdornment,
InputLabel,
LinearProgress,
2021-05-22 23:38:56 +02:00
Snackbar,
Typography
} from '@material-ui/core'
import {Visibility, VisibilityOff} from '@material-ui/icons'
import MuiAlert from '@material-ui/lab/Alert'
2021-05-22 23:38:56 +02:00
import {validateEmail} from '../../lib/utils/emails'
function Alert(props) {
return <MuiAlert elevation={6} variant='filled' {...props} />
}
2021-05-22 23:38:56 +02:00
function Koneksyon() {
const [loginError, setError] = useState('')
const [credentials, setCredentials] = useState({username: '', password: ''})
const [showPassword, setShowPassword] = useState(false)
const [loading, setLoading] = useState(false)
2021-05-22 23:38:56 +02:00
const [open, setOpen] = useState(true)
const router = useRouter()
const handleUpdate = update => {
setCredentials({...credentials, ...update})
}
const handleClick = async () => {
setLoading(true)
2021-05-22 23:38:56 +02:00
const response = await signIn('credentials', {
redirect: false,
...credentials
})
if (response.error) {
setError(response.error)
setLoading(false)
2021-05-22 23:38:56 +02:00
} else if (response.ok) {
setLoading(false)
router.push('/soumet')
2021-05-22 23:38:56 +02:00
}
}
const handleClose = (event, reason) => {
if (reason === 'clickaway') {
return
}
setOpen(false)
setError('')
}
useEffect(() => {
if (loginError) {
setOpen(true)
}
return () => {
setLoading(false)
}
2021-05-22 23:38:56 +02:00
}, [loginError])
const handleClickShowPassword = () => {
setShowPassword(!showPassword)
}
const handleMouseDownPassword = event => {
event.preventDefault()
}
return (
<Container maxWidth='sm'>
2021-05-24 13:33:17 +02:00
<Box align='center'>
<Typography display='inline' variant='h5' component='h1'>
Soumèt an tèks
</Typography>
<Typography>
<small>(soumettre un texte)</small>
</Typography>
</Box>
<FormControl fullWidth style={{marginTop: '3em'}} autoComplete='off'>
2021-05-22 23:38:56 +02:00
<InputLabel htmlFor='username'>Email</InputLabel>
<Input
value={credentials.username}
name='username'
type='email'
id='email'
onChange={event => handleUpdate({username: event.target.value})}
/>
</FormControl>
<FormControl fullWidth style={{marginTop: '1em'}}>
<InputLabel htmlFor='password'>Mot de passe</InputLabel>
<Input
value={credentials.password}
name='password'
type={showPassword ? 'text' : 'password'}
id='password'
endAdornment={
<InputAdornment position='end'>
<IconButton
aria-label='password visibility'
onClick={handleClickShowPassword}
onMouseDown={handleMouseDownPassword}
>
{showPassword ? <Visibility /> : <VisibilityOff />}
</IconButton>
</InputAdornment>
}
onChange={event => handleUpdate({password: event.target.value})}
/>
</FormControl>
<Button
fullWidth
disabled={loading || !validateEmail(credentials.username) || credentials.password === ''}
2021-05-24 13:34:56 +02:00
style={{marginTop: 40}}
2021-05-22 23:38:56 +02:00
variant='contained'
color='primary'
onClick={handleClick}
>
<Typography style={{fontWeight: 'bold'}}>
Koneksyon
</Typography>
</Button>
{loading && <LinearProgress size={24} style={{width: '100%', marginTop: '1em'}} />}
2021-05-24 13:34:56 +02:00
<Box align='center' marginTop={4}>
2021-05-24 19:21:22 +02:00
<Typography display='block' variant='h6' component='h2'>
Pour obtenir un accès, faites-en la demande
</Typography>
<Typography style={{fontSize: 30}} display='block'>
&#x1F4E9;
2021-05-24 13:34:56 +02:00
</Typography>
<Link passHref href='mailto:kontak@o-k-i.net?subject=Accès à soumèt'>
2021-05-24 19:21:22 +02:00
<a style={{color: 'green', fontSize: 20, textDecoration: 'none', fontWeight: 'bold'}}>kontak@o-k-i.net</a>
2021-05-24 13:34:56 +02:00
</Link>
</Box>
2021-05-22 23:38:56 +02:00
{loginError && (
<Snackbar
open={open}
autoHideDuration={6000}
onClose={handleClose}
>
<Alert severity='error' onClose={handleClose}><strong>{loginError}</strong></Alert>
2021-05-22 23:38:56 +02:00
</Snackbar>
)}
</Container>
)
}
export default Koneksyon