Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separates monthly and one time tips in to two panels #3394

Merged
merged 1 commit into from
Sep 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -258,6 +258,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "donationTitle", IDS_BRAVE_REWARDS_LOCAL_DONAT_TITLE },
{ "donationDesc", IDS_BRAVE_REWARDS_LOCAL_DONAT_DESC },
{ "donationTotalDonations", IDS_BRAVE_REWARDS_LOCAL_DONAT_TOTAL_DONATIONS }, // NOLINT
{ "donationTotalMonthlyContribution", IDS_BRAVE_REWARDS_LOCAL_DONAT_TOTAL_MONTHLY_CONTRIBUTION }, // NOLINT
{ "donationVisitSome", IDS_BRAVE_REWARDS_LOCAL_DONAT_VISIT_SOME },
{ "donationAbility", IDS_BRAVE_REWARDS_LOCAL_DONAT_ABILITY },
{ "donationAbilityYT", IDS_BRAVE_REWARDS_LOCAL_DONAT_ABILITY_YT },
Expand All @@ -267,6 +268,10 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "donationDisabledText1", IDS_BRAVE_REWARDS_LOCAL_DONAT_DISABLED_TEXT1 }, // NOLINT
{ "donationDisabledText2", IDS_BRAVE_REWARDS_LOCAL_DONAT_DISABLED_TEXT2 }, // NOLINT
{ "donationNextDate", IDS_BRAVE_REWARDS_LOCAL_DONAT_NEXT_DATE },
{ "monthlyContributionTitle", IDS_BRAVE_REWARDS_LOCAL_MONTHLY_CONTRIBUTION_TITLE }, // NOLINT
{ "monthlyContributionDesc", IDS_BRAVE_REWARDS_LOCAL_MONTHLY_CONTRIBUTION_DESC }, // NOLINT
{ "monthlyContributionEmpty", IDS_BRAVE_REWARDS_LOCAL_MONTHLY_CONTRIBUTION_EMPTY }, // NOLINT
{ "monthlyContributionDisabledText", IDS_BRAVE_REWARDS_LOCAL_MONTHLY_CONTRIBUTION_DISABLED_TEXT }, // NOLINT

{ "panelAddFunds", IDS_BRAVE_REWARDS_LOCAL_PANEL_ADD_FUNDS },
{ "panelWithdrawFunds", IDS_BRAVE_REWARDS_LOCAL_PANEL_WITHDRAW_FUNDS },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -993,7 +993,7 @@ class BraveRewardsBrowserTest :
contents(),
"const delay = t => new Promise(resolve => setTimeout(resolve, t));"
"delay(0).then(() => "
" document.querySelector(\"[type='donation']\")"
" document.querySelectorAll(\"[type='donation']\")[1]"
" .parentElement.parentElement.innerText);",
content::EXECUTE_SCRIPT_DEFAULT_OPTIONS,
content::ISOLATED_WORLD_ID_CONTENT_END);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

import * as React from 'react'
import { bindActionCreators, Dispatch } from 'redux'
import { connect } from 'react-redux'

import {
DisabledContent,
Box,
TableDonation,
List,
Tokens,
ModalDonation,
NextContribution
} from '../../ui/components'
import { Provider } from '../../ui/components/profile'

import { getLocale } from '../../../../common/locale'
import * as rewardsActions from '../actions/rewards_actions'
import * as utils from '../utils'
import { DetailRow } from '../../ui/components/tableDonation'

interface Props extends Rewards.ComponentProps {
}

interface State {
modalShowAll: boolean
}

