From ac2f680f6805534fdebad41755d7e112a00cd15a Mon Sep 17 00:00:00 2001 From: Sebastian Date: Wed, 7 Dec 2022 10:53:06 -0300 Subject: fix: show error message on login and registration form, prevent saving password on localstorage --- packages/demobank-ui/src/pages/home/index.tsx | 240 +++++++++----------------- 1 file changed, 81 insertions(+), 159 deletions(-) (limited to 'packages/demobank-ui') diff --git a/packages/demobank-ui/src/pages/home/index.tsx b/packages/demobank-ui/src/pages/home/index.tsx index 884113e85..9927f965c 100644 --- a/packages/demobank-ui/src/pages/home/index.tsx +++ b/packages/demobank-ui/src/pages/home/index.tsx @@ -213,32 +213,6 @@ function useWireTransferRequestType( return [retObj, retSetter]; } -/** - * Stores in the state a object containing a 'username' - * and 'password' field, in order to avoid losing the - * handle of the data entered by the user in fields. - */ -type CredentialsRequestTypeOpt = CredentialsRequestType | undefined; -function useCredentialsRequestType( - state?: CredentialsRequestType, -): [CredentialsRequestTypeOpt, StateUpdater] { - const ret = hooks.useLocalStorage( - "credentials-request-state", - JSON.stringify(state), - ); - const retObj: CredentialsRequestTypeOpt = ret[0] - ? JSON.parse(ret[0]) - : ret[0]; - const retSetter: StateUpdater = function (val) { - const newVal = - val instanceof Function - ? JSON.stringify(val(retObj)) - : JSON.stringify(val); - ret[1](newVal); - }; - return [retObj, retSetter]; -} - /** * Request preparators. * @@ -597,7 +571,7 @@ async function createWithdrawalCall( } async function loginCall( - req: CredentialsRequestType, + req: { username: string; password: string }, /** * FIXME: figure out if the two following * functions can be retrieved from the state. @@ -629,7 +603,7 @@ async function loginCall( * the page's (to indicate a successful login or a problem). */ async function registrationCall( - req: CredentialsRequestType, + req: { username: string; password: string }, /** * FIXME: figure out if the two following * functions can be retrieved somewhat from @@ -882,11 +856,7 @@ function ShowInputErrorLabel({ isDirty: boolean; }): VNode { if (message && isDirty) - return ( -
- {message} -
- ); + return
{message}
; return ; } @@ -1488,24 +1458,6 @@ function PaymentOptions({ currency }: { currency?: string }): VNode { ); } -function RegistrationButton(Props: any): VNode { - const { backendStateSetter, pageStateSetter } = Props; - const { i18n } = useTranslationContext(); - if (bankUiSettings.allowRegistrations) - return ( - - ); - - return ; -} - function undefinedIfEmpty(obj: T): T | undefined { return Object.keys(obj).some((k) => (obj as any)[k] !== undefined) ? obj @@ -1514,21 +1466,21 @@ function undefinedIfEmpty(obj: T): T | undefined { /** * Collect and submit login data. */ -function LoginForm(Props: any): VNode { - const { backendStateSetter, pageStateSetter } = Props; - const [submitData, submitDataSetter] = useCredentialsRequestType(); +function LoginForm(): VNode { + const [backendState, backendStateSetter] = useBackendState(); + const { pageState, pageStateSetter } = usePageContext(); + const [username, setUsername] = useState(); + const [password, setPassword] = useState(); const { i18n } = useTranslationContext(); const ref = useRef(null); useEffect(() => { ref.current?.focus(); }, []); - const errors = !submitData - ? undefined - : undefinedIfEmpty({ - username: !submitData.username ? i18n.str`Missing username` : undefined, - password: !submitData.password ? i18n.str`Missing password` : undefined, - }); + const errors = undefinedIfEmpty({ + username: !username ? i18n.str`Missing username` : undefined, + password: !password ? i18n.str`Missing password` : undefined, + }); return ( @@ -1615,24 +1566,23 @@ function LoginForm(Props: any): VNode { * Collect and submit registration data. */ function RegistrationForm(): VNode { - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [backendState, backendStateSetter] = useBackendState(); const { pageState, pageStateSetter } = usePageContext(); - const [submitData, submitDataSetter] = useCredentialsRequestType(); + const [username, setUsername] = useState(); + const [password, setPassword] = useState(); + const [repeatPassword, setRepeatPassword] = useState(); + const { i18n } = useTranslationContext(); - const errors = !submitData - ? undefined - : undefinedIfEmpty({ - username: !submitData.username ? i18n.str`Missing username` : undefined, - password: !submitData.password ? i18n.str`Missing password` : undefined, - repeatPassword: !submitData.repeatPassword - ? i18n.str`Missing password` - : submitData.repeatPassword !== submitData.password - ? i18n.str`Password don't match` - : undefined, - }); + const errors = undefinedIfEmpty({ + username: !username ? i18n.str`Missing username` : undefined, + password: !password ? i18n.str`Missing password` : undefined, + repeatPassword: !repeatPassword + ? i18n.str`Missing password` + : repeatPassword !== password + ? i18n.str`Password don't match` + : undefined, + }); return ( @@ -1650,16 +1600,15 @@ function RegistrationForm(): VNode { name="register-un" type="text" placeholder="Username" - value={submitData && submitData.username} - required + value={username ?? ""} onInput={(e): void => { - submitDataSetter((submitData: any) => ({ - ...submitData, - username: e.currentTarget.value, - })); + setUsername(e.currentTarget.value); }} /> -
+

@@ -1668,15 +1617,16 @@ function RegistrationForm(): VNode { name="register-pw" id="register-pw" placeholder="Password" - value={submitData && submitData.password} + value={password ?? ""} required onInput={(e): void => { - submitDataSetter((submitData: any) => ({ - ...submitData, - password: e.currentTarget.value, - })); + setPassword(e.currentTarget.value); }} /> +

@@ -1686,57 +1636,31 @@ function RegistrationForm(): VNode { name="register-repeat" id="register-repeat" placeholder="Same password" - value={submitData && submitData.repeatPassword} + value={repeatPassword ?? ""} required onInput={(e): void => { - submitDataSetter((submitData: any) => ({ - ...submitData, - repeatPassword: e.currentTarget.value, - })); + setRepeatPassword(e.currentTarget.value); }} /> +
- {/* - - // FIXME: add input validation (must start with +, otherwise only numbers) - { - submitDataSetter((submitData: any) => ({ - ...submitData, - phone: e.currentTarget.value, - }))}} /> -
- */}