From 93254a6ecfe22a45b8b5e54e68aa9a9ece8e95aa Mon Sep 17 00:00:00 2001 From: ryanml Date: Wed, 25 Apr 2018 23:48:22 -0700 Subject: [PATCH] Adding manual addition functionality to the publisher toggle button --- app/browser/api/ledger.js | 2 +- app/browser/reducers/ledgerReducer.js | 5 ++- .../brave/locales/en-US/app.properties | 2 +- .../components/navigation/publisherToggle.js | 7 ++++ js/actions/appActions.js | 5 ++- .../app/browser/reducers/ledgerReducerTest.js | 37 ++++++++++++++++++- 6 files changed, 51 insertions(+), 7 deletions(-) diff --git a/app/browser/api/ledger.js b/app/browser/api/ledger.js index 3da70a058cb..cc00c72c3da 100644 --- a/app/browser/api/ledger.js +++ b/app/browser/api/ledger.js @@ -851,7 +851,7 @@ const shouldTrackTab = (state, tabId) => { const addNewLocation = (state, location, tabId = tabState.TAB_ID_NONE, keepInfo = false, manualAdd = false) => { // We always want to have the latest active tabId - const currentTabId = manualAdd ? tabState.TAB_ID_NONE : pageDataState.getLastActiveTabId(state) + const currentTabId = manualAdd ? tabId : pageDataState.getLastActiveTabId(state) state = pageDataState.setLastActiveTabId(state, tabId) if (location === currentUrl && !manualAdd) { return state diff --git a/app/browser/reducers/ledgerReducer.js b/app/browser/reducers/ledgerReducer.js index 67ea3b9f422..0ec59546bed 100644 --- a/app/browser/reducers/ledgerReducer.js +++ b/app/browser/reducers/ledgerReducer.js @@ -175,13 +175,16 @@ const ledgerReducer = (state, action, immutableAction) => { } case appConstants.APP_ADD_PUBLISHER_TO_LEDGER: { + const tabId = action.get('tabId') const location = action.get('location') if (!location) { break } - state = ledgerApi.addNewLocation(state, location, tabState.TAB_ID_NONE, false, true) + const passedTabId = tabId || tabState.TAB_ID_NONE + + state = ledgerApi.addNewLocation(state, location, passedTabId, false, true) state = ledgerApi.pageDataChanged(state, {}, true) break } diff --git a/app/extensions/brave/locales/en-US/app.properties b/app/extensions/brave/locales/en-US/app.properties index bdb19347c82..ef89a4df0b6 100644 --- a/app/extensions/brave/locales/en-US/app.properties +++ b/app/extensions/brave/locales/en-US/app.properties @@ -162,7 +162,7 @@ notificationTryPaymentsYes=Sure, I'll try notificationUpdatePassword=Would you like Brave to update your password on {{origin}}? notificationUpdatePasswordWithUserName=Would you like Brave to update the password for {{username}} on {{origin}}? notNow=Not Now -notVisiblePublisher.title=Publisher is not yet added to the ledger, because it doesn't meet criteria yet. +notVisiblePublisher.title=Publisher is not yet added to the ledger, click to include in Brave Payments. ok=OK openTypeInNewPrivateTab=Open {{type}} in a new private tab openTypeInNewTab=Open {{type}} in a new tab diff --git a/app/renderer/components/navigation/publisherToggle.js b/app/renderer/components/navigation/publisherToggle.js index caa02aefb18..1dc70bab9bb 100644 --- a/app/renderer/components/navigation/publisherToggle.js +++ b/app/renderer/components/navigation/publisherToggle.js @@ -14,6 +14,7 @@ const BrowserButton = require('../common/browserButton') const appActions = require('../../../../js/actions/appActions') // State +const tabState = require('../../../common/state/tabState') const ledgerState = require('../../../common/state/ledgerState') // Utils @@ -51,18 +52,24 @@ class PublisherToggle extends React.Component { onAuthorizePublisher () { if (this.props.isVisibleInLedger) { appActions.changeSiteSetting(this.props.hostPattern, 'ledgerPayments', !this.props.isEnabledForPaymentsPublisher) + } else { + appActions.addPublisherToLedger(this.props.location, this.props.tabId) + appActions.changeSiteSetting(this.props.hostPattern, 'ledgerPayments', true) } } mergeProps (state, ownProps) { const currentWindow = state.get('currentWindow') const activeFrame = frameStateUtil.getActiveFrame(currentWindow) || Immutable.Map() + const tabId = activeFrame.get('tabId', tabState.TAB_ID_NONE) const location = activeFrame.get('location', '') const locationId = getBaseUrl(location) const publisherKey = ledgerState.getVerifiedPublisherLocation(state, locationId) const props = {} // used in renderer + props.tabId = tabId + props.location = location props.isVisibleInLedger = ledgerUtil.visibleP(state, publisherKey) props.isEnabledForPaymentsPublisher = ledgerUtil.stickyP(state, publisherKey) props.isVerifiedPublisher = ledgerState.getPublisherOption(state, publisherKey, 'verified') diff --git a/js/actions/appActions.js b/js/actions/appActions.js index 560ca02ff07..7d5875b8ab6 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -1834,10 +1834,11 @@ const appActions = { * Dispatches a message to add a given publisher to the ledger. * @param location - the URL of the publisher */ - addPublisherToLedger: function (location) { + addPublisherToLedger: function (location, tabId = false) { dispatch({ actionType: appConstants.APP_ADD_PUBLISHER_TO_LEDGER, - location + location, + tabId }) }, diff --git a/test/unit/app/browser/reducers/ledgerReducerTest.js b/test/unit/app/browser/reducers/ledgerReducerTest.js index 6f33b8f4fe7..1ecbd60b820 100644 --- a/test/unit/app/browser/reducers/ledgerReducerTest.js +++ b/test/unit/app/browser/reducers/ledgerReducerTest.js @@ -722,6 +722,8 @@ describe('ledgerReducer unit tests', function () { describe('APP_ADD_PUBLISHER_TO_LEDGER', function () { let addNewLocationSpy let pageDataChangedSpy + const tabIdNone = -1 + const testTabId = 7 beforeEach(function () { addNewLocationSpy = sinon.spy(fakeLedgerApi, 'addNewLocation') @@ -736,7 +738,8 @@ describe('ledgerReducer unit tests', function () { it('executes', function () { const newState = ledgerReducer(appState, Immutable.fromJS({ actionType: appConstants.APP_ADD_PUBLISHER_TO_LEDGER, - location: 'https://brave.com' + location: 'https://brave.com', + tabId: false })) assert(addNewLocationSpy.calledOnce) assert(pageDataChangedSpy.calledOnce) @@ -746,11 +749,41 @@ describe('ledgerReducer unit tests', function () { it('takes no action with a null location', function () { const newState = ledgerReducer(appState, Immutable.fromJS({ actionType: appConstants.APP_ADD_PUBLISHER_TO_LEDGER, - location: null + location: null, + tabId: false })) assert(addNewLocationSpy.notCalled) assert(pageDataChangedSpy.notCalled) assert.deepEqual(newState, appState) }) + + it('tab Id passed is equal to tabState.TAB_ID_NONE when not added via toggle', function () { + ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ADD_PUBLISHER_TO_LEDGER, + location: 'https://brave.com', + tabId: false + })) + assert.equal(tabIdNone, addNewLocationSpy.getCall(0).args[2]) + }) + + it('executes when dispatched with a tab id', function () { + const newState = ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ADD_PUBLISHER_TO_LEDGER, + location: 'https://brave.com', + tabId: testTabId + })) + assert(addNewLocationSpy.calledOnce) + assert(pageDataChangedSpy.calledOnce) + assert.notDeepEqual(newState, appState) + }) + + it('tab Id passed to addNewLocation is equal to passed tabId', function () { + ledgerReducer(appState, Immutable.fromJS({ + actionType: appConstants.APP_ADD_PUBLISHER_TO_LEDGER, + location: 'https://brave.com', + tabId: testTabId + })) + assert.equal(testTabId, addNewLocationSpy.getCall(0).args[2]) + }) }) })