Skip to content

Commit

Permalink
Merge pull request #3552 from brave/fix-6182
Browse files Browse the repository at this point in the history
Fixing incorrect verified notice behavior in tipping banner
  • Loading branch information
ryanml authored Oct 1, 2019
2 parents 72357f3 + 77fa8b3 commit d5f0890
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 5 deletions.
48 changes: 48 additions & 0 deletions browser/ui/webui/brave_tip_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class RewardsTipDOMHandler : public WebUIMessageHandler,
void OnGetRecurringTips(
std::unique_ptr<brave_rewards::ContentSiteList> list);
void TweetTip(const base::ListValue *args);
void GetExternalWallet(const base::ListValue* args);
void OnExternalWallet(
const int32_t result,
std::unique_ptr<brave_rewards::ExternalWallet> wallet);

void OnPublisherBanner(
std::unique_ptr<brave_rewards::PublisherBanner> banner);
Expand Down Expand Up @@ -136,6 +140,11 @@ void RewardsTipDOMHandler::RegisterMessages() {
"brave_rewards_tip.fetchBalance",
base::BindRepeating(&RewardsTipDOMHandler::FetchBalance,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards_tip.getExternalWallet",
base::BindRepeating(
&RewardsTipDOMHandler::GetExternalWallet,
base::Unretained(this)));
}

void RewardsTipDOMHandler::GetPublisherTipData(
Expand Down Expand Up @@ -421,3 +430,42 @@ void RewardsTipDOMHandler::FetchBalance(const base::ListValue* args) {
weak_factory_.GetWeakPtr()));
}
}

void RewardsTipDOMHandler::GetExternalWallet(
const base::ListValue* args) {
if (!rewards_service_) {
return;
}

const std::string type = args->GetList()[0].GetString();

rewards_service_->GetExternalWallet(type,
base::BindOnce(
&RewardsTipDOMHandler::OnExternalWallet,
weak_factory_.GetWeakPtr()));
}

void RewardsTipDOMHandler::OnExternalWallet(
const int32_t result,
std::unique_ptr<brave_rewards::ExternalWallet> wallet) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::DictionaryValue data;

if (wallet) {
data.SetString("token", wallet->token);
data.SetString("address", wallet->address);
data.SetString("type", wallet->type);
data.SetString("verifyUrl", wallet->verify_url);
data.SetString("addUrl", wallet->add_url);
data.SetString("withdrawUrl", wallet->withdraw_url);
data.SetString("userName", wallet->user_name);
data.SetString("accountUrl", wallet->account_url);
data.SetInteger("status", static_cast<int>(wallet->status));
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_tip.externalWallet", data);
}
6 changes: 6 additions & 0 deletions components/brave_rewards/resources/tip/actions/tip_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,9 @@ export const onBalance = (status: number, balance: RewardsTip.Balance) => action
status,
balance
})

export const getExternalWallet = () => action(types.GET_EXTERNAL_WALLET)

