import { action, computed, observable } from "mobx"; import { observer } from "mobx-react"; import * as React from "react"; import { Container, Dimmer, Form, Header, InputOnChangeData, Loader, Message, Segment } from "semantic-ui-react"; import { AppState, injectState } from "@client/state"; import log from "@common/logger"; import "@client/styles/LoginPage"; class LoginPageState { @observable username = ""; @observable password = ""; @observable loading: boolean = false; @observable error: string | null = null; @computed get canLogin() { return this.username.length > 0 && this.password.length > 0; } @action.bound onUsernameChange(e: any, data: InputOnChangeData) { this.username = data.value; } @action.bound onPasswordChange(e: any, data: InputOnChangeData) { this.password = data.value; } @action.bound login(appState: AppState) { this.loading = true; this.error = null; appState.httpApi .grantPassword(this.username, this.password) .then( action("loginSuccess", () => { this.loading = false; log.info("logged in"); appState.history.push("/"); }) ) .catch( action("loginError", (err: any) => { this.loading = false; this.error = err.message; log.error({ err }, "login error"); }) ); } } class LoginPage extends React.Component<{ appState: AppState }> { pageState = new LoginPageState(); render() { const { username, password, canLogin, loading, error } = this.pageState; return (
Login
{error} Login
); } login = () => { this.pageState.login(this.props.appState); }; } export default injectState(observer(LoginPage));