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

@ -11,27 +11,32 @@ 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) { constructor(props: any) {
super(props); super(props);
this.state = { this.state = {
status: Status.LOADING status: Status.LOADING,
} key: this.props.key,
};
this.reload = this.reload.bind(this); this.reload = this.reload.bind(this);
} }
@ -50,32 +55,39 @@ export class AsyncWidget extends React.Component<AsyncWidgetProperties, AsyncWid
} }
componentDidMount() { componentDidMount() {
this.reload() this.reload();
}
componentDidUpdate() {
if (this.state.key != this.props.key) {
this.setState({ key: this.props.key });
this.reload();
}
} }
render() { render() {
if (this.state.status === Status.ERROR) if (this.state.status === Status.ERROR)
return (<div style={{ return (
<div
style={{
display: "flex", display: "flex",
alignItems: "center", alignItems: "center",
justifyContent: "center", justifyContent: "center",
height: "100%", height: "100%",
}}> }}
>
<Paper> <Paper>
<div style={{ padding: "5px 10px" }}> <div style={{ padding: "5px 10px" }}>
{this.props.errorMessage} {this.props.errorMessage}
&nbsp; &nbsp;
<Button onClick={this.reload}>Try again</Button> <Button onClick={this.reload}>Try again</Button>
</div> </div>
</Paper> </Paper>
</div>); </div>
);
if(this.state.status === Status.SUCCESS) if (this.state.status === Status.SUCCESS) return this.props.onBuild();
return this.props.onBuild();
return (<CenterCircularProgress></CenterCircularProgress>) return <CenterCircularProgress></CenterCircularProgress>;
} }
} }