export const onExternalWallet = (wallet: RewardsTip.ExternalWallet) => action(types.ON_EXTERNAL_WALLET, {
wallet
})
7 changes: 6 additions & 1 deletion components/brave_rewards/resources/tip/brave_rewards_tip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ window.cr.define('brave_rewards_tip', function () {
getActions().onBalance(properties.status, properties.balance)
}

function externalWallet (wallet: RewardsTip.ExternalWallet) {
getActions().onExternalWallet(wallet)
}

return {
initialize,
publisherBanner,
Expand All @@ -94,7 +98,8 @@ window.cr.define('brave_rewards_tip', function () {
reconcileStamp,
recurringTipRemoved,
recurringTipSaved,
balance
balance,
externalWallet
}
})

Expand Down
28 changes: 25 additions & 3 deletions components/brave_rewards/resources/tip/components/siteBanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Banner extends React.Component<Props, State> {
this.actions.getBalance()
this.actions.getRecurringTips()
this.actions.getReconcileStamp()
this.actions.getExternalWallet()
}

get actions () {
Expand Down Expand Up @@ -182,15 +183,36 @@ class Banner extends React.Component<Props, State> {
return new Intl.DateTimeFormat('default', fmtArgs).format(reconcileStamp * 1000)
}

shouldShowConnectedMessage = () => {
const publisher = this.props.publisher
const { externalWallet, balance } = this.props.rewardsDonateData
const { wallets } = balance
const notVerified = publisher && utils.isPublisherNotVerified(publisher.status)
const connected = publisher && utils.isPublisherConnected(publisher.status)
const status = utils.getWalletStatus(externalWallet)

if (notVerified) {
return true
}

if (connected && (status === 'unverified' ||
status === 'disconnected_unverified' ||
status === 'disconnected_verified')) {
return false
}

const hasAnonBalance = wallets['anonymous'] && wallets['anonymous'] > 0

return connected && !hasAnonBalance
}

render () {
const { balance } = this.props.rewardsDonateData
const { total } = balance

const mediaMetaData = this.props.mediaMetaData
const publisher = this.props.publisher
const checkmark = utils.isPublisherConnectedOrVerified(publisher.status)
const verified = utils.isPublisherVerified(publisher.status)
const connected = utils.isPublisherConnected(publisher.status)
const bannerType = this.props.monthly ? 'monthly' : 'one-time'
let logo = publisher.logo

Expand Down Expand Up @@ -220,7 +242,7 @@ class Banner extends React.Component<Props, State> {
currentAmount={this.state.currentAmount || '0'}
onClose={this.onClose}
social={this.generateSocialLinks()}
showUnVerifiedNotice={!verified || connected}
showUnVerifiedNotice={this.shouldShowConnectedMessage()}
isVerified={checkmark}
learnMoreNotice={'https://brave.com/faq/#unclaimed-funds'}
addFundsLink={this.addFundsLink}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ export const enum types {
ON_RECURRING_TIP_REMOVED = '@@rewards/ON_RECURRING_TIP_REMOVED',
ON_RECURRING_TIP_SAVED = '@@rewards/ON_RECURRING_TIP_SAVED',
GET_BALANCE = '@@rewards/GET_BALANCE',
ON_BALANCE = '@@rewards/ON_BALANCE'
ON_BALANCE = '@@rewards/ON_BALANCE',
GET_EXTERNAL_WALLET = '@@rewards/GET_EXTERNAL_WALLET',
ON_EXTERNAL_WALLET = '@@rewards/ON_EXTERNAL_WALLET'
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ const publishersReducer: Reducer<RewardsTip.State> = (state: RewardsTip.State =
}
break
}
case types.GET_EXTERNAL_WALLET: {
chrome.send('brave_rewards_tip.getExternalWallet', ['uphold'])
break
}
case types.ON_EXTERNAL_WALLET: {
state = { ...state }
state.externalWallet = payload.wallet
break
}
}

return state
Expand Down
19 changes: 19 additions & 0 deletions components/brave_rewards/resources/tip/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,22 @@ export const isPublisherConnectedOrVerified = (status: Rewards.PublisherStatus)
export const isPublisherNotVerified = (status: Rewards.PublisherStatus) => {
return status === 0
}

export const getWalletStatus = (externalWallet?: RewardsTip.ExternalWallet) => {
if (!externalWallet) {
return 'unverified'
}

switch (externalWallet.status) {
case 1:
return 'connected'
case 2:
return 'verified'
case 3:
return 'disconnected_unverified'
case 4:
return 'disconnected_verified'
default:
return 'unverified'
}
}
23 changes: 23 additions & 0 deletions components/definitions/rewardsTip.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ declare namespace RewardsTip {
recurringDonations?: RecurringTips[] // TODO(nejczdovc): migrate to tips
reconcileStamp: number
balance: Balance
externalWallet?: ExternalWallet
}

interface ApplicationState {
Expand Down Expand Up @@ -92,4 +93,26 @@ declare namespace RewardsTip {
rates: Record<string, number>
wallets: Record<string, number>
}

export type WalletType = 'anonymous' | 'uphold'

export enum ExternalWalletStatus {
NOT_CONNECTED = 0,
CONNECTED = 1,
VERIFIED = 2,
DISCONNECTED_NOT_VERIFIED = 3,
DISCONNECTED_VERIFIED = 4
}

export interface ExternalWallet {
token: string
address: string
status: ExternalWalletStatus
type: WalletType
verifyUrl: string
addUrl: string
withdrawUrl: string
userName: string
accountUrl: string
}
}

0 comments on commit d5f0890

Please sign in to comment.