Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Merge pull request #12728 from NejcZdovc/feature/#12727-dropdown
Browse files Browse the repository at this point in the history
Adds dynamic dropdown for monthly amounts
  • Loading branch information
bsclifton authored Jan 19, 2018
2 parents c36885b + 67450d0 commit 78d12ee
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 4 deletions.
13 changes: 13 additions & 0 deletions app/browser/api/ledger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]),
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 4 additions & 1 deletion app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -330,7 +332,8 @@ const getMethods = () => {
getMediaProvider,
getMediaData,
getMediaKey,
milliseconds
milliseconds,
defaultMonthlyAmounts
}

let privateMethods = {}
Expand Down
7 changes: 5 additions & 2 deletions app/renderer/components/preferences/payment/enabledContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 <section className={css(styles.enabledContent)}>
<div className={css(
Expand Down Expand Up @@ -309,9 +311,10 @@ class EnabledContent extends ImmutableComponent {
data-isPanel
data-test-id='fundsSelectBox'
value={contributionAmount}
onChange={changeSetting.bind(null, this.props.onChangeSetting, settings.PAYMENTS_CONTRIBUTION_AMOUNT)}>
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)
Expand Down
1 change: 1 addition & 0 deletions docs/state.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ AppStore
creating: boolean, // wallet is being created
currentRate: number,
hasBitcoinHandler: boolean, // brave browser has a `bitcoin:` URI handler
monthlyAmounts: Array<float> // list of all monthly amounts for the contribution
passphrase: string, // the BAT wallet passphrase
paymentId: string,
probi: number,
Expand Down
69 changes: 68 additions & 1 deletion test/unit/app/browser/api/ledgerTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 () {
Expand Down
6 changes: 6 additions & 0 deletions test/unit/app/common/lib/ledgerUtilTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -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])
})
})
})

0 comments on commit 78d12ee

Please sign in to comment.