import React, { useState, ChangeEvent } from "react"; import { createUser } from "api/npm"; import { SinglePage } from "components"; import { useAuthState, useHealthState } from "context"; import styled from "styled-components"; import { Alert, Button, Container, Form, Card } from "tabler-react"; import logo from "../../img/logo-text-vertical-grey.png"; const Wrapper = styled(Container)` margin: 15px auto; max-width: 400px; display: block; `; const LogoWrapper = styled.div` text-align: center; padding-bottom: 15px; `; function Setup() { const { refreshHealth } = useHealthState(); const { login } = useAuthState(); const [loading, setLoading] = useState(false); const [errorMessage, setErrorMessage] = useState(""); const [formData, setFormData] = useState({ name: "", nickname: "", email: "", password: "", }); const onSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); setErrorMessage(""); const { password, ...payload } = { ...formData, ...{ isDisabled: false, roles: ["admin"], auth: { type: "password", secret: formData.password, }, }, }; try { const response = await createUser({ payload }); if (response && typeof response.id !== "undefined" && response.id) { // Success, Login using creds try { await login(response.email, password); // Trigger a Health change refreshHealth(); // window.location.reload(); } catch ({ message }) { setErrorMessage(message); setLoading(false); } } else { setErrorMessage("Unable to create user!"); } } catch ({ message }) { setErrorMessage(message); } setLoading(false); }; const onChange = ({ target }: ChangeEvent) => { setFormData({ ...formData, [target.name]: target.value }); }; const formBody = ( <> Initial Setup
{errorMessage ? {errorMessage} : null}
); return ( Logo ); } export default Setup;