Skip to content

Commit

Permalink
Merge pull request #3876 from brave/no-ads-ntp
Browse files Browse the repository at this point in the history
Adding state for unsupported ads regions to ntp rewards widget
  • Loading branch information
ryanml authored Nov 7, 2019
2 parents a315f9a + 1a25661 commit f47e66f
Show file tree
Hide file tree
Showing 11 changed files with 96 additions and 16 deletions.
19 changes: 19 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1084,5 +1084,24 @@ void BraveRewardsGetWalletExistsFunction::OnGetWalletExists(
Respond(OneArgument(std::make_unique<base::Value>(exists)));
}

BraveRewardsGetAdsSupportedFunction::
~BraveRewardsGetAdsSupportedFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetAdsSupportedFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
AdsService* ads_service_ =
AdsServiceFactory::GetForProfile(profile);

if (!ads_service_) {
return RespondNow(Error("Ads service is not initialized"));
}

const bool supported = ads_service_->IsSupportedLocale();
return RespondNow(
OneArgument(std::make_unique<base::Value>(supported)));
}

} // namespace api
} // namespace extensions
10 changes: 10 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,16 @@ class BraveRewardsGetWalletExistsFunction
void OnGetWalletExists(const bool exists);
};

class BraveRewardsGetAdsSupportedFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getAdsSupported", UNKNOWN)

protected:
~BraveRewardsGetAdsSupportedFunction() override;

ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

