Skip to content

Commit

Permalink
Merge pull request #9218 from Expensify/maxence-rcpc
Browse files Browse the repository at this point in the history
Refactor ChangePassword
  • Loading branch information
marcaaron authored Jun 23, 2022
2 parents 3ef2729 + d457cb1 commit b38693e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 21 deletions.
8 changes: 7 additions & 1 deletion src/libs/API.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,13 @@ function write(command, apiCommandParameters = {}, onyxData = {}) {
// Assemble all the request data we'll be storing in the queue
const request = {
command,
data,
data: {
...data,

// This should be removed once we are no longer using deprecatedAPI https://github.com/Expensify/Expensify/issues/215650
shouldRetry: true,
canCancel: true,
},
..._.omit(onyxData, 'optimisticData'),
};

Expand Down
48 changes: 29 additions & 19 deletions src/libs/actions/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {PUBLIC_DOMAINS as COMMON_PUBLIC_DOMAINS} from 'expensify-common/lib/CONS
import moment from 'moment';
import ONYXKEYS from '../../ONYXKEYS';
import * as DeprecatedAPI from '../deprecatedAPI';
import * as API from '../API';
import CONFIG from '../../CONFIG';
import CONST from '../../CONST';
import Navigation from '../Navigation/Navigation';
Expand Down Expand Up @@ -46,25 +47,34 @@ Onyx.connect({
*
* @param {String} oldPassword
* @param {String} password
* @returns {Promise}
*/
function changePasswordAndNavigate(oldPassword, password) {
Onyx.merge(ONYXKEYS.ACCOUNT, {...CONST.DEFAULT_ACCOUNT_DATA, loading: true});

return DeprecatedAPI.ChangePassword({oldPassword, password})
.then((response) => {
if (response.jsonCode !== 200) {
const error = lodashGet(response, 'message', 'Unable to change password. Please try again.');
Onyx.merge(ONYXKEYS.ACCOUNT, {error});
return;
}

const success = lodashGet(response, 'message', 'Password changed successfully.');
Onyx.merge(ONYXKEYS.ACCOUNT, {success});
})
.finally(() => {
Onyx.merge(ONYXKEYS.ACCOUNT, {loading: false});
});
function updatePassword(oldPassword, password) {
API.write('UpdatePassword', {
oldPassword,
password,
}, {
optimisticData: [
{
onyxMethod: 'merge',
key: ONYXKEYS.ACCOUNT,
value: {...CONST.DEFAULT_ACCOUNT_DATA, loading: true},
},
],
successData: [
{
onyxMethod: 'merge',
key: ONYXKEYS.ACCOUNT,
value: {loading: false},
},
],
failureData: [
{
onyxMethod: 'merge',
key: ONYXKEYS.ACCOUNT,
value: {loading: false},
},
],
});
}

/**
Expand Down Expand Up @@ -438,7 +448,7 @@ function generateStatementPDF(period) {
}

export {
changePasswordAndNavigate,
updatePassword,
closeAccount,
getBetas,
getUserDetails,
Expand Down
2 changes: 1 addition & 1 deletion src/pages/settings/PasswordPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ class PasswordPage extends Component {
if (!this.validate()) {
return;
}
User.changePasswordAndNavigate(this.state.currentPassword, this.state.newPassword);
User.updatePassword(this.state.currentPassword, this.state.newPassword);
}

render() {
Expand Down

0 comments on commit b38693e

Please sign in to comment.