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