2022-04-07 16:08:00 +00:00
|
|
|
import React, { useContext, useState, useEffect } from "react";
|
|
|
|
import { InjectedRouter } from "react-router";
|
|
|
|
import { max } from "lodash";
|
|
|
|
|
2022-04-22 16:45:35 +00:00
|
|
|
import paths from "router/paths";
|
2022-04-07 16:08:00 +00:00
|
|
|
import { AppContext } from "context/app";
|
|
|
|
import usersAPI from "services/entities/users";
|
|
|
|
import local from "utilities/local";
|
|
|
|
|
2022-08-31 16:17:27 +00:00
|
|
|
import FlashMessage from "components/FlashMessage";
|
|
|
|
import { INotification } from "interfaces/notification";
|
2022-04-07 16:08:00 +00:00
|
|
|
// @ts-ignore
|
2022-04-22 16:45:35 +00:00
|
|
|
import RegistrationForm from "components/forms/RegistrationForm";
|
|
|
|
// @ts-ignore
|
|
|
|
import Breadcrumbs from "./Breadcrumbs";
|
|
|
|
// @ts-ignore
|
2022-04-07 16:08:00 +00:00
|
|
|
import fleetLogoText from "../../../assets/images/fleet-logo-text-white.svg";
|
|
|
|
|
2022-08-31 16:17:27 +00:00
|
|
|
const ERROR_NOTIFICATION: INotification = {
|
|
|
|
alertType: "error",
|
|
|
|
isVisible: true,
|
|
|
|
message:
|
|
|
|
"We were unable to configure Fleet. If your Fleet server is behind a proxy, please ensure the server can be reached.",
|
|
|
|
};
|
|
|
|
|
2022-04-07 16:08:00 +00:00
|
|
|
interface IRegistrationPageProps {
|
|
|
|
router: InjectedRouter;
|
|
|
|
}
|
|
|
|
|
2022-08-31 16:17:27 +00:00
|
|
|
const baseClass = "registration-page";
|
|
|
|
|
2022-04-07 16:08:00 +00:00
|
|
|
const RegistrationPage = ({ router }: IRegistrationPageProps) => {
|
|
|
|
const { currentUser, setCurrentUser, setAvailableTeams } = useContext(
|
|
|
|
AppContext
|
|
|
|
);
|
2022-08-31 16:17:27 +00:00
|
|
|
const [page, setPage] = useState(1);
|
|
|
|
const [pageProgress, setPageProgress] = useState(1);
|
|
|
|
const [showSetupError, setShowSetupError] = useState(false);
|
2022-04-07 16:08:00 +00:00
|
|
|
|
|
|
|
useEffect(() => {
|
2022-11-17 15:45:35 +00:00
|
|
|
const { DASHBOARD } = paths;
|
2022-04-07 16:08:00 +00:00
|
|
|
|
|
|
|
if (currentUser) {
|
2022-11-17 15:45:35 +00:00
|
|
|
return router.push(DASHBOARD);
|
2022-04-07 16:08:00 +00:00
|
|
|
}
|
|
|
|
}, [currentUser]);
|
|
|
|
|
|
|
|
const onNextPage = () => {
|
|
|
|
const nextPage = page + 1;
|
|
|
|
setPage(nextPage);
|
|
|
|
setPageProgress(max([nextPage, pageProgress]) || 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
const onRegistrationFormSubmit = async (formData: any) => {
|
|
|
|
const { MANAGE_HOSTS } = paths;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const { token } = await usersAPI.setup(formData);
|
|
|
|
local.setItem("auth_token", token);
|
|
|
|
|
|
|
|
const { user, available_teams } = await usersAPI.me();
|
|
|
|
setCurrentUser(user);
|
2023-03-31 17:40:14 +00:00
|
|
|
setAvailableTeams(user, available_teams);
|
2022-04-07 16:08:00 +00:00
|
|
|
return router.push(MANAGE_HOSTS);
|
2022-05-18 17:03:00 +00:00
|
|
|
} catch (error) {
|
|
|
|
setPage(1);
|
2022-08-31 16:17:27 +00:00
|
|
|
setPageProgress(1);
|
|
|
|
setShowSetupError(true);
|
2022-04-07 16:08:00 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const onSetPage = (pageNum: number) => {
|
|
|
|
if (pageNum > pageProgress) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
setPage(pageNum);
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
2022-08-31 16:17:27 +00:00
|
|
|
<div className={baseClass}>
|
2022-04-07 16:08:00 +00:00
|
|
|
<img
|
|
|
|
alt="Fleet logo"
|
|
|
|
src={fleetLogoText}
|
2022-08-31 16:17:27 +00:00
|
|
|
className={`${baseClass}__logo`}
|
2022-04-07 16:08:00 +00:00
|
|
|
/>
|
|
|
|
<Breadcrumbs
|
|
|
|
onClick={onSetPage}
|
|
|
|
page={page}
|
|
|
|
pageProgress={pageProgress}
|
|
|
|
/>
|
|
|
|
<RegistrationForm
|
|
|
|
page={page}
|
|
|
|
onNextPage={onNextPage}
|
|
|
|
onSubmit={onRegistrationFormSubmit}
|
|
|
|
/>
|
2022-08-31 16:17:27 +00:00
|
|
|
{showSetupError && (
|
|
|
|
<FlashMessage
|
|
|
|
className={`${baseClass}__flash-message`}
|
|
|
|
fullWidth={false}
|
|
|
|
notification={ERROR_NOTIFICATION}
|
|
|
|
onRemoveFlash={() => setShowSetupError(false)}
|
|
|
|
/>
|
|
|
|
)}
|
2022-04-07 16:08:00 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default RegistrationPage;
|