2022-02-06 18:42:29 +04:00
import { useState , useEffect , forwardRef } from 'react'
2022-02-07 16:25:22 +04:00
import PropTypes from 'prop-types'
2022-02-03 01:59:49 +04:00
import { useSession } from 'next-auth/react'
2022-02-06 18:42:29 +04:00
import MuiAlert from '@mui/material/Alert'
import Snackbar from '@mui/material/Snackbar'
2022-03-06 09:22:05 +04:00
import Box from '@mui/material/Box'
2021-05-24 03:00:14 +02:00
2021-05-22 23:42:46 +02:00
import HeadLayout from '../components/head-layout'
2021-06-26 12:23:29 +02:00
import Koneksyon from '../components/sesyon/koneksyon'
import Dekoneksyon from '../components/sesyon/dekoneksyon'
2021-05-24 13:05:46 +02:00
import EkriTeks from '../components/soumet/ekri-teks'
2022-03-06 09:22:05 +04:00
import Footer from '../components/footer'
2021-05-24 03:00:14 +02:00
2022-05-17 00:31:13 +04:00
import { jwennUserEpiToken , jwennUserEpiUsername } from '../lib/oki-api'
2022-02-07 16:25:22 +04:00
import NewPassword from '../components/password/new-password'
2022-03-25 00:07:27 +04:00
import ChwaTeks from '../components/soumet/chwa-teks'
2022-02-06 18:42:29 +04:00
const Alert = forwardRef ( function Alert ( props , ref ) {
return < MuiAlert ref = { ref } elevation = { 6 } variant = 'filled' { ... props } / >
} )
2021-05-22 23:42:46 +02:00
2022-02-07 16:25:22 +04:00
export default function Soumet ( { code } ) {
2022-02-03 01:59:49 +04:00
const { data : session } = useSession ( )
2022-05-17 00:31:13 +04:00
const [ localUsername , setLocalUsername ] = useState ( null )
2022-02-06 18:42:29 +04:00
const [ username , setUsername ] = useState ( null )
const [ open , setOpen ] = useState ( true )
2022-03-25 00:07:27 +04:00
const [ selectedTeks , setSelectedTeks ] = useState ( null )
2022-03-27 04:51:00 +04:00
const [ canAutoTranslate , setCanAutoTranslate ] = useState ( false )
2022-02-06 18:42:29 +04:00
const handleClose = ( event , reason ) => {
if ( reason === 'clickaway' ) {
return
}
setOpen ( false )
}
2021-05-24 03:00:14 +02:00
2022-03-27 04:51:00 +04:00
useEffect ( ( ) => {
2022-05-17 00:31:13 +04:00
if ( session ? . jwt ) {
const getUser = async token => {
const user = await jwennUserEpiToken ( token )
2022-03-27 04:51:00 +04:00
setCanAutoTranslate ( user . canAutoTranslate )
}
2022-05-17 00:31:13 +04:00
getUser ( session . jwt )
2022-03-27 04:51:00 +04:00
}
} )
2022-02-06 18:42:29 +04:00
useEffect ( ( ) => {
2022-05-17 00:31:13 +04:00
if ( localStorage . getItem ( 'username' ) ) {
const username = localStorage . getItem ( 'username' )
setLocalUsername ( username )
2022-02-06 18:42:29 +04:00
}
} , [ ] )
useEffect ( ( ) => {
2022-05-17 00:31:13 +04:00
if ( localUsername ) {
const getUser = async username => {
const user = await jwennUserEpiUsername ( username )
2022-02-06 18:42:29 +04:00
setUsername ( user ? . username )
}
2022-05-17 00:31:13 +04:00
getUser ( localUsername )
2022-02-06 18:42:29 +04:00
}
2022-05-17 00:31:13 +04:00
} , [ localUsername ] )
2022-02-06 18:42:29 +04:00
useEffect ( ( ) => {
2022-05-17 00:31:13 +04:00
if ( username && localStorage . getItem ( 'username' ) ) {
localStorage . removeItem ( 'username' )
2022-02-06 18:42:29 +04:00
}
} , [ username ] )
2021-05-22 23:42:46 +02:00
return (
2023-06-25 15:47:29 +04:00
< HeadLayout title = 'Soumèt - Soumettre un texte' summary = 'Proposez la transcription d’ un texte, accompagnée d’ une ou plusieurs traductions.' tab = { 4 } slug = 'soumet' >
2022-03-06 09:22:05 +04:00
< Box sx = { { display : 'flex' , flexDirection : 'column' , minHeight : '100vh' } } >
< Box sx = { { flexGrow : 1 , marginTop : 1 , marginBottom : 10 } } >
{ ! session && ! code && (
< Koneksyon
chimen = '/soumet'
/ >
) }
{ ! session && code && (
< NewPassword code = { code } / >
) }
{ session && session . user && (
< >
< Dekoneksyon position = 'absolute' top = { 95 } left = { 5 } chimen = '/soumet' / >
2022-03-25 00:07:27 +04:00
< ChwaTeks selectedTeks = { selectedTeks } setSelectedTeks = { setSelectedTeks } / >
2022-03-27 04:51:00 +04:00
< EkriTeks canAutoTranslate = { canAutoTranslate } selectedTeks = { selectedTeks } setSelectedTeks = { setSelectedTeks } / >
2022-03-06 09:22:05 +04:00
< / >
) }
{ session && ! session . user && (
< Dekoneksyon position = 'absolute' top = { 95 } left = { 5 } chimen = '/soumet' / >
) }
{ username && (
< Snackbar
open = { open }
autoHideDuration = { 10_000 }
onClose = { handleClose }
>
< Alert severity = 'success' onClose = { handleClose } > < strong > Bonjour { username } , votre compte a été activé avec succès . Vous pouvez vous connecter . < / s t r o n g > < / A l e r t >
< / S n a c k b a r >
) }
< / B o x >
< Footer / >
< / B o x >
2021-05-22 23:42:46 +02:00
< / H e a d L a y o u t >
)
}
2022-02-07 16:25:22 +04:00
Soumet . defaultProps = {
code : null
}
Soumet . propTypes = {
code : PropTypes . string
}
export async function getServerSideProps ( { query } ) {
const { code } = query
return {
props : {
code : code || null
}
}
}