Expand Down
1 change: 1 addition & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "rewardsWidgetEnableTitle", IDS_REWARDS_WIDGET_ENABLE_TITLE },
{ "rewardsWidgetReEnableSubTitle", IDS_REWARDS_WIDGET_REENABLE_SUBTITLE }, // NOLINT
{ "rewardsWidgetEnableSubTitle", IDS_REWARDS_WIDGET_ENABLE_SUBTITLE }, // NOLINT
{ "rewardsWidgetAdsNotSupported", IDS_BRAVE_REWARDS_LOCAL_ADS_NOT_SUPPORTED }, // NOLINT
{ "rewardsWidgetNotificationTitle", IDS_REWARDS_WIDGET_NOTIFICATION_TITLE }, // NOLINT
{ "rewardsWidgetNotificationTextAds", IDS_REWARDS_WIDGET_NOTIFICATION_TEXT_ADS }, // NOLINT
{ "rewardsWidgetNotificationTextUGP", IDS_REWARDS_WIDGET_NOTIFICATION_TEXT_UGP } // NOLINT
Expand Down
17 changes: 17 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -656,6 +656,23 @@
}
]
},
{
"name": "getAdsSupported",
"type": "function",
"description": "Gets whether ads is supported in the user's region",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "supported",
"type": "boolean"
}
]
}
]
},
{
"name": "getAdsEstimatedEarnings",
"type": "function",
Expand Down
6 changes: 6 additions & 0 deletions components/brave_new_tab_ui/api/initialData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type InitialData = {

export type PreInitialRewardsData = {
enabledAds: boolean
adsSupported: boolean
enabledMain: boolean
}

Expand Down Expand Up @@ -54,17 +55,22 @@ export async function getRewardsPreInitialData (): Promise<PreInitialRewardsData
try {
const [
enabledAds,
adsSupported,
enabledMain
] = await Promise.all([
new Promise(resolve => chrome.braveRewards.getAdsEnabled((enabledAds: boolean) => {
resolve(enabledAds)
})),
new Promise(resolve => chrome.braveRewards.getAdsSupported((adsSupported: boolean) => {
resolve(adsSupported)
})),
new Promise(resolve => chrome.braveRewards.getRewardsMainEnabled((enabledMain: boolean) => {
resolve(enabledMain)
}))
])
return {
enabledAds,
adsSupported,
enabledMain
} as PreInitialRewardsData
} catch (err) {
Expand Down
34 changes: 22 additions & 12 deletions components/brave_new_tab_ui/components/default/rewards/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ import {
Amount,
ConvertedAmount,
LearnMoreLink,
TurnOnAdsButton
TurnOnAdsButton,
UnsupportedMessage
} from './style'
import Notification from './notification'
import { BatColorIcon } from 'brave-ui/components/icons'
Expand All @@ -43,6 +44,7 @@ export interface RewardsProps {
walletCorrupted: boolean
adsEstimatedEarnings: number
onlyAnonWallet?: boolean
adsSupported?: boolean
onCreateWallet: () => void
onEnableAds: () => void
onEnableRewards: () => void
Expand Down Expand Up @@ -135,19 +137,27 @@ class Rewards extends React.PureComponent<RewardsProps, {}> {
onEnableAds,
adsEstimatedEarnings,
onlyAnonWallet,
totalContribution
totalContribution,
adsSupported
} = this.props

const rates = balance.rates || {}
const showEnableAds = type === AmountItemType.ADS && !enabledAds
const showEnableAds = type === AmountItemType.ADS && !enabledAds && adsSupported
const amount = type === AmountItemType.TIPS
? totalContribution
: adsEstimatedEarnings.toFixed(1)
const converted = convertBalance(amount, rates)
const batFormatString = onlyAnonWallet ? getLocale('rewardsWidgetBatPoints') : getLocale('rewardsWidgetBat')

return (
<AmountItem>
<AmountItem isLast={type === AmountItemType.TIPS}>
<AmountDescription>
{
type === AmountItemType.ADS
? getLocale('rewardsWidgetEstimatedEarnings')
: getLocale('rewardsWidgetMonthlyTips')
}
</AmountDescription>
{
showEnableAds
? <TurnOnAdsButton onClick={onEnableAds}>
Expand All @@ -156,7 +166,7 @@ class Rewards extends React.PureComponent<RewardsProps, {}> {
: null
}
{
!showEnableAds
!showEnableAds && !(type === AmountItemType.ADS && !adsSupported)
? <AmountInformation data-test-id={`widget-amount-total-${type}`}>
<Amount>{amount}</Amount>
<ConvertedAmount>
Expand All @@ -165,13 +175,13 @@ class Rewards extends React.PureComponent<RewardsProps, {}> {
</AmountInformation>
: null
}
<AmountDescription>
{
type === AmountItemType.ADS
? getLocale('rewardsWidgetEstimatedEarnings')
: getLocale('rewardsWidgetMonthlyTips')
}
</AmountDescription>
{
type === AmountItemType.ADS && !adsSupported
? <UnsupportedMessage>
{getLocale('rewardsWidgetAdsNotSupported')}
</UnsupportedMessage>
: null
}
</AmountItem>
)
}
Expand Down
21 changes: 17 additions & 4 deletions components/brave_new_tab_ui/components/default/rewards/style.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import styled from 'styled-components'
import palette from 'brave-ui/theme/colors'

interface StyleProps {
isLast?: boolean
}

export const WidgetWrapper = styled<{}, 'div'>('div')`
color: white;
padding: 10px 15px;
Expand Down Expand Up @@ -93,16 +97,17 @@ export const TurnOnButton = styled<{}, 'button'>('button')`

export const TurnOnAdsButton = styled(TurnOnButton)`
width: 100%;
margin-bottom: 5px;
margin-top: 8px;
display: block;
`

export const AmountItem = styled<{}, 'div'>('div')`
margin-top: 10px;
export const AmountItem = styled<StyleProps, 'div'>('div')`
margin-top: ${p => p.isLast ? 20 : 10}px;
margin-bottom: ${p => p.isLast ? -10 : 0}px;
`

export const AmountInformation = styled<{}, 'div'>('div')`
margin-bottom: 5px;
margin-top: 5px;
`

export const Amount = styled<{}, 'span'>('span')`
Expand All @@ -118,6 +123,7 @@ export const ConvertedAmount = styled<{}, 'span'>('span')`
export const AmountDescription = styled<{}, 'span'>('span')`
font-size: 14px;
color: #fff;
margin-bottom: 5px;
`

export const NotificationWrapper = styled(WidgetWrapper)`
Expand Down Expand Up @@ -151,3 +157,10 @@ export const CloseIcon = styled<{}, 'div'>('div')`
float: right;
cursor: pointer;
`

export const UnsupportedMessage = styled<{}, 'div'>('div')`
color: rgba(255, 255, 255, 0.70);
font-size: 14px;
max-width: 235px;
margin-top: 8px;
`
1 change: 1 addition & 0 deletions components/brave_new_tab_ui/reducers/new_tab_reducer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,7 @@ export const newTabReducer: Reducer<NewTab.State | undefined> = (state: NewTab.S
rewardsState: {
...state.rewardsState,
enabledAds: preInitialRewardsDataPayload.enabledAds,
adsSupported: preInitialRewardsDataPayload.adsSupported,
enabledMain: preInitialRewardsDataPayload.enabledMain
}
}
Expand Down
1 change: 1 addition & 0 deletions components/brave_new_tab_ui/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const defaultState: NewTab.State = {
},
dismissedNotifications: [],
enabledAds: false,
adsSupported: false,
enabledMain: false,
grants: [],
onlyAnonWallet: false,
Expand Down
1 change: 1 addition & 0 deletions components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ declare namespace chrome.braveRewards {
addListener: (callback: (enabledMain: boolean) => void) => void
}
const getAdsEnabled: (callback: (enabled: boolean) => void) => {}
const getAdsSupported: (callback: (supported: boolean) => void) => {}
const getBalanceReports: (callback: (reports: Record<string, Rewards.Report>) => void) => {}
const getAdsEstimatedEarnings: (callback: (amount: number) => void) => {}
const getRewardsMainEnabled: (callback: (enabled: boolean) => void) => {}
Expand Down
1 change: 1 addition & 0 deletions components/definitions/newTab.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ declare namespace NewTab {

export interface RewardsWidgetState {
adsEstimatedEarnings: number
adsSupported?: boolean
balance: RewardsBalance
dismissedNotifications: string[]
enabledAds: boolean
Expand Down

0 comments on commit f47e66f

Please sign in to comment.