import { useEffect, useRef } from "react"; import { Box, Center, Flex, FormControl, FormErrorMessage, FormLabel, Heading, Input, Stack, useColorModeValue, useToast, } from "@chakra-ui/react"; import { useQueryClient } from "@tanstack/react-query"; import { Field, Form, Formik } from "formik"; import { createUser } from "src/api/npm"; // import logo from "src/assets/logo-256.png"; import { LocalePicker, PrettyButton, ThemeSwitcher } from "src/components"; import { useAuthState } from "src/context"; import { intl } from "src/locale"; import { validateEmail, validateString } from "src/modules/Validations"; interface Payload { name: string; email: string; password: string; } function Setup() { const toast = useToast(); const nameRef = useRef(null); const queryClient = useQueryClient(); const { login } = useAuthState(); const onSubmit = async (values: Payload, { setSubmitting }: any) => { const { password, ...payload } = { ...values, ...{ isDisabled: false, auth: { type: "local", secret: values.password, }, capabilities: ["full-admin"], }, }; const showErr = (msg: string) => { toast({ description: intl.formatMessage({ id: `error.${msg}`, }), status: "error", position: "top", duration: 3000, isClosable: true, }); }; try { const response = await createUser(payload); if (response && typeof response.id !== "undefined" && response.id) { try { await login("local", response.email, password); // Trigger a Health change await queryClient.refetchQueries({ queryKey: ["health"] }); // window.location.reload(); } catch (err: any) { showErr(err.message); } } else { showErr("cannot_create_user"); } } catch (err: any) { showErr(err.message); } setSubmitting(false); }; useEffect(() => { // @ts-expect-error ts-migrate(2531) FIXME: Object is possibly 'null'. nameRef.current.focus(); }, []); return (
Logo
{intl.formatMessage({ id: "setup.title" })} {({ isSubmitting }) => (
{({ field, form }: any) => ( {intl.formatMessage({ id: "user.name" })} {form.errors.name} )} {({ field, form }: any) => ( {intl.formatMessage({ id: "user.email" })} {form.errors.email} )} {({ field, form }: any) => ( {intl.formatMessage({ id: "user.password" })} {form.errors.password} )} {intl.formatMessage({ id: "setup.create" })}
)}
); } export default Setup;