import { useEffect, useState } from "react";
import { getStatus } from "../../domain/apiMethods";

export const PureApiInfo = ({ status, version }) =>
  status === "healthy" ? (
    <p>API Version: {version}</p>
  ) : (
    <p>Unable to communicate with API server! Error: {status}</p>
  );

const ApiInfo = () => {
  const [data, setData] = useState(null);
  useEffect(() => {
    getStatus().then(setData);
  }, []);

  if (data === null) {
    return <p>Connecting to back-end...</p>;
  }

  const { status, version } = data;
  return <PureApiInfo status={status} version={version} />;
};

export default ApiInfo;
