Skip to content

Commit

Permalink
Fixes empty state flow
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Dec 4, 2018
1 parent b0c4a7b commit 6cd63da
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 6 deletions.
4 changes: 3 additions & 1 deletion browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ void RewardsDOMHandler::OnWalletProperties(
if (error_code == 0 && wallet_properties) {
walletInfo->SetDouble("balance", wallet_properties->balance);
walletInfo->SetString("probi", wallet_properties->probi);
ui_values->SetBoolean("emptyWallet", (wallet_properties->balance > 0));
ui_values->SetBoolean("emptyWallet", (wallet_properties->balance == 0));

auto rates = std::make_unique<base::DictionaryValue>();
for (auto const& rate : wallet_properties->rates) {
Expand Down Expand Up @@ -316,6 +316,8 @@ void RewardsDOMHandler::OnWalletProperties(
}

values.SetDictionary("ui", std::move(ui_values));
// TODO this needs to be moved out of this flow, because
// now we set this values every minute
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.initAutoContributeSettings", values);

Expand Down
18 changes: 16 additions & 2 deletions components/brave_rewards/resources/ui/reducers/rewards_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,33 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
switch (action.type) {
case types.INIT_AUTOCONTRIBUTE_SETTINGS:
{
state = { ...state }
let properties = action.payload.properties
let ui = state.ui

if (!properties || Object.keys(properties).length === 0) {
break
}

state = { ...state }
const isEmpty = !ui || ui.emptyWallet

Object.keys(properties).map((property: string) => {
if (properties[property] !== undefined) {
if (properties[property] !== undefined && properties[property] !== 'ui') {
state[property] = properties[property]
} else if (properties[property] === 'ui') {
ui = Object.assign(ui, properties[property])
}
})

if (!isEmpty) {
ui.emptyWallet = false
}

state = {
...state,
ui
}

break
}
case types.ON_SETTING_SAVE:
Expand Down
6 changes: 3 additions & 3 deletions components/brave_rewards/resources/ui/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ export const defaultState: Rewards.State = {
recoveryKey: '',
reconcileStamp: 0,
ui: {
walletRecoverySuccess: null,
emptyWallet: true,
walletServerProblem: false,
modalBackup: false,
walletCorrupted: false,
walletImported: false
walletImported: false,
walletRecoverySuccess: null,
walletServerProblem: false
},
autoContributeList: [],
reports: {},
Expand Down
91 changes: 91 additions & 0 deletions components/test/brave_rewards/ui/reducers/rewards_reducer_test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
/* global chrome */

import reducers from '../../../../brave_rewards/resources/ui/reducers'
import { types } from '../../../../brave_rewards/resources/ui/constants/rewards_types'
import { defaultState } from '../../../../brave_rewards/resources/ui/storage'

describe('rewards reducer', () => {
const constantDate = new Date('2018-01-01T12:00:00')

beforeAll(() => {
global.Date = class extends Date {
constructor () {
super()
return constantDate
}
}
});

describe('INIT_AUTOCONTRIBUTE_SETTINGS', () => {
describe('empty wallet', () => {
it('import flow - empty', () => {
const expectedState: Rewards.State = { ...defaultState }
expectedState.ui.emptyWallet = true

const assertion = reducers(undefined, {
type: types.INIT_AUTOCONTRIBUTE_SETTINGS,
payload: {
properties: {
ui: {
emptyWallet: true
}
}
}
})
expect(assertion).toEqual({
rewardsData: expectedState
})
})

it('import flow - funded', () => {
const expectedState: Rewards.State = { ...defaultState }
expectedState.ui.emptyWallet = false

const assertion = reducers(undefined, {
type: types.INIT_AUTOCONTRIBUTE_SETTINGS,
payload: {
properties: {
ui: {
emptyWallet: false
}
}
}
})

expect(assertion).toEqual({
rewardsData: expectedState
})
})

it('import flow - existing state', () => {
const initState: Rewards.State = { ...defaultState }
initState.ui.emptyWallet = false
initState.ui.walletRecoverySuccess = true

const expectedState: Rewards.State = { ...defaultState }
expectedState.ui.emptyWallet = false
expectedState.ui.walletRecoverySuccess = true

const assertion = reducers({
rewardsData: initState
}, {
type: types.INIT_AUTOCONTRIBUTE_SETTINGS,
payload: {
properties: {
ui: {
emptyWallet: true
}
}
}
})

expect(assertion).toEqual({
rewardsData: expectedState
})
})
})
})
})

0 comments on commit 6cd63da

Please sign in to comment.