From 550d026061a7fbd623b2aad8736c0a00200f67f3 Mon Sep 17 00:00:00 2001 From: Manav Rathi Date: Tue, 10 Jun 2025 15:17:59 +0530 Subject: [PATCH] Update --- .../accounts/components/SignUpContents.tsx | 194 +++++++++--------- .../new/photos/components/DeleteAccount.tsx | 20 +- .../new/photos/components/DevSettings.tsx | 21 +- 3 files changed, 116 insertions(+), 119 deletions(-) diff --git a/web/packages/accounts/components/SignUpContents.tsx b/web/packages/accounts/components/SignUpContents.tsx index 6f74f89713..b144323d5a 100644 --- a/web/packages/accounts/components/SignUpContents.tsx +++ b/web/packages/accounts/components/SignUpContents.tsx @@ -35,7 +35,7 @@ import { setJustSignedUp, setLocalReferralSource, } from "ente-shared/storage/localStorage/helpers"; -import { useFormik, type FormikHelpers } from "formik"; +import { useFormik } from "formik"; import { t } from "i18next"; import type { NextRouter } from "next/router"; import React, { useCallback, useState } from "react"; @@ -47,13 +47,6 @@ import { AccountsPageTitle, } from "./layouts/centered-paper"; -interface FormValues { - email: string; - passphrase: string; - confirm: string; - referral: string; -} - interface SignUpContentsProps { router: NextRouter; /** Called when the user clicks the login option instead. */ @@ -76,104 +69,108 @@ export const SignUpContents: React.FC = ({ [], ); - const registerUser = async ( - { email, passphrase, confirm, referral }: FormValues, - { setFieldError }: FormikHelpers, - ) => { - try { - if (passphrase !== confirm) { - setFieldError("confirm", t("password_mismatch_error")); - return; - } - setLoading(true); - try { - setLocalReferralSource(referral); - await sendOTT(email, "signup"); - await setLSUser({ email }); - } catch (e) { - log.error("Signup failed", e); - if ( - await isMuseumHTTPError(e, 409, "USER_ALREADY_REGISTERED") - ) { - setFieldError("email", t("email_already_registered")); - } else { - setFieldError("email", t("generic_error")); - } - throw e; - } - try { - const { masterKey, kek, keyAttributes } = - await generateKeysAndAttributes(passphrase); - - const srpSetupAttributes = await generateSRPSetupAttributes( - await deriveSRPPassword(kek), - ); - - setData("originalKeyAttributes", keyAttributes); - setData("srpSetupAttributes", srpSetupAttributes); - await generateAndSaveInteractiveKeyAttributes( - passphrase, - keyAttributes, - masterKey, - ); - - await saveMasterKeyInSessionAndSafeStore(masterKey); - setJustSignedUp(true); - void router.push("/verify"); - } catch (e) { - setFieldError("confirm", t("password_generation_failed")); - throw e; - } - } catch (e) { - log.error("signup failed", e); - } - setLoading(false); - }; - - const { values, errors, handleChange, handleSubmit } = useFormik({ - initialValues: { email: "", passphrase: "", confirm: "", referral: "" }, + const formik = useFormik({ + initialValues: { + email: "", + password: "", + confirmPassword: "", + referral: "", + }, validationSchema: Yup.object().shape({ email: Yup.string() .email(t("invalid_email_error")) .required(t("required")), - passphrase: Yup.string().required(t("required")), - confirm: Yup.string().required(t("required")), + password: Yup.string().required(t("required")), + confirmPassword: Yup.string().required(t("required")), }), validateOnChange: false, validateOnBlur: false, - onSubmit: registerUser, + onSubmit: async ( + { email, password, confirmPassword, referral }, + { setFieldError }, + ) => { + try { + if (password != confirmPassword) { + setFieldError("confirm", t("password_mismatch_error")); + return; + } + setLoading(true); + try { + setLocalReferralSource(referral); + await sendOTT(email, "signup"); + await setLSUser({ email }); + } catch (e) { + log.error("Signup failed", e); + if ( + await isMuseumHTTPError( + e, + 409, + "USER_ALREADY_REGISTERED", + ) + ) { + setFieldError("email", t("email_already_registered")); + } else { + setFieldError("email", t("generic_error")); + } + throw e; + } + try { + const { masterKey, kek, keyAttributes } = + await generateKeysAndAttributes(password); + + const srpSetupAttributes = await generateSRPSetupAttributes( + await deriveSRPPassword(kek), + ); + + setData("originalKeyAttributes", keyAttributes); + setData("srpSetupAttributes", srpSetupAttributes); + await generateAndSaveInteractiveKeyAttributes( + password, + keyAttributes, + masterKey, + ); + + await saveMasterKeyInSessionAndSafeStore(masterKey); + setJustSignedUp(true); + void router.push("/verify"); + } catch (e) { + setFieldError("confirm", t("password_generation_failed")); + throw e; + } + } catch (e) { + log.error("signup failed", e); + } + setLoading(false); + }, }); const form = ( -
+ - - + = ({ }, }} /> - - - + = ({ = ({ color="accent" type="submit" loading={loading} - disabled={!acceptTerms || isWeakPassword(values.passphrase)} + disabled={ + !acceptTerms || isWeakPassword(formik.values.password) + } > {t("create_account")} diff --git a/web/packages/new/photos/components/DeleteAccount.tsx b/web/packages/new/photos/components/DeleteAccount.tsx index f5f5df71b5..dea30d9c40 100644 --- a/web/packages/new/photos/components/DeleteAccount.tsx +++ b/web/packages/new/photos/components/DeleteAccount.tsx @@ -56,7 +56,7 @@ const DeleteAccountDialogContents: React.FC< const [acceptDataDeletion, setAcceptDataDeletion] = useState(false); const [loading, setLoading] = useState(false); - const { values, touched, errors, handleChange, handleSubmit } = useFormik({ + const formik = useFormik({ initialValues: { reason: "", feedback: "" }, validate: ({ reason, feedback }) => { if (!reason) return { reason: t("required") }; @@ -144,30 +144,32 @@ const DeleteAccountDialogContents: React.FC< }; return ( - + {t("delete_account_reason_label")} - {touched.reason && errors.reason && ( + {formik.touched.reason && formik.errors.reason && ( - {errors.reason} + {formik.errors.reason} )} = ({ initialAPIOrigin, onClose }) => { - const form = useFormik({ + const formik = useFormik({ initialValues: { apiOrigin: initialAPIOrigin }, validate: ({ apiOrigin }) => { try { @@ -121,12 +121,12 @@ const Form: React.FC = ({ initialAPIOrigin, onClose }) => { // touched state of apiOrigin gets set too early, perhaps because of the // autoFocus). const hasError = - form.submitCount > 0 && - form.touched.apiOrigin && - !!form.errors.apiOrigin; + formik.submitCount > 0 && + formik.touched.apiOrigin && + !!formik.errors.apiOrigin; return ( - + {t("developer_settings")} @@ -134,17 +134,16 @@ const Form: React.FC = ({ initialAPIOrigin, onClose }) => { = ({ initialAPIOrigin, onClose }) => { type="submit" color="accent" fullWidth - disabled={form.isSubmitting} + disabled={formik.isSubmitting} > {t("save")}