diff --git a/app/browser/reducers/ledgerReducer.js b/app/browser/reducers/ledgerReducer.js index 67ea3b9f422..b650e041213 100644 --- a/app/browser/reducers/ledgerReducer.js +++ b/app/browser/reducers/ledgerReducer.js @@ -519,6 +519,11 @@ const ledgerReducer = (state, action, immutableAction) => { state = aboutPreferencesState.setBackupStatus(state, true) break } + case appConstants.APP_ON_PUBLISHER_TOGGLE_UPDATE: + { + state = ledgerApi.pageDataChanged(state, {}, true) + break + } } return state } diff --git a/app/common/lib/ledgerUtil.js b/app/common/lib/ledgerUtil.js index 129c50b9a8a..b947f95b994 100644 --- a/app/common/lib/ledgerUtil.js +++ b/app/common/lib/ledgerUtil.js @@ -26,6 +26,7 @@ const {responseHasContent} = require('./httpUtil') const urlUtil = require('../../../js/lib/urlutil') const getSetting = require('../../../js/settings').getSetting const urlParse = require('../urlParse') +const appStore = require('../../../js/stores/appStoreRenderer') /** * Is page an actual page being viewed by the user? (not an error page, etc) @@ -591,6 +592,45 @@ const getMediaProvider = (url, firstPartyUrl, referrer) => { return provider } +const shouldSetTimeout = (publisherKey) => { + const minimumVisits = parseInt(getSetting(settings.PAYMENTS_MINIMUM_VISITS)) + + if (minimumVisits === 1) { + return true + } + + const publisher = ledgerState.getPublisher(appStore.state, publisherKey) + const publisherVisits = publisher.get('visits') + + if (publisherVisits === undefined) { + return minimumVisits === 1 + } + + const visitDifference = minimumVisits - publisherVisits + + return (visitDifference === 1) +} + +const getTimeoutWait = (publisherKey) => { + const minimumVisitTime = getSetting(settings.PAYMENTS_MINIMUM_VISIT_TIME) + const publisher = ledgerState.getPublisher(appStore.state, publisherKey) + + if (!publisher) { + return minimumVisitTime + } + + const publisherDuration = publisher.get('duration') + + if ( + publisherDuration === undefined || + publisherDuration >= minimumVisitTime + ) { + return minimumVisitTime + } + + return (minimumVisitTime - publisherDuration) +} + const defaultMonthlyAmounts = Immutable.List([5.0, 7.5, 10.0, 17.5, 25.0, 50.0, 75.0, 100.0]) const milliseconds = { @@ -625,7 +665,9 @@ const getMethods = () => { defaultMonthlyAmounts, getDefaultMediaFavicon, generateMediaCacheData, - shouldShowMenuOption + shouldShowMenuOption, + shouldSetTimeout, + getTimeoutWait } let privateMethods = {} diff --git a/app/renderer/components/navigation/publisherToggle.js b/app/renderer/components/navigation/publisherToggle.js index caa02aefb18..cea9eab03ab 100644 --- a/app/renderer/components/navigation/publisherToggle.js +++ b/app/renderer/components/navigation/publisherToggle.js @@ -32,6 +32,7 @@ const fundUnverifiedPublisherImage = require('../../../extensions/brave/img/urlb class PublisherToggle extends React.Component { constructor (props) { super(props) + this.mounted = false this.onAuthorizePublisher = this.onAuthorizePublisher.bind(this) } @@ -54,6 +55,50 @@ class PublisherToggle extends React.Component { } } + setUpdateTimeout () { + const updateWait = ledgerUtil.getTimeoutWait(this.props.publisherKey) + const shouldSetTimeout = ledgerUtil.shouldSetTimeout(this.props.publisherKey) + if (!this.mounted || !shouldSetTimeout) { + return + } + let updateTimeout = setTimeout(() => { + appActions.onPublisherToggleUpdate() + }, updateWait) + this.setState({updateTimeout: updateTimeout}) + } + + clearUpdateTimeout () { + this.state && clearTimeout(this.state.updateTimeout) + } + + componentDidMount () { + this.mounted = true + if (!this.props.isVisibleInLedger) { + this.setUpdateTimeout() + } + } + + componentWillUnmount () { + this.mounted = false + this.clearUpdateTimeout() + } + + componentWillReceiveProps (newProps) { + if (!this.mounted) { + return + } + if ( + !newProps.isVisibleInLedger && + ( + newProps.location !== this.props.location || + newProps.publisherKey !== this.props.publisherKey + ) + ) { + this.clearUpdateTimeout() + this.setUpdateTimeout() + } + } + mergeProps (state, ownProps) { const currentWindow = state.get('currentWindow') const activeFrame = frameStateUtil.getActiveFrame(currentWindow) || Immutable.Map() @@ -63,6 +108,8 @@ class PublisherToggle extends React.Component { const props = {} // used in renderer + props.location = location + props.publisherKey = publisherKey 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..bd7d735e2f3 100644 --- a/js/actions/appActions.js +++ b/js/actions/appActions.js @@ -1984,6 +1984,12 @@ const appActions = { dispatch({ actionType: appConstants.APP_ON_LEDGER_BACKUP_SUCCESS }) + }, + + onPublisherToggleUpdate: function () { + dispatch({ + actionType: appConstants.APP_ON_PUBLISHER_TOGGLE_UPDATE + }) } } diff --git a/js/constants/appConstants.js b/js/constants/appConstants.js index d050e07ac33..93596a95ec3 100644 --- a/js/constants/appConstants.js +++ b/js/constants/appConstants.js @@ -200,7 +200,8 @@ const appConstants = { APP_ON_REFERRAL_ACTIVITY: _, APP_ON_LEDGER_MEDIA_PUBLISHER: _, APP_ON_LEDGER_BACKUP_SUCCESS: _, - APP_ADD_PUBLISHER_TO_LEDGER: _ + APP_ADD_PUBLISHER_TO_LEDGER: _, + APP_ON_PUBLISHER_TOGGLE_UPDATE: _ } module.exports = mapValuesByKeys(appConstants)