import React, { useReducer } from 'react'; import { WindowState } from "../Windows"; import { Box, Paper, Typography, TextField, Button } from "@material-ui/core"; import { useHistory } 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 } export function RegisterWindowControlled(props: { state: RegisterWindowState, dispatch: (action: any) => void, }) { let history: any = useHistory(); let auth: Auth = useAuth(); const onSubmit = (event: any) => { event.preventDefault(); auth.signup(props.state.email, props.state.password) .then(() => { props.dispatch({ type: RegisterWindowStateActions.SetStatus, value: RegistrationStatus.Successful, }) }).catch((e: any) => { props.dispatch({ type: RegisterWindowStateActions.SetStatus, value: RegistrationStatus.Unsuccessful, }) }) } return Sign up
props.dispatch({ type: RegisterWindowStateActions.SetEmail, value: e.target.value })} /> props.dispatch({ type: RegisterWindowStateActions.SetPassword, value: e.target.value })} /> {props.state.status === RegistrationStatus.Successful && Registration successful! Please {sign in} to continue. } {props.state.status === RegistrationStatus.Unsuccessful && Registration failed - please check your inputs and try again. } {props.state.status !== RegistrationStatus.Successful && } Already have an account?
}