import React, { useRef } from "react";
import { AsyncWidget } from "../widgets/AsyncWidget";
import { User, UserApi } from "../api/UserApi";

export function ProfileRoute(): React.ReactElement {
  const [user, setUser] = React.useState<null | User>(null);

  const load = async () => {
    setUser(await UserApi.GetUserInfo());
  };

  const counter = useRef(0);

  return (
    <AsyncWidget
      loadKey={counter.current}
      load={load}
      errMsg="Echec du chargement des informations du compte utilisateur !"
      build={() => (
        <>
          <p>ready !!! {user!.name}</p>
        </>
      )}
    />
  );
}
