@ -1,4 +1,13 @@
import { Button , Flex , FormControl , FormLabel , Heading , Input , Select , useToast } from '@chakra-ui/react' ;
import {
Button ,
Flex ,
FormControl ,
FormLabel ,
Heading ,
Input ,
Select ,
useToast ,
} from '@chakra-ui/react' ;
import axios from 'axios' ;
import { useState } from 'react' ;
import { useDispatch } from 'react-redux' ;
@ -6,124 +15,126 @@ import { useHistory } from 'react-router-dom';
import { authLogin } from '../features/auth/authSlice' ;
function OrgSelect ( props ) {
const orgs = [ "MISI: Solidariti" , "UNDI18" ] ;
const orgOptions = orgs . map ( ( name ) => {
return (
< option value = { name } > { name } < / o p t i o n >
)
} ) ;
const orgs = [ 'MISI: Solidariti' , 'UNDI18' ] ;
const orgOptions = orgs . map ( name => {
return < option value = { name } > { name } < / o p t i o n > ;
} ) ;
return (
< Select placeholder = { props . placeholder } onChange = { props . onChange } >
{ orgOptions }
< / S e l e c t >
) ;
return (
< Select placeholder = { props . placeholder } onChange = { props . onChange } >
{ orgOptions }
< / S e l e c t >
) ;
}
function Create ( ) {
const dispatch = useDispatch ( ) ;
const toast = useToast ( ) ;
const history = useHistory ( ) ;
const dispatch = useDispatch ( ) ;
const toast = useToast ( ) ;
const history = useHistory ( ) ;
const [ name , setName ] = useState ( null ) ;
const [ email , setEmail ] = useState ( null ) ;
const [ phoneNumber , setPhoneNumber ] = useState ( null ) ;
const [ org , setOrg ] = useState ( null ) ;
const [ password , setPassword ] = useState ( null ) ;
const [ name , setName ] = useState ( null ) ;
const [ email , setEmail ] = useState ( null ) ;
const [ phoneNumber , setPhoneNumber ] = useState ( null ) ;
const [ org , setOrg ] = useState ( null ) ;
const [ password , setPassword ] = useState ( null ) ;
const handleSubmit = e => {
e . preventDefault ( ) ;
const handleSubmit = ( e ) => {
e . preventDefault ( ) ;
if ( ! name | ! email | ! phoneNumber | ! org | ! password ) {
toast ( {
title : 'Problem!' ,
description : 'Please fill in all the fields' ,
status : 'error' ,
duration : 2000 ,
isClosable : true ,
} ) ;
return ;
}
if ( ! name | ! email | ! phoneNumber | ! org | ! password ) {
toast ( {
title : 'Problem!' ,
description : 'Please fill in all the fields' ,
status : 'error' ,
duration : 2000 ,
isClosable : true ,
} ) ;
return ;
axios
. post (
` ${ process . env . REACT_APP_API_URL } /create ` ,
{
name : name ,
email : email ,
org : org ,
phoneNumber : org ,
password : password ,
} ,
{
withCredentials : true ,
}
)
. then ( response => {
if ( response . data . success ) {
dispatch ( authLogin ( ) ) ;
history . push ( '/success' ) ;
} else {
toast ( {
title : 'An error occurred' ,
description : response . data . message ,
status : 'error' ,
duration : 5000 ,
isClosable : true ,
} ) ;
}
} )
. catch ( e => {
if ( e . response . status === 401 ) {
toast ( {
title : 'Verification Error' ,
description :
"You couldn't be verified. Please try scanning the verification QR Code again." ,
status : 'error' ,
duration : 9000 ,
isClosable : true ,
} ) ;
}
} ) ;
} ;
axios
. post (
` ${ process . env . REACT_APP_API_URL } /create ` ,
{
name : name ,
email : email ,
org : org ,
phoneNumber : org ,
password : password ,
} ,
{
withCredentials : true ,
}
)
. then ( response => {
if ( response . data . success ) {
dispatch ( authLogin ( ) ) ;
history . push ( '/success' ) ;
} else {
toast ( {
title : 'An error occurred' ,
description : response . data . message ,
status : 'error' ,
duration : 5000 ,
isClosable : true ,
} ) ;
}
} )
. catch ( e => {
if ( e . response . status === 401 ) {
toast ( {
title : 'Verification Error' ,
description : "You couldn't be verified. Please try scanning the verification QR Code again." ,
status : 'error' ,
duration : 9000 ,
isClosable : true ,
} ) ;
}
} ) ;
}
return (
< Flex
height = "100vh"
background = "teal.100"
alignItems = "center"
justifyContent = "center"
>
< Flex direction = "column" background = "white" p = { 12 } rounded = { 6 } >
< Heading mb = { 6 } >
Create Account
< / H e a d i n g >
< form onSubmit = { handleSubmit } >
< FormControl mb = { 6 } >
< FormLabel > Name : < / F o r m L a b e l >
< Input onChange = { e => setName ( e . target . value ) } / >
< / F o r m C o n t r o l >
< FormControl mb = { 6 } >
< FormLabel > Email : < / F o r m L a b e l >
< Input type = "email" onChange = { e => setEmail ( e . target . value ) } / >
< / F o r m C o n t r o l >
< FormControl mb = { 6 } >
< FormLabel > Phone Number : < / F o r m L a b e l >
< Input onChange = { e => setPhoneNumber ( e . target . value ) } / >
< / F o r m C o n t r o l >
< FormControl mb = { 6 } >
< FormLabel > Organization : < / F o r m L a b e l >
< OrgSelect placeholder = "Select Organization" onChange = { e => setOrg ( e . target . value ) } / >
< / F o r m C o n t r o l >
< FormControl mb = { 6 } >
< FormLabel > Password : < / F o r m L a b e l >
< Input type = "password" onChange = { e => setPassword ( e . target . value ) } / >
< / F o r m C o n t r o l >
< Button type = "submit" > Create ! < / B u t t o n >
< / f o r m >
< / F l e x >
< / F l e x >
)
return (
< Flex
height = "100vh"
background = "teal.100"
alignItems = "center"
justifyContent = "center"
>
< Flex direction = "column" background = "white" p = { 12 } rounded = { 6 } >
< Heading mb = { 6 } > Create Account < / H e a d i n g >
< form onSubmit = { handleSubmit } >
< FormControl mb = { 6 } >
< FormLabel > Name : < / F o r m L a b e l >
< Input onChange = { e => setName ( e . target . value ) } / >
< / F o r m C o n t r o l >
< FormControl mb = { 6 } >
< FormLabel > Email : < / F o r m L a b e l >
< Input type = "email" onChange = { e => setEmail ( e . target . value ) } / >
< / F o r m C o n t r o l >
< FormControl mb = { 6 } >
< FormLabel > Phone Number : < / F o r m L a b e l >
< Input onChange = { e => setPhoneNumber ( e . target . value ) } / >
< / F o r m C o n t r o l >
< FormControl mb = { 6 } >
< FormLabel > Organization : < / F o r m L a b e l >
< OrgSelect
placeholder = "Select Organization"
onChange = { e => setOrg ( e . target . value ) }
/ >
< / F o r m C o n t r o l >
< FormControl mb = { 6 } >
< FormLabel > Password : < / F o r m L a b e l >
< Input
type = "password"
onChange = { e => setPassword ( e . target . value ) }
/ >
< / F o r m C o n t r o l >
< Button type = "submit" > Create ! < / B u t t o n >
< / f o r m >
< / F l e x >
< / F l e x >
) ;
}
export default Create ;