import {NextPage} from "next";
import Router from "next/router";
import * as React from "react";

import AuthContext from "@sentrei/common/context/AuthContext";
import spaceFormType from "@sentrei/common/types/spaceFormType";
import {analytics} from "@sentrei/common/utils/firebase";
import Loader from "@sentrei/ui/components/Loader";

import SpaceForm from "@sentrei/ui/components/SpaceForm";
import SentreiAppHeader from "@sentrei/web/components/SentreiAppHeader";

const Create: NextPage = () => {
  const {user, profile} = React.useContext(AuthContext);

  React.useEffect(() => {
    analytics().setCurrentScreen("dashboard");
  }, []);

  if (user === undefined) {
    return <Loader />;
  }

  if (!user) {
    Router.push("/");
  }

  return (
    <>
      <SentreiAppHeader />
      {user && profile && (
        <SpaceForm profile={profile} type={spaceFormType.create} user={user} />
      )}
    </>
  );
};

export default Create;
