Skip to content

Commit

Permalink
fix: simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
chaotixkilla committed Apr 13, 2022
1 parent eb8528d commit bfcf820
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 79 deletions.
3 changes: 1 addition & 2 deletions lib/brain.js
Original file line number Diff line number Diff line change
Expand Up @@ -3629,12 +3629,11 @@ Brain.prototype._getFiatButtonResponse = function _getFiatButtonResponse () {
const tx = this.tx
const cassettes = this.trader.cassettes
const virtualCassettes = this.trader.virtualCassettes
const uiCassettes = buildUiCassettes(cassettes, virtualCassettes)

const txLimit = getAmountToHardLimit(this.trader.triggers, this.customerTxHistory, tx)
const activeDenominations = Tx.computeCashOut(tx, cassettes, virtualCassettes, txLimit)

return { tx, cassettes: uiCassettes, activeDenominations }
return { tx, activeDenominations }
}

Brain.prototype._chooseFiat = function _chooseFiat () {
Expand Down
53 changes: 14 additions & 39 deletions ui/js/app.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion ui/js/app.js.map

Large diffs are not rendered by default.

45 changes: 8 additions & 37 deletions ui/src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -1489,46 +1489,17 @@ function formatDenomination (denom) {
}

function buildCassetteButtons (_cassettes, numberOfButtons) {
var activeDenoms = _cassettes.reduce(
(acc, value) => {
acc[value.denomination] = value.count === null || value.count > 0
return acc
},
{}
)

var selectedDenoms = Object.keys(activeDenoms).reduce(
(acc, value) => {
acc[value] = false
return acc
},
{}
)

cassettes = _cassettes
for (var i = 0; i < numberOfButtons; i++) {
var cassette = null
var denomination = null

var intersection = Object.keys(activeDenoms).reduce((acc, value) => {
acc[value] = Boolean(activeDenoms[value] && selectedDenoms[value])
return acc
}, {})

// If we're on the last available button and no selected cassettes by now are active
// This ensures that there's always an available button if there's any bill on a cassette
if (i + 1 === numberOfButtons && Object.values(intersection).every(it => it === false)) {
const firstActiveDenom = Object.keys(activeDenoms).find(it => activeDenoms[it] === true)
cassette = cassettes.find(it => it.denomination === firstActiveDenom && (it.count === null || it.count > 0))
} else {
cassette = cassettes.find(it => it.denomination === cassettes[i].denomination && (it.count === null || it.count > 0))
|| cassettes[i]
|| cassettes[cassettes.length - 1]
}
var activeCassettes = _cassettes.filter(it => it.count === null || it.count > 0)
var inactiveCassettes = _cassettes.filter(it => it.count === 0)

var allCassettes = activeCassettes.concat(inactiveCassettes)
var selectedCassettes = allCassettes.slice(0, numberOfButtons)
var sortedCassettes = selectedCassettes.sort((a, b) => a.denomination - b.denomination)

denomination = formatDenomination(cassette.denomination || 0)
for (var i = 0; i < sortedCassettes.length; i++) {
var denomination = formatDenomination(sortedCassettes[i].denomination || 0)
$('.cash-button[data-denomination-index=' + i + '] .js-denomination').text(denomination)
selectedDenoms[denomination] = true
}
}

Expand Down

0 comments on commit bfcf820

Please sign in to comment.