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'
|
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'
|
2021-05-24 03:00:14 +02:00
|
|
|
|
2022-02-06 18:42:29 +04:00
|
|
|
import {jwennTeksEpiUserId, jwennUser} from '../lib/oki-api'
|
2022-02-07 16:25:22 +04:00
|
|
|
import NewPassword from '../components/password/new-password'
|
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()
|
2021-05-24 03:00:14 +02:00
|
|
|
const [teksEpiUserId, setTeksEpiUserId] = useState([])
|
2022-02-06 18:42:29 +04:00
|
|
|
const [userId, setUserId] = useState(null)
|
|
|
|
|
const [username, setUsername] = useState(null)
|
|
|
|
|
const [open, setOpen] = useState(true)
|
|
|
|
|
|
|
|
|
|
const handleClose = (event, reason) => {
|
|
|
|
|
if (reason === 'clickaway') {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setOpen(false)
|
|
|
|
|
}
|
2021-05-24 03:00:14 +02:00
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (session && session.user) {
|
|
|
|
|
const {_id} = session.user
|
|
|
|
|
const jwennTeks = async userId => {
|
|
|
|
|
const teks = await jwennTeksEpiUserId(userId)
|
|
|
|
|
setTeksEpiUserId(teks)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
jwennTeks(_id)
|
|
|
|
|
}
|
|
|
|
|
}, [session])
|
2021-05-22 23:42:46 +02:00
|
|
|
|
2022-02-06 18:42:29 +04:00
|
|
|
useEffect(() => {
|
|
|
|
|
if (localStorage.getItem('user-id')) {
|
|
|
|
|
const userId = localStorage.getItem('user-id')
|
|
|
|
|
setUserId(userId)
|
|
|
|
|
}
|
|
|
|
|
}, [])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (userId) {
|
|
|
|
|
const getUsername = async id => {
|
|
|
|
|
const user = await jwennUser(id)
|
|
|
|
|
setUsername(user?.username)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
getUsername(userId)
|
|
|
|
|
}
|
|
|
|
|
}, [userId])
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (username && localStorage.getItem('user-id')) {
|
|
|
|
|
localStorage.removeItem('user-id')
|
|
|
|
|
}
|
|
|
|
|
}, [username])
|
|
|
|
|
|
2021-05-22 23:42:46 +02:00
|
|
|
return (
|
2022-02-07 16:20:23 +04:00
|
|
|
<HeadLayout title='Soumèt - Soumettre un texte' tab={3} slug='soumet'>
|
2022-02-07 16:25:22 +04:00
|
|
|
{!session && !code && (
|
2021-06-26 12:23:29 +02:00
|
|
|
<Koneksyon
|
|
|
|
|
chimen='/soumet'
|
|
|
|
|
/>
|
2021-05-22 23:42:46 +02:00
|
|
|
)}
|
2022-02-07 16:25:22 +04:00
|
|
|
|
|
|
|
|
{!session && code && (
|
|
|
|
|
<NewPassword code={code} />
|
|
|
|
|
)}
|
2021-05-22 23:42:46 +02:00
|
|
|
{session && session.user && (
|
2021-05-24 03:00:14 +02:00
|
|
|
<>
|
2021-06-26 12:23:29 +02:00
|
|
|
<Dekoneksyon position='absolute' top={95} left={5} chimen='/soumet' />
|
2021-05-24 03:00:14 +02:00
|
|
|
<EkriTeks session={session} teks={teksEpiUserId} />
|
|
|
|
|
</>
|
2021-05-22 23:42:46 +02:00
|
|
|
)}
|
2021-06-10 19:24:45 +02:00
|
|
|
{session && !session.user && (
|
2021-06-26 12:23:29 +02:00
|
|
|
<Dekoneksyon position='absolute' top={95} left={5} chimen='/soumet' />
|
2021-06-10 19:24:45 +02:00
|
|
|
)}
|
2022-02-06 18:42:29 +04:00
|
|
|
{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.</strong></Alert>
|
|
|
|
|
</Snackbar>
|
|
|
|
|
)}
|
2021-05-22 23:42:46 +02:00
|
|
|
</HeadLayout>
|
|
|
|
|
)
|
|
|
|
|
}
|
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
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|