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

Commit

Permalink
Fixes #12675, enabling publisher toggle after time requirements satis…
Browse files Browse the repository at this point in the history
…fied
  • Loading branch information
ryanml committed Apr 26, 2018
1 parent a2256bc commit ec69a3c
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/browser/reducers/ledgerReducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
44 changes: 43 additions & 1 deletion app/common/lib/ledgerUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -625,7 +665,9 @@ const getMethods = () => {
defaultMonthlyAmounts,
getDefaultMediaFavicon,
generateMediaCacheData,
shouldShowMenuOption
shouldShowMenuOption,
shouldSetTimeout,
getTimeoutWait
}

let privateMethods = {}
Expand Down
47 changes: 47 additions & 0 deletions app/renderer/components/navigation/publisherToggle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand All @@ -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()
Expand All @@ -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')
Expand Down
6 changes: 6 additions & 0 deletions js/actions/appActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -1984,6 +1984,12 @@ const appActions = {
dispatch({
actionType: appConstants.APP_ON_LEDGER_BACKUP_SUCCESS
})
},

onPublisherToggleUpdate: function () {
dispatch({
actionType: appConstants.APP_ON_PUBLISHER_TOGGLE_UPDATE
})
}
}

Expand Down
3 changes: 2 additions & 1 deletion js/constants/appConstants.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit ec69a3c

Please sign in to comment.