From b97e03041aeec61ad6477d10978c4c2ed40262bd Mon Sep 17 00:00:00 2001 From: MathieuG-P <40181755+Zagrios@users.noreply.github.com> Date: Tue, 26 Dec 2023 23:14:43 +0100 Subject: [PATCH] [chore] reduce code duplication --- .../enter-meta-token-modal.component.tsx | 62 ++++++++++--------- 1 file changed, 33 insertions(+), 29 deletions(-) diff --git a/src/renderer/components/modal/modal-types/bs-downgrade/enter-meta-token-modal.component.tsx b/src/renderer/components/modal/modal-types/bs-downgrade/enter-meta-token-modal.component.tsx index f7c8be4f..d18a694c 100644 --- a/src/renderer/components/modal/modal-types/bs-downgrade/enter-meta-token-modal.component.tsx +++ b/src/renderer/components/modal/modal-types/bs-downgrade/enter-meta-token-modal.component.tsx @@ -44,10 +44,6 @@ export const EnterMetaTokenModal: ModalComponent = ({resolver}) => { ); } -const isPasswordValid = (password: string) => { - return password.length >= 8; -} - const EnterPasswordView = ({onValid, onCancel, dontHaveToken}: {onValid: (token: string) => void, onCancel: () => void, dontHaveToken: () => void}) => { const t = useTranslation(); @@ -55,7 +51,6 @@ const EnterPasswordView = ({onValid, onCancel, dontHaveToken}: {onValid: (token: const config = useService(ConfigurationService); const [password, setPassword] = useState(""); - const [showPassword, setShowPassword] = useState(false); const getTokenFromStorage = () => { const cryptedToken = config.get(OCULUS_TOKEN_STORAGE_KEY); @@ -63,7 +58,7 @@ const EnterPasswordView = ({onValid, onCancel, dontHaveToken}: {onValid: (token: const { result: token, error } = tryit(() => crypto.AES.decrypt(cryptedToken, password).toString(crypto.enc.Utf8)); if(error){ - logRenderError(error, "Often indicate that the password is not the same as the one used to save the token."); + logRenderError(error, "Often indicate that the password is not the same as the one used to save the token. This error can occur when the use is writing the password."); } return token; @@ -85,16 +80,7 @@ const EnterPasswordView = ({onValid, onCancel, dontHaveToken}: {onValid: (token:
-
- - {!isPasswordValid(password) && password.length > 0 && ( - {t("modals.enter-meta-token.body.password-too-short")} - )} -
-
- setPassword(e.target.value)} value={password} type={showPassword ? "text" : "password"} name="password" id="password" placeholder={t("modals.enter-meta-token.body.password")} /> - setShowPassword(prev => !prev)} /> -
+ setPassword(v.password)} value={password}/>
@@ -126,7 +112,7 @@ const EnterOculusTokenView = ({onValid, onCancel, alreadyHaveToken}: {onValid: ( const [showToken, setShowToken] = useState(false); const [stay, setStay] = useState(false); const [password, setPassword] = useState(""); - const [showPassword, setShowPassword] = useState(false); + const [passwordValid, setPasswordValid] = useState(false); const storeToken = (token: string, password: string) => { const cryptedToken = crypto.AES.encrypt(token, password).toString(); @@ -170,22 +156,13 @@ const EnterOculusTokenView = ({onValid, onCancel, alreadyHaveToken}: {onValid: (
- {t("modals.enter-meta-token.body.save-my-token")} + {t("modals.enter-meta-token.body.save-my-token")}
-
- - {!isPasswordValid(password) && password.length > 0 && ( - {t("modals.enter-meta-token.body.password-too-short")} - )} -
-
- setPassword(e.target.value)} value={password} type={showPassword ? "text" : "password"} name="password" id="password" placeholder={t("modals.enter-meta-token.body.password")} /> - setShowPassword(prev => !prev)} /> -
+ {setPassword(v.password); setPasswordValid(v.valid)}} value={password}/>
@@ -198,10 +175,37 @@ const EnterOculusTokenView = ({onValid, onCancel, alreadyHaveToken}: {onValid: (
- +
); } + +const PasswordInput = ({onChange, value}: {onChange: (value : {password: string, valid: boolean}) => void, value: string}) => { + + const t = useTranslation(); + + const [showPassword, setShowPassword] = useState(false); + + const isPasswordValid = (password: string) => { + return password.length >= 8; + } + + return ( + <> +
+ + {!isPasswordValid(value) && value.length > 0 && ( + {t("modals.enter-meta-token.body.password-too-short")} + )} +
+
+ onChange({password: e.target.value, valid: isPasswordValid(e.target.value)})} value={value} type={showPassword ? "text" : "password"} name="password" id="password" placeholder={t("modals.enter-meta-token.body.password")} /> + setShowPassword(prev => !prev)} /> +
+ + ) + +}