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

Commit

Permalink
Moving ledger into appState part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Sep 26, 2017
1 parent 45dcc23 commit aff082f
Show file tree
Hide file tree
Showing 29 changed files with 2,738 additions and 2,490 deletions.
2,231 changes: 2,231 additions & 0 deletions app/browser/api/ledger.js

Large diffs are not rendered by default.

167 changes: 131 additions & 36 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ const settings = require('../../../js/constants/settings')
const ledgerState = require('../../common/state/ledgerState')

// Utils
const ledgerUtil = require('../../common/lib/ledgerUtil')
const ledgerApi = require('../../browser/api/ledger')
const siteSettings = require('../../common/state/siteSettingsState')
const urlUtil = require('../../../js/lib/urlutil')
const {makeImmutable} = require('../../common/state/immutableUtil')
const getSetting = require('../../../js/settings').getSetting

Expand All @@ -26,30 +28,19 @@ const ledgerReducer = (state, action, immutableAction) => {
state = state.setIn(['ledger', 'info'], action.get('ledgerInfo'))
break
}
// TODO refactor
case appConstants.APP_UPDATE_LOCATION_INFO:
{
state = state.setIn(['ledger', 'locations'], action.get('locationInfo'))
break
}
case appConstants.APP_LEDGER_RECOVERY_STATUS_CHANGED:
{
state = ledgerState.setRecoveryStatus(state, action.get('recoverySucceeded'))
break
}
case appConstants.APP_SET_STATE:
{
state = ledgerUtil.init(state)
state = ledgerApi.init(state)
break
}
case appConstants.APP_BACKUP_KEYS:
{
ledgerUtil.backupKeys(state, action.get('backupAction'))
ledgerApi.backupKeys(state, action.get('backupAction'))
break
}
case appConstants.APP_RECOVER_WALLET:
{
state = ledgerUtil.recoverKeys(
state = ledgerApi.recoverKeys(
state,
action.get('useRecoveryKeyFile'),
action.get('firstRecoveryKey'),
Expand All @@ -59,7 +50,7 @@ const ledgerReducer = (state, action, immutableAction) => {
}
case appConstants.APP_SHUTTING_DOWN:
{
state = ledgerUtil.quit(state)
state = ledgerApi.quit(state)
break
}
case appConstants.APP_ON_CLEAR_BROWSING_DATA:
Expand All @@ -75,46 +66,49 @@ const ledgerReducer = (state, action, immutableAction) => {
// TODO not sure that we use APP_IDLE_STATE_CHANGED anymore
case appConstants.APP_IDLE_STATE_CHANGED:
{
state = ledgerUtil.pageDataChanged(state)
ledgerUtil.addVisit('NOOP', underscore.now(), null)
state = ledgerApi.pageDataChanged(state)
ledgerApi.addVisit('NOOP', underscore.now(), null)
break
}
case appConstants.APP_CHANGE_SETTING:
{
switch (action.get('key')) {
case settings.PAYMENTS_ENABLED:
{
state = ledgerUtil.initialize(state, action.get('value'))
state = ledgerApi.initialize(state, action.get('value'))
break
}
case settings.PAYMENTS_CONTRIBUTION_AMOUNT:
{
ledgerUtil.setPaymentInfo(action.get('value'))
ledgerApi.setPaymentInfo(action.get('value'))
break
}
case settings.PAYMENTS_MINIMUM_VISIT_TIME:
{
const value = action.get('value')
if (value <= 0) break
ledgerUtil.synopsis.options.minPublisherDuration = action.value
ledgerApi.saveOptionSynopsis('minPublisherDuration', value)
state = ledgerState.setSynopsisOption(state, 'minPublisherDuration', value)
state = ledgerApi.updatePublisherInfo(state)
break
}
case settings.PAYMENTS_MINIMUM_VISITS:
{
const value = action.get('value')
if (value <= 0) break

ledgerUtil.synopsis.options.minPublisherVisits = value
ledgerApi.saveOptionSynopsis('minPublisherVisits', value)
state = ledgerState.setSynopsisOption(state, 'minPublisherVisits', value)
state = ledgerApi.updatePublisherInfo(state)
break
}

case settings.PAYMENTS_ALLOW_NON_VERIFIED:
{
const value = action.get('value')
ledgerUtil.synopsis.options.showOnlyVerified = value
ledgerApi.saveOptionSynopsis('showOnlyVerified', value)
state = ledgerState.setSynopsisOption(state, 'showOnlyVerified', value)
state = ledgerApi.updatePublisherInfo(state)
break
}
}
Expand All @@ -135,9 +129,9 @@ const ledgerReducer = (state, action, immutableAction) => {
case 'ledgerPaymentsShown':
{
if (action.get('value') === false) {
delete ledgerUtil.synopsis.publishers[publisherKey]
ledgerApi.deleteSynopsis(publisherKey)
state = ledgerState.deletePublishers(state, publisherKey)
state = ledgerUtil.updatePublisherInfo(state)
state = ledgerApi.updatePublisherInfo(state)
}
break
}
Expand All @@ -147,19 +141,19 @@ const ledgerReducer = (state, action, immutableAction) => {
if (publisher.isEmpty()) {
break
}
state = ledgerUtil.updatePublisherInfo(state)
state = ledgerUtil.verifiedP(state, publisherKey)
state = ledgerApi.updatePublisherInfo(state)
state = ledgerApi.verifiedP(state, publisherKey)
break
}
case 'ledgerPinPercentage':
{
const value = action.get('value')
const publisher = ledgerState.getPublisher(state, publisherKey)
if (publisher.isEmpty()) {
if (publisher.isEmpty() || publisher.get('pinPercentage') === value) {
break
}

ledgerUtil.synopsis.publishers[publisherKey].pinPercentage = action.get('value')
state = ledgerUtil.updatePublisherInfo(state, publisherKey)
state = ledgerState.setPublishersProp(state, publisherKey, 'pinPercentage', value)
state = ledgerApi.updatePublisherInfo(state, publisherKey)
break
}
}
Expand All @@ -182,15 +176,13 @@ const ledgerReducer = (state, action, immutableAction) => {
if (publisher.isEmpty()) {
break
}
state = ledgerUtil.updatePublisherInfo(state)
state = ledgerApi.updatePublisherInfo(state)
}
break
}
case appConstants.APP_NETWORK_CONNECTED:
{
setTimeout((state) => {
ledgerUtil.networkConnected(state)
}, 1000, state)
ledgerApi.networkConnected(state)
break
}
case appConstants.APP_NAVIGATOR_HANDLER_REGISTERED:
Expand All @@ -211,7 +203,110 @@ const ledgerReducer = (state, action, immutableAction) => {
case windowConstants.WINDOW_SET_FOCUSED_FRAME:
case windowConstants.WINDOW_GOT_RESPONSE_DETAILS:
{
state = ledgerUtil.pageDataChanged(state)
state = ledgerApi.pageDataChanged(state)
break
}
case appConstants.APP_ON_FAVICON_RECEIVED:
{
state = ledgerState.setPublishersProp(state, action.get('publisherKey'), 'faviconURL', action.get('blob'))
state = ledgerApi.updatePublisherInfo(state)
break
}
case appConstants.APP_ON_EXCLUSION_STATUS:
{
const key = action.get('publisherKey')
const pattern = urlUtil.getHostPattern(key)
const value = action.get('excluded')
ledgerApi.savePublisherOption(key, 'exclude', value)
state = siteSettings.setSettingsProp(state, pattern, 'ledgerPayments', value)
state = ledgerState.setPublishersProp(state, key, ['options', 'exclude'], value)
state = ledgerApi.updatePublisherInfo(state)
break
}
case appConstants.APP_ON_LEDGER_LOCATION_UPDATE:
{
const location = action.get('location')
state = ledgerState.setLocationProp(state, location, action.get('prop'), action.get('value'))
break
}
case appConstants.APP_ON_PUBLISHER_OPTION_UPDATE:
{
const value = action.get('value')
const key = action.get('publisherKey')
const prop = action.get('prop')
state = ledgerState.setPublisherOption(state, key, prop, value)

if (action.get('saveIntoSettings')) {
const pattern = urlUtil.getHostPattern(key)
if (prop === 'exclude') {
state = siteSettings.setSettingsProp(state, pattern, 'ledgerPayments', value)
}
}
break
}
case appConstants.APP_ON_LEDGER_WALLET_CREATE:
{
ledgerApi.boot()
break
}
case appConstants.APP_ON_BOOT_STATE_FILE:
{
state = ledgerApi.onBootStateFile(state)
break
}
case appConstants.APP_ON_LEDGER_BALANCE_RECEIVED:
{
state = ledgerApi.balanceReceived(state, action.get('unconfirmed'))
break
}
case appConstants.APP_ON_WALLET_PROPERTIES:
{
state = ledgerApi.onWalletProperties(state, action.get('body'))
break
}
case appConstants.APP_LEDGER_PAYMENTS_PRESENT:
{
ledgerApi.paymentPresent(state, action.get('tabId'), action.get('present'))
break
}
case appConstants.APP_ON_ADD_FUNDS_CLOSED:
{
ledgerApi.addFoundClosed(state)
break
}
case appConstants.APP_ON_WALLET_RECOVERY:
{
state = ledgerApi.onWalletRecovery(state, action.get('error'), action.get('result'))
break
}
case appConstants.APP_ON_BRAVERY_PROPERTIES:
{
state = ledgerApi.onBraveryProperties(state, action.get('error'), action.get('result'))
break
}
case appConstants.APP_ON_FIRST_LEDGER_SYNC:
{
state = ledgerApi.onLedgerFirstSync(state, action.get('parsedData'))
break
}
case appConstants.APP_ON_LEDGER_CALLBACK:
{
state = ledgerApi.onCallback(state, action.get('result'), action.get('delayTime'))
break
}
case appConstants.APP_ON_TIME_UNTIL_RECONCILE:
{
state = ledgerApi.onTimeUntilReconcile(state, action.get('stateResult'))
break
}
case appConstants.APP_ON_LEDGER_RUN:
{
ledgerApi.run(state, action.get('delay'))
break
}
case appConstants.APP_ON_NETWORK_CONNECTED:
{
state = ledgerApi.onNetworkConnected(state)
break
}
}
Expand Down
21 changes: 14 additions & 7 deletions app/browser/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ const bookmarksState = require('../common/state/bookmarksState')
const bookmarkFoldersState = require('../common/state/bookmarkFoldersState')
const historyState = require('../common/state/historyState')
const bookmarkOrderCache = require('../common/cache/bookmarkOrderCache')
const ledgerState = require('../common/state/ledgerState')
const {getWindow} = require('./windows')

let currentPartitionNumber = 0
Expand Down Expand Up @@ -154,8 +155,8 @@ const updateAboutDetails = (tab, tabValue) => {
let location = getBaseUrl(url)

// TODO(bridiver) - refactor these to use state helpers
const ledgerInfo = appState.get('ledgerInfo')
const publisherInfo = appState.get('publisherInfo')
const ledgerInfo = ledgerState.getInfoProps(appState)
const synopsis = appState.getIn(['ledger', 'about'])
const preferencesData = appState.getIn(['about', 'preferences'])
const appSettings = appState.get('settings')
let allSiteSettings = appState.get('siteSettings')
Expand All @@ -176,17 +177,23 @@ const updateAboutDetails = (tab, tabValue) => {
const autofillAddresses = appState.getIn(['autofill', 'addresses'])
const versionInformation = appState.getIn(['about', 'brave', 'versionInformation'])
const aboutDetails = tabValue.get('aboutDetails')
// TODO(bridiver) - convert this to an action

// TODO save this into values into the sate so that we don't call this app action on every state change
// this should be saved in app state when windows will be refactored #11151
/*
if (url === 'about:preferences#payments') {
tab.on('destroyed', () => {
process.emit(messages.LEDGER_PAYMENTS_PRESENT, tabValue.get('tabId'), false)
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
})
process.emit(messages.LEDGER_PAYMENTS_PRESENT, tabValue.get('tabId'), true)
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
} else {
process.emit(messages.LEDGER_PAYMENTS_PRESENT, tabValue.get('tabId'), false)
appActions.ledgerPaymentsPresent(tabValue.get('tabId'), false)
}
*/
if (location === 'about:preferences' || location === 'about:contributions' || location === aboutUrls.get('about:contributions')) {
const ledgerData = ledgerInfo.merge(publisherInfo).merge(preferencesData)
const ledgerData = ledgerInfo
.merge(synopsis)
.merge(preferencesData)
tab.send(messages.LEDGER_UPDATED, ledgerData.toJS())
tab.send(messages.SETTINGS_UPDATED, appSettings.toJS())
tab.send(messages.SITE_SETTINGS_UPDATED, allSiteSettings.toJS())
Expand Down
Loading

0 comments on commit aff082f

Please sign in to comment.