class MonthlyContributionBox extends React.Component<Props, State> {
constructor (props: Props) {
super(props)
this.state = {
modalShowAll: false
}
}

get actions () {
return this.props.actions
}

disabledContent = () => {
return (
<DisabledContent
type={'monthly'}
>
{getLocale('monthlyContributionDisabledText')}
</DisabledContent>
)
}

getRows = () => {
const { balance, recurringList } = this.props.rewardsData
let recurring: DetailRow[] = []

if (!recurringList) {
return recurring
}

return recurringList.map((item: Rewards.Publisher) => {
let faviconUrl = `chrome://favicon/size/48@1x/${item.url}`
const verified = utils.isPublisherConnectedOrVerified(item.status)

if (item.favIcon && verified) {
faviconUrl = `chrome://favicon/size/48@1x/${item.favIcon}`
}

return {
profile: {
name: item.name,
verified,
provider: (item.provider ? item.provider : undefined) as Provider,
src: faviconUrl
},
contribute: {
tokens: item.percentage.toFixed(1),
converted: utils.convertBalance(item.percentage.toString(), balance.rates)
},
url: item.url,
type: 'recurring' as any,
onRemove: () => { this.actions.removeRecurringTip(item.id) }
}
})
}

onModalToggle = () => {
this.setState({
modalShowAll: !this.state.modalShowAll
})
}

render () {
const {
balance,
firstLoad,
enabledMain,
recurringList,
reconcileStamp
} = this.props.rewardsData
const showDisabled = firstLoad !== false || !enabledMain
const tipRows = this.getRows()
const topRows = tipRows.slice(0, 5)
const numRows = tipRows && tipRows.length
const allSites = !(numRows > 5)
const total = utils.tipsListTotal(recurringList)
const converted = utils.convertBalance(total, balance.rates)

return (
<Box
type={'donation'}
title={getLocale('monthlyContributionTitle')}
description={getLocale('monthlyContributionDesc')}
disabledContent={showDisabled ? this.disabledContent() : null}
>
{
this.state.modalShowAll
? <ModalDonation
rows={tipRows}
onClose={this.onModalToggle}
title={getLocale('monthlyContributionTitle')}
/>
: null
}
<List title={getLocale('donationTotalMonthlyContribution')}>
<Tokens value={total} converted={converted} />
</List>
<List title={getLocale('donationNextDate')}>
<NextContribution>
{new Intl.DateTimeFormat('default', { month: 'short', day: 'numeric' }).format(reconcileStamp * 1000)}
</NextContribution>
</List>

<TableDonation
rows={topRows}
allItems={allSites}
numItems={numRows}
headerColor={true}
onShowAll={this.onModalToggle}
>
{getLocale('monthlyContributionEmpty')}
</TableDonation>
</Box>
)
}
}

const mapStateToProps = (state: Rewards.ApplicationState) => ({
rewardsData: state.rewardsData
})

const mapDispatchToProps = (dispatch: Dispatch) => ({
actions: bindActionCreators(rewardsActions, dispatch)
})

export default connect(
mapStateToProps,
mapDispatchToProps
)(MonthlyContributionBox)
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import PageWallet from './pageWallet'
import AdsBox from './adsBox'
import ContributeBox from './contributeBox'
import TipBox from './tipsBox'
import MonthlyContributionBox from './monthlyContributionBox'

// Utils
import * as rewardsActions from '../actions/rewards_actions'
Expand Down Expand Up @@ -244,6 +245,7 @@ class SettingsPage extends React.Component<Props, State> {
}
<AdsBox />
<ContributeBox />
<MonthlyContributionBox />
<TipBox />
</Column>
<Column size={1} customStyle={{ justifyContent: 'center', flexWrap: 'wrap' }}>
Expand Down
120 changes: 33 additions & 87 deletions components/brave_rewards/resources/page/components/tipsBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ import {
TableDonation,
List,
Tokens,
ModalDonation,
NextContribution
ModalDonation
} from '../../ui/components'
import { Provider } from '../../ui/components/profile'

Expand Down Expand Up @@ -58,83 +57,40 @@ class TipBox extends React.Component<Props, State> {
)
}

getTotal = () => {
const { reports } = this.props.rewardsData

const currentTime = new Date()
const reportKey = `${currentTime.getFullYear()}_${currentTime.getMonth() + 1}`
const report: Rewards.Report = reports[reportKey]

if (report) {
return utils.tipsTotal(report)
}

return '0.0'
}

