Skip to content

Commit

Permalink
Separate Navigation.goBack() from a util function
Browse files Browse the repository at this point in the history
  • Loading branch information
MaciejSWM committed Mar 21, 2024
1 parent 8168992 commit 63f7a7f
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 27 deletions.
46 changes: 20 additions & 26 deletions src/libs/actions/PersonalDetails.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import type {PersonalDetails, PersonalDetailsList, PrivatePersonalDetails} from
import type {SelectedTimezone, Timezone} from '@src/types/onyx/PersonalDetails';
import * as Session from './Session';

type OptionsProps = {
preventGoBack?: boolean;
};

let currentUserEmail = '';
let currentUserAccountID = -1;
Onyx.connect({
Expand Down Expand Up @@ -75,33 +71,31 @@ function updatePronouns(pronouns: string) {
Navigation.goBack();
}

function updateDisplayName(firstName: string, lastName: string, options: OptionsProps = {}) {
if (currentUserAccountID) {
const parameters: UpdateDisplayNameParams = {firstName, lastName};
function updateDisplayName(firstName: string, lastName: string) {
if (!currentUserAccountID) {
return;
}

API.write(WRITE_COMMANDS.UPDATE_DISPLAY_NAME, parameters, {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[currentUserAccountID]: {
const parameters: UpdateDisplayNameParams = {firstName, lastName};

API.write(WRITE_COMMANDS.UPDATE_DISPLAY_NAME, parameters, {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: {
[currentUserAccountID]: {
firstName,
lastName,
displayName: PersonalDetailsUtils.createDisplayName(currentUserEmail ?? '', {
firstName,
lastName,
displayName: PersonalDetailsUtils.createDisplayName(currentUserEmail ?? '', {
firstName,
lastName,
}),
},
}),
},
},
],
});
}

if (!options.preventGoBack) {
Navigation.goBack();
}
},
],
});
}

function updateLegalName(legalFirstName: string, legalLastName: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function BaseOnboardingPersonalDetails({currentUserPersonalDetails, shouldUseNat
const {shouldUseNarrowLayout} = useOnboardingLayout();

const saveAndNavigate = useCallback((values: FormOnyxValues<'displayNameForm'>) => {
PersonalDetails.updateDisplayName(values.firstName.trim(), values.lastName.trim(), {preventGoBack: true});
PersonalDetails.updateDisplayName(values.firstName.trim(), values.lastName.trim());

Navigation.navigate(ROUTES.ONBOARDING_PURPOSE);
}, []);
Expand Down
1 change: 1 addition & 0 deletions src/pages/settings/Profile/DisplayNamePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type DisplayNamePageProps = DisplayNamePageOnyxProps & WithCurrentUserPersonalDe
*/
const updateDisplayName = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.DISPLAY_NAME_FORM>) => {
PersonalDetails.updateDisplayName(values.firstName.trim(), values.lastName.trim());
Navigation.goBack();
};

function DisplayNamePage({isLoadingApp = true, currentUserPersonalDetails}: DisplayNamePageProps) {
Expand Down

0 comments on commit 63f7a7f

Please sign in to comment.