@@ -9,6 +9,8 @@ import { Alert } from '@codegouvfr/react-dsfr/Alert';
99import MuiAlert from '@mui/material/Alert' ;
1010import Snackbar , { SnackbarCloseReason } from '@mui/material/Snackbar' ;
1111import ShuffleIcon from '@mui/icons-material/Shuffle' ;
12+ import api from '../../axios/axios' ;
13+ import { redirect , useNavigate } from 'react-router-dom' ;
1214import { fr } from '@codegouvfr/react-dsfr' ;
1315
1416interface AuthModalProps {
@@ -23,12 +25,68 @@ interface AuthModalProps {
2325 authenticated : boolean | null ;
2426 setButtons : ( a : boolean ) => void ;
2527 buttons : boolean ;
28+ conferenceNumber : number ;
29+ participantNumber : number ;
2630}
2731
2832function HomeForm ( props : AuthModalProps ) {
2933 const [ message , setMessage ] = useState < JSX . Element | string > ( < > </ > ) ;
3034 const [ messageType , setMessageType ] = useState < string > ( '' ) ;
3135 const [ open , setOpen ] = useState ( false ) ;
36+ const [ openModal , setOpenModal ] = useState ( false ) ;
37+
38+ const navigate = useNavigate ( ) ;
39+
40+ function handle ( ) {
41+ function roomNameConstraintOk ( roomName : string ) {
42+ const regex = new RegExp (
43+ '^(?=(?:[a-zA-Z0-9]*[a-zA-Z]))(?=(?:[a-zA-Z0-9]*[0-9]){3})[a-zA-Z0-9]{10,}$'
44+ ) ;
45+ return regex . test ( roomName ) ;
46+ }
47+ if ( ! props . roomName ) {
48+ const room = generateRoomName ( ) ;
49+ props . setRoomName ( room ) ;
50+ if ( roomNameConstraintOk ( room ) ) {
51+ api . get ( '/authentication/whereami' ) . then ( res => {
52+ if ( res . data . toLowerCase ( ) == 'internet' ) {
53+ if ( ! props . authenticated ) {
54+ // modal.open();
55+ setOpenModal ( true ) ;
56+ }
57+ if ( props . authenticated ) {
58+ props . joinConference ( room ) ;
59+ }
60+ }
61+ if ( res . data . toLowerCase ( ) !== 'internet' ) {
62+ props . joinConference ( room ) ;
63+ }
64+ } ) ;
65+ }
66+ } else if ( roomNameConstraintOk ( props . roomName ) ) {
67+ api
68+ . get ( '/roomExists/' + props . roomName )
69+ . then ( res => {
70+ return navigate ( '/' + props . roomName ) ;
71+ } )
72+ . catch ( err => {
73+ api . get ( '/authentication/whereami' ) . then ( res => {
74+ if ( res . data . toLowerCase ( ) == 'internet' ) {
75+ if ( ! props . authenticated ) {
76+ setOpenModal ( true ) ;
77+ }
78+ if ( props . authenticated ) {
79+ return props . joinConference ( props . roomName ) ;
80+ }
81+ }
82+ if ( res . data . toLowerCase ( ) !== 'internet' ) {
83+ return props . joinConference ( props . roomName ) ;
84+ }
85+ } ) ;
86+ } ) ;
87+ }
88+ setOpenModal ( false ) ;
89+ }
3290
3391 const change = ( e : string ) => {
3492 verifyAndSetVAlue ( e ) ;
@@ -171,7 +229,13 @@ function HomeForm(props: AuthModalProps) {
171229 < div className = { styles . HomeForm } >
172230 < h3 > La WebConférence de l'État pour tous les agents publics</ h3 >
173231 < p > Audio, vidéo, chat, partage d'écran et de documents</ p >
174- < div className = { styles . form } >
232+ < form
233+ className = { styles . form }
234+ onSubmit = { e => {
235+ e . preventDefault ( ) ;
236+ handle ( ) ;
237+ } }
238+ >
175239 < div className = { styles . confButtons } >
176240 < Input
177241 style = { { width : '100%' } }
@@ -188,30 +252,39 @@ function HomeForm(props: AuthModalProps) {
188252 onChange : ( e : any ) => change ( e . target . value ) ,
189253 } }
190254 />
255+
191256 < Button
192257 className = { styles . plusButton }
193258 onClick = { e => {
194259 e . preventDefault ( ) ;
195260 verifyAndSetVAlue ( generateRoomName ( ) ) ;
196261 } }
262+ type = "button"
197263 >
198264 < ShuffleIcon />
199265 </ Button >
200266 </ div >
201267 < div className = { styles . confButtons } >
202- < AuthModal { ...props } setOpen = { setOpen } buttons = { props . buttons } />
268+ < AuthModal
269+ { ...props }
270+ setOpen = { setOpen }
271+ buttons = { props . buttons }
272+ openModal = { openModal }
273+ />
203274 < Button
204275 className = { styles . plusButton }
205276 onClick = { ( ) => props . setButtons ( ! props . buttons ) }
206277 nativeButtonProps = { { id : 'plusButton' } }
278+ type = "button"
207279 >
208280 { props . buttons ? down : up }
209281 </ Button >
210282 </ div >
211- </ div >
283+ </ form >
212284 < p > { message } </ p >
213285 < Badge severity = "info" >
214- Actuellement, il y a 0 conférences et 0 participants.
286+ Actuellement, il y a { props . conferenceNumber } conférences et{ ' ' }
287+ { props . participantNumber } participants.
215288 </ Badge >
216289 < hr />
217290 < Alert
0 commit comments