From fafbe96c3755885d96129cf183e0c636563dda53 Mon Sep 17 00:00:00 2001 From: binh-dam-ibigroup <56846598+binh-dam-ibigroup@users.noreply.github.com> Date: Mon, 16 Oct 2023 17:07:09 -0400 Subject: [PATCH] improvement(user-account-screen): Consolidate input change handling. --- .../user/existing-account-display.tsx | 40 +++-------------- lib/components/user/new-account-wizard.tsx | 11 +++++ .../user/sequential-pane-display.tsx | 7 ++- lib/components/user/user-account-screen.tsx | 45 +++++++++++++++++-- 4 files changed, 62 insertions(+), 41 deletions(-) diff --git a/lib/components/user/existing-account-display.tsx b/lib/components/user/existing-account-display.tsx index b9784fa2b..f89ccca55 100644 --- a/lib/components/user/existing-account-display.tsx +++ b/lib/components/user/existing-account-display.tsx @@ -1,10 +1,9 @@ import { connect } from 'react-redux' import { FormattedMessage, useIntl } from 'react-intl' import { FormikProps } from 'formik' -import React, { useCallback } from 'react' +import React from 'react' import { AppReduxState } from '../../util/state-types' -import { toastSuccess } from '../util/toasts' import { TransitModeConfig } from '../../util/config-types' import PageTitle from '../util/page-title' @@ -24,44 +23,15 @@ interface Props extends FormikProps { /** * This component handles the existing account display. */ -const ExistingAccountDisplay = (parentProps: Props) => { +const ExistingAccountDisplay = (props: Props) => { // The props include Formik props that provide access to the current user data // and to its own blur/change/submit event handlers that automate the state. // We forward the props to each pane so that their individual controls // can be wired to be managed by Formik. - const { handleChange, submitForm, wheelchairEnabled } = parentProps + + const { wheelchairEnabled } = props const intl = useIntl() - const props = { - ...parentProps, - handleChange: useCallback( - async (e) => { - // Apply changes and submit the form right away to update the user profile. - handleChange(e) - try { - // Disable input during submission - e.target.disabled = true - await submitForm() - // Re-enable input during submission - e.target.disabled = false - // Display a toast notification on success. - toastSuccess( - intl.formatMessage({ - // Use a summary text for the field, if defined (e.g. to replace long labels), - // otherwise, fall back on the first label of the input. - defaultMessage: e.target.labels[0]?.innerText, - id: `components.ExistingAccountDisplay.fields.${e.target.name}` - }), - intl.formatMessage({ - id: 'components.ExistingAccountDisplay.fieldUpdated' - }) - ) - } catch { - alert('Error updating profile') - } - }, - [intl, handleChange, submitForm] - ) - } + const panes = [ { pane: FavoritePlaceList, diff --git a/lib/components/user/new-account-wizard.tsx b/lib/components/user/new-account-wizard.tsx index 9cc45c7b9..aa9ac30cf 100644 --- a/lib/components/user/new-account-wizard.tsx +++ b/lib/components/user/new-account-wizard.tsx @@ -1,6 +1,7 @@ import { Form, FormikProps } from 'formik' import { FormattedMessage, useIntl } from 'react-intl' import React, { useCallback } from 'react' +import toast from 'react-hot-toast' import PageTitle from '../util/page-title' @@ -28,6 +29,7 @@ interface Props extends FormikUserProps { */ const NewAccountWizard = ({ activePaneId, + onCancel, // provided by UserAccountScreen onCreate, // provided by UserAccountScreen ...formikProps // provided by Formik }: Props): JSX.Element => { @@ -41,6 +43,14 @@ const NewAccountWizard = ({ } }, [onCreate, userData]) + const handleFinish = useCallback(() => { + // Display a toast to acknowledge saved changes + // (although in reality, changes quietly took effect in previous screens). + toast.success(intl.formatMessage({ id: 'actions.user.preferencesSaved' })) + + onCancel && onCancel() + }, [intl, onCancel]) + if (activePaneId === 'verify') { const verifyEmail = intl.formatMessage({ id: 'components.NewAccountWizard.verify' @@ -97,6 +107,7 @@ const NewAccountWizard = ({ diff --git a/lib/components/user/sequential-pane-display.tsx b/lib/components/user/sequential-pane-display.tsx index e41a953b7..9f2df97bf 100644 --- a/lib/components/user/sequential-pane-display.tsx +++ b/lib/components/user/sequential-pane-display.tsx @@ -21,6 +21,7 @@ export interface PaneProps { interface OwnProps { activePaneId: string + onFinish?: () => void panes: PaneProps[] } @@ -57,7 +58,7 @@ class SequentialPaneDisplay extends Component> { } _handleToNextPane = async (e: MouseEvent