import React, { 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'; export enum LoginStatus { NoneSubmitted = 0, Unsuccessful, // Note: no "successful" status because that would lead to a redirect. } export interface LoginWindowState extends WindowState { email: string, password: string, status: LoginStatus, } export enum LoginWindowStateActions { SetEmail = "SetEmail", SetPassword = "SetPassword", SetStatus = "SetStatus", } export function LoginWindowReducer(state: LoginWindowState, action: any) { switch (action.type) { case LoginWindowStateActions.SetEmail: return { ...state, email: action.value } case LoginWindowStateActions.SetPassword: return { ...state, password: action.value } case LoginWindowStateActions.SetStatus: return { ...state, status: action.value } default: throw new Error("Unimplemented LoginWindow state update.") } } export default function LoginWindow(props: {}) { const [state, dispatch] = useReducer(LoginWindowReducer, { email: "", password: "", status: LoginStatus.NoneSubmitted, }); return } export function LoginWindowControlled(props: { state: LoginWindowState, 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.signin(props.state.email, props.state.password) .then(() => { history.replace(from); }).catch((e: any) => { props.dispatch({ type: LoginWindowStateActions.SetStatus, value: LoginStatus.Unsuccessful, }) }) } return Sign in
props.dispatch({ type: LoginWindowStateActions.SetEmail, value: e.target.value })} /> props.dispatch({ type: LoginWindowStateActions.SetPassword, value: e.target.value })} /> {props.state.status === LoginStatus.Unsuccessful && Login failed - Please check your credentials. } Need an account?
}