From 67450d08633fcdf033af0435b6277eca1ea86595 Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 19 Jan 2018 16:30:10 +0100 Subject: [PATCH] Adds dynamic dropdown for monthly amounts Resolves #12727 Auditors: Test Plan: --- app/browser/api/ledger.js | 13 ++++ app/common/lib/ledgerUtil.js | 5 +- .../preferences/payment/enabledContent.js | 7 +- docs/state.md | 1 + test/unit/app/browser/api/ledgerTest.js | 69 ++++++++++++++++++- test/unit/app/common/lib/ledgerUtilTest.js | 6 ++ 6 files changed, 97 insertions(+), 4 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index 4f95452202c..b9ee9f168e0 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -93,6 +93,7 @@ const clientOptions = { version: 'v2', environment: process.env.LEDGER_ENVIRONMENT || 'production' } + const fileTypes = { bmp: Buffer.from([0x42, 0x4d]), gif: Buffer.from([0x47, 0x49, 0x46, 0x38, [0x37, 0x39], 0x61]), @@ -1631,6 +1632,18 @@ const onWalletProperties = (state, body) => { } } + // monthly amount list + let list = body.getIn(['parameters', 'adFree', 'choices', 'BAT']) + if (list == null || !Immutable.List.isList(list) || list.isEmpty()) { + list = ledgerUtil.defaultMonthlyAmounts + } + + const currentAmount = ledgerState.getContributionAmount(state) + if (!list.includes(currentAmount)) { + list = list.push(currentAmount).sort() + } + state = ledgerState.setInfoProp(state, 'monthlyAmounts', list) + // unconfirmed amount const unconfirmed = parseFloat(body.get('unconfirmed')) if (unconfirmed >= 0) { diff --git a/app/common/lib/ledgerUtil.js b/app/common/lib/ledgerUtil.js index 2589fdcaf57..010041076bc 100644 --- a/app/common/lib/ledgerUtil.js +++ b/app/common/lib/ledgerUtil.js @@ -303,6 +303,8 @@ const getMediaProvider = (url) => { return provider } +const defaultMonthlyAmounts = Immutable.List([5.0, 7.5, 10.0, 17.5, 25.0, 50.0, 75.0, 100.0]) + const milliseconds = { year: 365 * 24 * 60 * 60 * 1000, week: 7 * 24 * 60 * 60 * 1000, @@ -330,7 +332,8 @@ const getMethods = () => { getMediaProvider, getMediaData, getMediaKey, - milliseconds + milliseconds, + defaultMonthlyAmounts } let privateMethods = {} diff --git a/app/renderer/components/preferences/payment/enabledContent.js b/app/renderer/components/preferences/payment/enabledContent.js index a700e31bff0..61ab0f8b106 100644 --- a/app/renderer/components/preferences/payment/enabledContent.js +++ b/app/renderer/components/preferences/payment/enabledContent.js @@ -10,6 +10,7 @@ const Immutable = require('immutable') // util const {batToCurrencyString, formatCurrentBalance, formattedDateFromTimestamp, walletStatus} = require('../../../../common/lib/ledgerUtil') const {l10nErrorText} = require('../../../../common/lib/httpUtil') +const ledgerUtil = require('../../../../common/lib/ledgerUtil') const {changeSetting} = require('../../../lib/settingsUtil') const settings = require('../../../../../js/constants/settings') const locale = require('../../../../../js/l10n') @@ -270,6 +271,7 @@ class EnabledContent extends ImmutableComponent { const walletStatusText = walletStatus(ledgerData) const contributionAmount = ledgerState.getContributionAmount(null, ledgerData.get('contributionAmount'), this.props.settings) const inTransition = ledgerData.getIn(['migration', 'btc2BatTransitionPending']) === true + const amountList = ledgerData.get('monthlyAmounts') || ledgerUtil.defaultMonthlyAmounts return
+ onChange={changeSetting.bind(null, this.props.onChangeSetting, settings.PAYMENTS_CONTRIBUTION_AMOUNT)} + > { - [5.0, 7.5, 10.0, 17.5, 25.0, 50.0, 75.0, 100.0].map((amount) => { + amountList.map((amount) => { let alternative = '' if (ledgerData.has('currentRate')) { const converted = batToCurrencyString(amount, ledgerData) diff --git a/docs/state.md b/docs/state.md index c412c160e80..0bb8768719f 100644 --- a/docs/state.md +++ b/docs/state.md @@ -207,6 +207,7 @@ AppStore creating: boolean, // wallet is being created currentRate: number, hasBitcoinHandler: boolean, // brave browser has a `bitcoin:` URI handler + monthlyAmounts: Array // list of all monthly amounts for the contribution passphrase: string, // the BAT wallet passphrase paymentId: string, probi: number, diff --git a/test/unit/app/browser/api/ledgerTest.js b/test/unit/app/browser/api/ledgerTest.js index ece81acc0b0..dacb2b83f1e 100644 --- a/test/unit/app/browser/api/ledgerTest.js +++ b/test/unit/app/browser/api/ledgerTest.js @@ -1214,8 +1214,9 @@ describe('ledger api unit tests', function () { }) describe('onWalletProperties', function () { - const state = defaultAppState + let state = defaultAppState .setIn(['ledger', 'info', 'contributionAmount'], 0) + .setIn(['ledger', 'info', 'monthlyAmounts'], Immutable.List([5.0, 7.5, 10.0, 17.5, 25.0, 50.0, 75.0, 100.0])) describe('generatePaymentData', function () { let generatePaymentDataSpy @@ -1517,6 +1518,72 @@ describe('ledger api unit tests', function () { assert.deepEqual(result.toJS(), expectedState.toJS()) }) }) + + describe('monthly amount list', function () { + const oldState = state + + const body = Immutable.fromJS({ + parameters: { + adFree: { + choices: { + BAT: [ + 5, + 15, + 20 + ] + } + } + } + }) + + before(function () { + state = state + .deleteIn(['ledger', 'info', 'monthlyAmounts']) + }) + + after(function () { + state = oldState + }) + + it('null case', function () { + const result = ledgerApi.onWalletProperties(state, Immutable.Map()) + assert.deepEqual(result.toJS(), oldState.toJS()) + }) + + it('list is string', function () { + const result = ledgerApi.onWalletProperties(state, Immutable.fromJS({ + parameters: { + adFree: { + choices: { + BAT: 'rewrwer' + } + } + } + })) + assert.deepEqual(result.toJS(), oldState.toJS()) + }) + + it('user has monthly amount that is not on the list', function () { + const result = ledgerApi.onWalletProperties(state, body) + + const expectedState = oldState + .setIn(['ledger', 'info', 'monthlyAmounts'], Immutable.List([5.0, 10.0, 15.0, 20.0])) + + assert.deepEqual(result.toJS(), expectedState.toJS()) + }) + + it('list is normal', function () { + contributionAmount = 5 + + const expectedState = oldState + .setIn(['ledger', 'info', 'monthlyAmounts'], Immutable.List([5.0, 15.0, 20.0])) + + const result = ledgerApi.onWalletProperties(state, body) + assert.deepEqual(result.toJS(), expectedState.toJS()) + + contributionAmount = 10 + }) + }) }) describe('claimPromotion', function () { diff --git a/test/unit/app/common/lib/ledgerUtilTest.js b/test/unit/app/common/lib/ledgerUtilTest.js index 7b50b6245e4..d0c855da2b2 100644 --- a/test/unit/app/common/lib/ledgerUtilTest.js +++ b/test/unit/app/common/lib/ledgerUtilTest.js @@ -514,4 +514,10 @@ describe('ledgerUtil unit test', function () { assert.equal(result, 31536000000) }) }) + + describe('defaultMonthlyAmounts', function () { + it('should match', function () { + assert.deepEqual(ledgerUtil.defaultMonthlyAmounts.toJS(), [5.0, 7.5, 10.0, 17.5, 25.0, 50.0, 75.0, 100.0]) + }) + }) })