1- import React , { useRef } from "react"
1+ import React , { useRef , useState } from "react"
22import { Modal , Button , Form } from "react-bootstrap"
3+ import { WOQLClient } from "@terminusdb/terminusdb-client" ;
4+ import { localSettings } from "../../localSettings"
5+ import { formatErrorMessage } from '../hooks/hookUtils'
6+ import { Alert } from "react-bootstrap" ;
37
4- export const LoginModal = ( { showModal, setShowModal } ) => {
8+ export const LoginModal = ( { showModal, setShowModal, isCloseble } ) => {
9+ const [ errorMessage , setErrorMessage ] = useState ( false )
510 const nameRef = useRef ( null ) ;
611 const passwordRef = useRef ( null ) ;
7- const orgRef = useRef ( null ) ;
12+ // const orgRef = useRef(null);
813// to be review
914 const loading = false
10- const handleClose = ( ) => setShowModal ( false ) ;
15+ const handleClose = ( ) => {
16+ if ( setShowModal ) setShowModal ( false ) ;
17+ }
1118
1219 const changeLoggeduser = async ( ) => {
1320 const name = nameRef . current . value
1421 const password = passwordRef . current . value
15- const organization = orgRef . current . value
22+ // const organization = orgRef.current.value
1623 if ( ! name || name === "" || ! password || password === "" ) {
1724 setError ( "Team name and password are mandatory" )
1825 return
1926 } else {
20- localStorage . setItem ( "User" , name )
21- localStorage . setItem ( "Key" , password )
22- localStorage . setItem ( "Org" , organization )
23- // to be review using routing
24- const base = process . env . BASE_URL ? `/${ process . env . BASE_URL } ` : ""
25- window . location . replace ( `${ base } /${ organization } ` )
27+ const client = new WOQLClient ( localSettings . server , { user :name , key :password } )
28+ client . info ( ) . then ( response => {
29+ if ( response [ "api:info" ] && response [ "api:info" ] [ 'authority' ] === "anonymous" ) {
30+ setErrorMessage ( "Incorrect authentication information, please enter your username and password again" )
31+ } else {
32+ localStorage . setItem ( "Terminusdb-USER" , name )
33+ localStorage . setItem ( "Terminusdb-KEY" , password )
34+ //localStorage.setItem("Org",organization)
35+ // to be review using routing
36+ const base = process . env . BASE_URL ? `/${ process . env . BASE_URL } ` : ""
37+ window . location . replace ( `${ base } ` )
38+ }
39+ } ) . catch ( err => {
40+ const message = formatErrorMessage ( err )
41+ setErrorMessage ( message )
42+ } )
43+
44+
2645 }
2746 }
2847
48+ const onHide = isCloseble ? { onHide :handleClose } : { }
49+
2950
3051 //<Loading message={`Deleting Data Product ${dataProductDetails.label} ...`} type={PROGRESS_BAR_COMPONENT}/> }
31- return < Modal size = "lg" className = "modal-dialog-right" show = { showModal } onHide = { handleClose } >
52+ return < Modal size = "lg" className = "modal-dialog-right" show = { showModal } { ... onHide } >
3253 < Modal . Header >
33- < Modal . Title className = "h6" > Login</ Modal . Title >
34- < Button variant = "close" aria-label = "Close" onClick = { handleClose } />
54+ < Modal . Title className = "h6" > Login to TerminusDB </ Modal . Title >
55+ { isCloseble && < Button variant = "close" aria-label = "Close" onClick = { handleClose } /> }
3556 </ Modal . Header >
3657 < Modal . Body className = "p-5" >
37- { /* errorMessage &&
38- <Alert variant="danger" onClose={() => setError (false)} dismissible>{errorMessage}</Alert>*/ }
58+ { errorMessage &&
59+ < Alert variant = "danger" onClose = { ( ) => setErrorMessage ( false ) } dismissible > { errorMessage } </ Alert > }
3960 < Form >
4061 < Form . Group className = "mb-3" >
41- < Form . Control required
62+ < Form . Control required
63+ onBlur = { ( ) => setErrorMessage ( false ) }
4264 ref = { nameRef }
4365 id = "add_element_name"
4466 type = "text"
4567 placeholder = { `Please type the user name` } />
4668 </ Form . Group >
4769 < Form . Group className = "mb-3" >
4870 < Form . Control required
71+ onBlur = { ( ) => setErrorMessage ( false ) }
4972 ref = { passwordRef }
5073 id = "add_element_password"
5174 type = "password"
5275 placeholder = { `Please type the user password` } />
5376 </ Form . Group >
54- < Form . Group className = "mb-3" >
55- < Form . Control required
56- ref = { orgRef }
57- id = "add_element_organization"
58- type = "text"
59- placeholder = { `Please type the user organization` } />
60- </ Form . Group >
6177 </ Form >
6278 </ Modal . Body >
6379 < Modal . Footer >
@@ -66,7 +82,7 @@ export const LoginModal = ({showModal, setShowModal }) => {
6682 id = "add_element_button"
6783 variant = "info"
6884 title = { `Login With a different user` }
69- onClick = { changeLoggeduser } > { loading ? 'Loading ...' : "Connect with the User " }
85+ onClick = { changeLoggeduser } > { loading ? 'Loading ...' : "Login " }
7086 </ Button >
7187 </ Modal . Footer >
7288 </ Modal >
0 commit comments