parent
2029b58466
commit
04a025f850
6 changed files with 197 additions and 27 deletions
@ -0,0 +1,131 @@ |
||||
import React, { useState, useReducer } from 'react'; |
||||
import { WindowState } from "../Windows"; |
||||
import { Box, Paper, Typography, TextField, Button } from "@material-ui/core"; |
||||
import { useHistory, useLocation } from 'react-router'; |
||||
import { useAuth, Auth } from '../../../lib/useAuth'; |
||||
import Alert from '@material-ui/lab/Alert'; |
||||
import { Link } from 'react-router-dom'; |
||||
|
||||
export enum RegistrationStatus { |
||||
NoneSubmitted = 0, |
||||
Successful, |
||||
Unsuccessful, |
||||
} |
||||
|
||||
export interface RegisterWindowState extends WindowState { |
||||
email: string, |
||||
password: string, |
||||
status: RegistrationStatus, |
||||
} |
||||
export enum RegisterWindowStateActions { |
||||
SetEmail = "SetEmail", |
||||
SetPassword = "SetPassword", |
||||
SetStatus = "SetStatus", |
||||
} |
||||
export function RegisterWindowReducer(state: RegisterWindowState, action: any) { |
||||
switch (action.type) { |
||||
case RegisterWindowStateActions.SetEmail: |
||||
return { ...state, email: action.value } |
||||
case RegisterWindowStateActions.SetPassword: |
||||
return { ...state, password: action.value } |
||||
case RegisterWindowStateActions.SetStatus: |
||||
return { ...state, status: action.value } |
||||
default: |
||||
throw new Error("Unimplemented RegisterWindow state update.") |
||||
} |
||||
} |
||||
|
||||
export default function RegisterWindow(props: {}) { |
||||
const [state, dispatch] = useReducer(RegisterWindowReducer, { |
||||
email: "", |
||||
password: "", |
||||
status: RegistrationStatus.NoneSubmitted, |
||||
}); |
||||
|
||||
return <RegisterWindowControlled state={state} dispatch={dispatch} /> |
||||
} |
||||
|
||||
export function RegisterWindowControlled(props: { |
||||
state: RegisterWindowState, |
||||
dispatch: (action: any) => void, |
||||
}) { |
||||
let history: any = useHistory(); |
||||
let location: any = useLocation(); |
||||
let auth: Auth = useAuth(); |
||||
let { from } = location.state || { from: { pathname: "/" } }; |
||||
|
||||
const onSubmit = (event: any) => { |
||||
event.preventDefault(); |
||||
auth.signup(props.state.email, props.state.password) |
||||
.then(() => { |
||||
console.log("succes!") |
||||
props.dispatch({ |
||||
type: RegisterWindowStateActions.SetStatus, |
||||
value: RegistrationStatus.Successful, |
||||
}) |
||||
}).catch((e: any) => { |
||||
console.log("Fail!") |
||||
props.dispatch({ |
||||
type: RegisterWindowStateActions.SetStatus, |
||||
value: RegistrationStatus.Unsuccessful, |
||||
}) |
||||
}) |
||||
} |
||||
|
||||
return <Box width="100%" justifyContent="center" display="flex" flexWrap="wrap"> |
||||
<Box |
||||
m={1} |
||||
mt={4} |
||||
> |
||||
<Paper> |
||||
<Box p={3}> |
||||
<Typography variant="h5">Sign up</Typography> |
||||
<form noValidate onSubmit={onSubmit}> |
||||
<TextField |
||||
variant="outlined" |
||||
margin="normal" |
||||
required |
||||
fullWidth |
||||
id="email" |
||||
label="Email" |
||||
name="email" |
||||
autoFocus |
||||
onInput={(e: any) => props.dispatch({ |
||||
type: RegisterWindowStateActions.SetEmail, |
||||
value: e.target.value |
||||
})} |
||||
/> |
||||
<TextField |
||||
variant="outlined" |
||||
margin="normal" |
||||
required |
||||
fullWidth |
||||
id="password" |
||||
label="Password" |
||||
name="password" |
||||
type="password" |
||||
onInput={(e: any) => props.dispatch({ |
||||
type: RegisterWindowStateActions.SetPassword, |
||||
value: e.target.value |
||||
})} |
||||
/> |
||||
{props.state.status === RegistrationStatus.Successful && <Alert severity="success"> |
||||
Registration successful! Please {<Link to="/login">sign in</Link>} to continue. |
||||
</Alert> |
||||
} |
||||
{props.state.status === RegistrationStatus.Unsuccessful && <Alert severity="error"> |
||||
Registration failed - please check your inputs and try again. |
||||
</Alert> |
||||
} |
||||
{props.state.status !== RegistrationStatus.Successful && <Button |
||||
type="submit" |
||||
fullWidth |
||||
variant="outlined" |
||||
color="primary" |
||||
>Sign up</Button>} |
||||
</form> |
||||
</Box> |
||||
</Paper> |
||||
</Box> |
||||
</Box> |
||||
} |
Loading…
Reference in new issue