Skip to content

Commit

Permalink
Merge pull request #2040 from Expensify/nikki-dateutils-whitescreen
Browse files Browse the repository at this point in the history
Ensure we're always including timezone in myPersonalDetails
  • Loading branch information
jasperhuangg authored Mar 25, 2021
2 parents ae7f4d7 + f3cfc16 commit 12aaf9d
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 10 deletions.
8 changes: 1 addition & 7 deletions src/libs/DateUtils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import moment from 'moment';
import 'moment-timezone';
import _ from 'underscore';
import Onyx from 'react-native-onyx';
import Str from 'expensify-common/lib/str';
import ONYXKEYS from '../ONYXKEYS';
Expand All @@ -9,12 +8,7 @@ import CONST from '../CONST';
let timezone;
Onyx.connect({
key: ONYXKEYS.MY_PERSONAL_DETAILS,
callback: (val) => {
timezone = val ? val.timezone : CONST.DEFAULT_TIME_ZONE.selected;
if (_.isObject(timezone)) {
timezone = val.timezone.selected;
}
},
callback: (val) => { timezone = val ? val.timezone.selected : CONST.DEFAULT_TIME_ZONE.selected; },
});

/**
Expand Down
11 changes: 8 additions & 3 deletions src/libs/actions/PersonalDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,16 @@ function fetch() {
returnValueList: 'personalDetailsList',
})
.then((data) => {
const allPersonalDetails = formatPersonalDetails(data.personalDetailsList);
let myPersonalDetails = {};

// If personalDetailsList is empty, ensure we set the personal details for the current user
const personalDetailsList = _.isEmpty(data.personalDetailsList)
? {[currentUserEmail]: myPersonalDetails}
: data.personalDetailsList;
const allPersonalDetails = formatPersonalDetails(personalDetailsList);
Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, allPersonalDetails);

const myPersonalDetails = allPersonalDetails[currentUserEmail]
|| {avatar: getAvatar(undefined, currentUserEmail)};
myPersonalDetails = allPersonalDetails[currentUserEmail];

// Add the first and last name to the current user's MY_PERSONAL_DETAILS key
myPersonalDetails.firstName = lodashGet(data.personalDetailsList, [currentUserEmail, 'firstName'], '');
Expand Down
2 changes: 2 additions & 0 deletions src/libs/migrateOnyx.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import RenameActiveClientsKey from './migrations/RenameActiveClientsKey';
import RenamePriorityModeKey from './migrations/RenamePriorityModeKey';
import ReformatTimezone from './migrations/ReformatTimezone';

export default function () {
const startTime = Date.now();
Expand All @@ -10,6 +11,7 @@ export default function () {
const migrationPromises = [
RenameActiveClientsKey,
RenamePriorityModeKey,
ReformatTimezone,
];

// Reduce all promises down to a single promise. All promises run in a linear fashion, waiting for the
Expand Down
41 changes: 41 additions & 0 deletions src/libs/migrations/ReformatTimezone.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import ONYXKEYS from '../../ONYXKEYS';

// This migration changes the format of the timezone in the Onyx key MY_PERSONAL_DETAILS from a string to an object
export default function () {
return new Promise((resolve) => {
// Connect to the old key in Onyx to get the old value of myPersonalDetails timezone
// then update the timezone to be the default timezone and set the myPersonalDetails
// key with the updated values
const connectionID = Onyx.connect({
key: ONYXKEYS.MY_PERSONAL_DETAILS,
callback: (myPersonalDetails) => {
Onyx.disconnect(connectionID);

if (_.isUndefined(myPersonalDetails) || _.isEmpty(myPersonalDetails)) {
console.debug('[Migrate Onyx] Skipped migration ReformatTimezone: No myPersonalDetails key found');
return resolve();
}

// Fail early here because there is nothing to migrate, the timezone is already in the expected format
if (_.isObject(myPersonalDetails.timezone)) {
console.debug('[Migrate Onyx] Skipped migration ReformatTimezone');
return resolve();
}

// Update the timezone with the user's old timezone selection and set "automatic" to false
// because we don't know if their old timezone was set automatically or not
const details = myPersonalDetails;
details.timezone = {selected: details.timezone, automatic: false};
Onyx.set({
[ONYXKEYS.MY_PERSONAL_DETAILS]: details,
})
.then(() => {
console.debug('[Migrate Onyx] Ran migration ReformatTimezone');
resolve();
});
},
});
});
}

0 comments on commit 12aaf9d

Please sign in to comment.