import React, { useEffect, useState } from 'react'; import Modal, { RequiredModalProps } from '@/components/elements/Modal'; import { Form, Formik, FormikActions } from 'formik'; import { object, string } from 'yup'; import Field from '@/components/elements/Field'; import getTwoFactorTokenUrl from '@/api/account/getTwoFactorTokenUrl'; import enableAccountTwoFactor from '@/api/account/enableAccountTwoFactor'; import FlashMessageRender from '@/components/FlashMessageRender'; import { Actions, useStoreActions } from 'easy-peasy'; import { ApplicationStore } from '@/state'; import { httpErrorToHuman } from '@/api/http'; interface Values { code: string; } export default ({ ...props }: RequiredModalProps) => { const [ token, setToken ] = useState(''); const [ loading, setLoading ] = useState(true); const updateUserData = useStoreActions((actions: Actions) => actions.user.updateUserData); const { addError, clearFlashes } = useStoreActions((actions: Actions) => actions.flashes); useEffect(() => { clearFlashes('account:two-factor'); getTwoFactorTokenUrl() .then(setToken) .catch(error => { console.error(error); }); }, []); const submit = ({ code }: Values, { setSubmitting }: FormikActions) => { clearFlashes('account:two-factor'); enableAccountTwoFactor(code) .then(() => { updateUserData({ useTotp: true }); props.onDismissed(); }) .catch(error => { console.error(error); addError({ message: httpErrorToHuman(error), key: 'account:two-factor' }); setSubmitting(false); }); }; return ( {({ isSubmitting, isValid }) => (
{!token || !token.length ? : setLoading(false)} className={'w-full h-full shadow-none rounded-0'} /> }
)}
); };