revite/src/context/intermediate/modals/Onboarding.tsx

79 lines
2.6 KiB
TypeScript
Raw Normal View History

import { SubmitHandler, useForm } from "react-hook-form";
2021-07-05 11:23:23 +01:00
2021-06-19 18:46:05 +01:00
import styles from "./Onboarding.module.scss";
2021-07-05 11:23:23 +01:00
import { Text } from "preact-i18n";
import { useState } from "preact/hooks";
import wideSVG from "../../../assets/wide.svg";
2021-06-19 18:46:05 +01:00
import Button from "../../../components/ui/Button";
import Preloader from "../../../components/ui/Preloader";
2021-07-05 11:23:23 +01:00
import FormField from "../../../pages/login/FormField";
import { takeError } from "../../revoltjs/util";
2021-06-19 18:46:05 +01:00
interface Props {
2021-07-05 11:25:20 +01:00
onClose: () => void;
callback: (username: string, loginAfterSuccess?: true) => Promise<void>;
2021-06-19 18:46:05 +01:00
}
interface FormInputs {
2021-07-05 11:25:20 +01:00
username: string;
}
2021-06-19 18:46:05 +01:00
export function OnboardingModal({ onClose, callback }: Props) {
2021-07-05 11:25:20 +01:00
const { handleSubmit, register } = useForm<FormInputs>();
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | undefined>(undefined);
2021-06-19 18:46:05 +01:00
2021-07-05 11:25:20 +01:00
const onSubmit: SubmitHandler<FormInputs> = ({ username }) => {
setLoading(true);
callback(username, true)
.then(onClose)
.catch((err: any) => {
setError(takeError(err));
setLoading(false);
});
};
2021-06-19 18:46:05 +01:00
2021-07-05 11:25:20 +01:00
return (
<div className={styles.onboarding}>
<div className={styles.header}>
<h1>
<Text id="app.special.modals.onboarding.welcome" />
<img src={wideSVG} />
</h1>
</div>
<div className={styles.form}>
{loading ? (
<Preloader type="spinner" />
) : (
<>
<p>
<Text id="app.special.modals.onboarding.pick" />
</p>
<form
onSubmit={
handleSubmit(
onSubmit,
) as JSX.GenericEventHandler<HTMLFormElement>
}>
<div>
<FormField
type="username"
register={register}
showOverline
error={error}
/>
</div>
<Button type="submit">
<Text id="app.special.modals.actions.continue" />
</Button>
</form>
</>
)}
</div>
<div />
</div>
);
2021-06-19 18:46:05 +01:00
}