Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fromAccount instead of selectedAcccount for account updates on edit #15449

Merged
merged 3 commits into from
Aug 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 21 additions & 19 deletions ui/ducks/send/send.js
Original file line number Diff line number Diff line change
Expand Up @@ -1440,28 +1440,30 @@ const slice = createSlice({
extraReducers: (builder) => {
builder
.addCase(ACCOUNT_CHANGED, (state, action) => {
// If we are on the edit flow then we need to watch for changes to the
// current account.address in state and keep balance updated
// appropriately
if (
state.stage === SEND_STAGES.EDIT &&
action.payload.account.address === state.selectedAccount.address
) {
// This event occurs when the user's account details update due to
// background state changes. If the account that is being updated is
// the current from account on the edit flow we need to update
// the balance for the account and revalidate the send state.
state.selectedAccount.balance = action.payload.account.balance;
// We need to update the asset balance if the asset is the native
// network asset. Once we update the balance we recompute error state.
// This event occurs when the user's account details update due to
// background state changes. If the account that is being updated is
// the current from account on the edit flow we need to update
// the balance for the account and revalidate the send state.
if (state.stage === SEND_STAGES.EDIT && action.payload.account) {
const draftTransaction =
state.draftTransactions[state.currentTransactionUUID];
if (draftTransaction?.asset.type === ASSET_TYPES.NATIVE) {
draftTransaction.asset.balance = action.payload.account.balance;
if (
draftTransaction &&
draftTransaction.fromAccount &&
draftTransaction.fromAccount.address ===
action.payload.account.address
) {
draftTransaction.fromAccount.balance =
action.payload.account.balance;
// We need to update the asset balance if the asset is the native
// network asset. Once we update the balance we recompute error state.
if (draftTransaction.asset.type === ASSET_TYPES.NATIVE) {
draftTransaction.asset.balance = action.payload.account.balance;
}
slice.caseReducers.validateAmountField(state);
slice.caseReducers.validateGasField(state);
slice.caseReducers.validateSendState(state);
}
slice.caseReducers.validateAmountField(state);
slice.caseReducers.validateGasField(state);
slice.caseReducers.validateSendState(state);
}
})
.addCase(ADDRESS_BOOK_UPDATED, (state, action) => {
Expand Down
42 changes: 39 additions & 3 deletions ui/ducks/send/send.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1142,9 +1142,14 @@ describe('Send Slice', () => {
});

describe('Account Changed', () => {
it('should', () => {
it('should correctly update the fromAccount in an edit', () => {
const accountsChangedState = {
...INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
...getInitialSendStateWithExistingTxState({
fromAccount: {
address: '0xAddress',
balance: '0x0',
},
}),
stage: SEND_STAGES.EDIT,
selectedAccount: {
address: '0xAddress',
Expand All @@ -1164,11 +1169,42 @@ describe('Send Slice', () => {

const result = sendReducer(accountsChangedState, action);

expect(result.selectedAccount.balance).toStrictEqual(
const draft = getTestUUIDTx(result);

expect(draft.fromAccount.balance).toStrictEqual(
action.payload.account.balance,
);
});

it('should gracefully handle missing account param in payload', () => {
const accountsChangedState = {
...getInitialSendStateWithExistingTxState({
fromAccount: {
address: '0xAddress',
balance: '0x0',
},
}),
stage: SEND_STAGES.EDIT,
selectedAccount: {
address: '0xAddress',
balance: '0x0',
},
};

const action = {
type: 'ACCOUNT_CHANGED',
payload: {
account: undefined,
},
};

const result = sendReducer(accountsChangedState, action);

const draft = getTestUUIDTx(result);

expect(draft.fromAccount.balance).toStrictEqual('0x0');
});

it(`should not edit account balance if action payload address is not the same as state's address`, () => {
const accountsChangedState = {
...INITIAL_SEND_STATE_FOR_EXISTING_DRAFT,
Expand Down