Ready to present user settings

This commit is contained in:
Pierre HUBERT 2021-05-13 15:09:23 +02:00
parent e820c28648
commit d02d025418
2 changed files with 90 additions and 56 deletions

View File

@ -6,6 +6,8 @@
import React from "react"; import React from "react";
import { useParams } from "react-router-dom"; import { useParams } from "react-router-dom";
import { AccountHelper, AdminAccount } from "../../helpers/AccountHelper";
import { AsyncWidget } from "../widgets/AsyncWidget";
export function AccountSettingsRoute() { export function AccountSettingsRoute() {
let params: any = useParams(); let params: any = useParams();
@ -17,7 +19,9 @@ interface SettingsRouteProps {
id: number; id: number;
} }
interface SettingsRouteState {} interface SettingsRouteState {
account: AdminAccount;
}
class AccountSettingsRouteInner extends React.Component< class AccountSettingsRouteInner extends React.Component<
SettingsRouteProps, SettingsRouteProps,
@ -26,10 +30,28 @@ class AccountSettingsRouteInner extends React.Component<
constructor(props: any) { constructor(props: any) {
super(props); super(props);
this.state = {}; this.load = this.load.bind(this);
this.build = this.build.bind(this);
}
async load() {
const account = await AccountHelper.getAdminInfo(this.props.id);
this.setState({ account: account });
} }
render() { render() {
return <div>"hello {this.props.id}</div>; return (
<AsyncWidget
key={this.props.id}
errorMessage="Failed to load admin information!"
onBuild={this.build}
load={this.load}
></AsyncWidget>
);
}
build() {
return <p>{this.state.account.email}</p>;
} }
} }

View File

@ -9,73 +9,85 @@ import React, { ReactNode } from "react";
import { CenterCircularProgress } from "./CenterCircularProgress"; import { CenterCircularProgress } from "./CenterCircularProgress";
enum Status { enum Status {
LOADING, LOADING,
ERROR, ERROR,
SUCCESS SUCCESS,
} }
export interface AsyncWidgetProperties { export interface AsyncWidgetProperties {
load: () => Promise<void>, key?: number;
errorMessage: string, load: () => Promise<void>;
onBuild: () => ReactNode errorMessage: string;
onBuild: () => ReactNode;
} }
interface AsyncWidgetState { interface AsyncWidgetState {
status: Status, status: Status;
key?: number;
} }
export class AsyncWidget extends React.Component<AsyncWidgetProperties, AsyncWidgetState> { export class AsyncWidget extends React.Component<
AsyncWidgetProperties,
AsyncWidgetState
> {
constructor(props: any) {
super(props);
constructor(props: any) { this.state = {
super(props); status: Status.LOADING,
key: this.props.key,
};
this.state = { this.reload = this.reload.bind(this);
status: Status.LOADING }
}
this.reload = this.reload.bind(this); async reload() {
} try {
this.setState({ status: Status.LOADING });
async reload() { await this.props.load();
try {
this.setState({status: Status.LOADING});
await this.props.load(); this.setState({ status: Status.SUCCESS });
} catch (e) {
console.error(e);
this.setState({ status: Status.ERROR });
}
}
this.setState({status: Status.SUCCESS}); componentDidMount() {
} catch(e) { this.reload();
console.error(e); }
this.setState({status: Status.ERROR});
}
}
componentDidMount() { componentDidUpdate() {
this.reload() if (this.state.key != this.props.key) {
} this.setState({ key: this.props.key });
this.reload();
}
}
render() { render() {
if (this.state.status === Status.ERROR)
return (
<div
style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
height: "100%",
}}
>
<Paper>
<div style={{ padding: "5px 10px" }}>
{this.props.errorMessage}
&nbsp;
<Button onClick={this.reload}>Try again</Button>
</div>
</Paper>
</div>
);
if (this.state.status === Status.ERROR) if (this.state.status === Status.SUCCESS) return this.props.onBuild();
return (<div style={{
display: "flex",
alignItems: "center",
justifyContent: "center",
height: "100%",
}}>
<Paper>
<div style={{padding: "5px 10px"}}>
{this.props.errorMessage}
&nbsp; return <CenterCircularProgress></CenterCircularProgress>;
}
<Button onClick={this.reload}>Try again</Button>
</div>
</Paper>
</div>);
if(this.state.status === Status.SUCCESS)
return this.props.onBuild();
return (<CenterCircularProgress></CenterCircularProgress>)
}
} }