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