getTipsRows = () => {
const { balance, recurringList, tipsList } = this.props.rewardsData

// Recurring
let recurring: DetailRow[] = []
if (recurringList) {
recurring = recurringList.map((item: Rewards.Publisher) => {
let faviconUrl = `chrome://favicon/size/48@1x/${item.url}`
const verified = utils.isPublisherConnectedOrVerified(item.status)
if (item.favIcon && verified) {
faviconUrl = `chrome://favicon/size/48@1x/${item.favIcon}`
}

return {
profile: {
name: item.name,
verified,
provider: (item.provider ? item.provider : undefined) as Provider,
src: faviconUrl
},
contribute: {
tokens: item.percentage.toFixed(1),
converted: utils.convertBalance(item.percentage.toString(), balance.rates)
},
url: item.url,
type: 'recurring' as any,
onRemove: () => { this.actions.removeRecurringTip(item.id) }
}
})
}

// Tips
const { balance, tipsList } = this.props.rewardsData
let tips: DetailRow[] = []
if (tipsList) {
tips = tipsList.map((item: Rewards.Publisher) => {
let faviconUrl = `chrome://favicon/size/48@1x/${item.url}`
const verified = utils.isPublisherConnectedOrVerified(item.status)
if (item.favIcon && verified) {
faviconUrl = `chrome://favicon/size/48@1x/${item.favIcon}`
}

const token = utils.convertProbiToFixed(item.percentage.toString())

return {
profile: {
name: item.name,
verified,
provider: (item.provider ? item.provider : undefined) as Provider,
src: faviconUrl
},
contribute: {
tokens: token,
converted: utils.convertBalance(token, balance.rates)
},
url: item.url,
text: item.tipDate ? new Date(item.tipDate * 1000).toLocaleDateString() : undefined,
type: 'donation' as any,
onRemove: () => { this.actions.removeRecurringTip(item.id) }
}
})
if (!tipsList) {
return tips
}

return recurring.concat(tips)
return tipsList.map((item: Rewards.Publisher) => {
let faviconUrl = `chrome://favicon/size/48@1x/${item.url}`
const verified = utils.isPublisherConnectedOrVerified(item.status)
if (item.favIcon && verified) {
faviconUrl = `chrome://favicon/size/48@1x/${item.favIcon}`
}

const token = utils.convertProbiToFixed(item.percentage.toString())

return {
profile: {
name: item.name,
verified,
provider: (item.provider ? item.provider : undefined) as Provider,
src: faviconUrl
},
contribute: {
tokens: token,
converted: utils.convertBalance(token, balance.rates)
},
url: item.url,
text: item.tipDate ? new Date(item.tipDate * 1000).toLocaleDateString() : undefined,
type: 'donation' as any,
onRemove: () => { this.actions.removeRecurringTip(item.id) }
}
})
}

onModalToggle = () => {
Expand Down Expand Up @@ -219,16 +175,15 @@ class TipBox extends React.Component<Props, State> {
firstLoad,
enabledMain,
ui,
recurringList,
reconcileStamp
tipsList
} = this.props.rewardsData
const { walletImported } = ui
const showDisabled = firstLoad !== false || !enabledMain
const tipRows = this.getTipsRows()
const topRows = tipRows.slice(0, 5)
const numRows = tipRows && tipRows.length
const allSites = !(numRows > 5)
const total = this.getTotal()
const total = utils.tipsListTotal(tipsList, true)
const converted = utils.convertBalance(total, balance.rates)

return (
Expand All @@ -247,22 +202,13 @@ class TipBox extends React.Component<Props, State> {
? <ModalDonation
rows={tipRows}
onClose={this.onModalToggle}
title={getLocale('donationTips')}
/>
: null
}
<List title={getLocale('donationTotalDonations')}>
<Tokens value={total} converted={converted} />
</List>
{
recurringList && recurringList.length > 0
? <List title={getLocale('donationNextDate')}>
<NextContribution>
{new Intl.DateTimeFormat('default', { month: 'short', day: 'numeric' }).format(reconcileStamp * 1000)}
</NextContribution>
</List>
: null
}

<TableDonation
rows={topRows}
allItems={allSites}
Expand Down
18 changes: 18 additions & 0 deletions components/brave_rewards/resources/page/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ export const tipsTotal = (report: Rewards.Report) => {
return new BigNumber(report.donation).plus(tips).dividedBy('1e18').toFixed(1, BigNumber.ROUND_DOWN)
}

export const tipsListTotal = (list: Rewards.Publisher[], convertProbi = false) => {
if (list.length === 0) {
return '0.0'
}

let tipsTotal: number = 0

list.map((item: Rewards.Publisher) => {
if (convertProbi) {
tipsTotal += parseFloat(convertProbiToFixed(item.percentage.toString()))
} else {
tipsTotal += item.percentage
}
})

return tipsTotal.toFixed(1)
}

export const constructBackupString = (backupKey: string) => {
return `Brave Wallet Recovery Key\nDate created: ${new Date(Date.now()).toLocaleDateString()} \n\nRecovery Key: ${backupKey}` +
'\n\nNote: This key is not stored on Brave servers. ' +
Expand Down
Loading