Skip to content

Commit

Permalink
Merge pull request #1868 from brave/recurring-banner-62
Browse files Browse the repository at this point in the history
Fixes date for recurring banner
  • Loading branch information
NejcZdovc committed Mar 7, 2019
1 parent d2edde0 commit 039d62f
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 53 deletions.
125 changes: 87 additions & 38 deletions browser/ui/webui/brave_donate_ui.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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/. */

#include "brave/browser/ui/webui/brave_donate_ui.h"

#include <memory>
#include <string>
#include <utility>

#include "base/memory/weak_ptr.h"
#include "brave/browser/brave_browser_process_impl.h"
#include "brave/browser/ui/webui/basic_ui.h"
#include "brave/common/pref_names.h"
Expand Down Expand Up @@ -32,35 +38,43 @@ namespace {
// The handler for Javascript messages for Brave about: pages
class RewardsDonateDOMHandler : public WebUIMessageHandler,
public brave_rewards::RewardsServiceObserver {
public:
RewardsDonateDOMHandler() {};
public:
RewardsDonateDOMHandler();
~RewardsDonateDOMHandler() override;

void Init();

// WebUIMessageHandler implementation.
void RegisterMessages() override;

private:
private:
void GetPublisherDonateData(const base::ListValue* args);
void GetWalletProperties(const base::ListValue* args);
void OnDonate(const base::ListValue* args);
void GetRecurringDonations(const base::ListValue* args);
void GetReconcileStamp(const base::ListValue* args);
void OnReconcileStamp(uint64_t reconcile_stamp);

// RewardsServiceObserver implementation
void OnWalletProperties(brave_rewards::RewardsService* rewards_service,
void OnWalletProperties(
brave_rewards::RewardsService* rewards_service,
int error_code,
std::unique_ptr<brave_rewards::WalletProperties> wallet_properties) override;
void OnRecurringDonationUpdated(brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList) override;
std::unique_ptr<brave_rewards::WalletProperties> wallet_properties)
override;
void OnRecurringDonationUpdated(
brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList) override;
void OnPublisherBanner(brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) override;

brave_rewards::RewardsService* rewards_service_; // NOT OWNED
base::WeakPtrFactory<RewardsDonateDOMHandler> weak_factory_;

DISALLOW_COPY_AND_ASSIGN(RewardsDonateDOMHandler);
};

RewardsDonateDOMHandler::RewardsDonateDOMHandler() : weak_factory_(this) {}

RewardsDonateDOMHandler::~RewardsDonateDOMHandler() {
if (rewards_service_)
rewards_service_->RemoveObserver(this);
Expand All @@ -75,21 +89,30 @@ void RewardsDonateDOMHandler::Init() {
}

void RewardsDonateDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards_donate.getPublisherBanner",
base::BindRepeating(&RewardsDonateDOMHandler::GetPublisherDonateData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards_donate.getWalletProperties",
base::BindRepeating(&RewardsDonateDOMHandler::GetWalletProperties,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards_donate.onDonate",
base::BindRepeating(&RewardsDonateDOMHandler::OnDonate,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards_donate.getRecurringDonations",
base::BindRepeating(&RewardsDonateDOMHandler::GetRecurringDonations,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards_donate.getPublisherBanner",
base::BindRepeating(&RewardsDonateDOMHandler::GetPublisherDonateData,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards_donate.getWalletProperties",
base::BindRepeating(&RewardsDonateDOMHandler::GetWalletProperties,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards_donate.onDonate",
base::BindRepeating(&RewardsDonateDOMHandler::OnDonate,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards_donate.getRecurringDonations",
base::BindRepeating(&RewardsDonateDOMHandler::GetRecurringDonations,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards_donate.getReconcileStamp",
base::BindRepeating(&RewardsDonateDOMHandler::GetReconcileStamp,
base::Unretained(this)));
}

void RewardsDonateDOMHandler::GetPublisherDonateData(const base::ListValue* args) {
void RewardsDonateDOMHandler::GetPublisherDonateData(
const base::ListValue* args) {
std::string publisher_key;
args->GetString(0, &publisher_key);
rewards_service_->GetPublisherBanner(publisher_key);
Expand Down Expand Up @@ -149,7 +172,8 @@ void RewardsDonateDOMHandler::OnWalletProperties(

result.SetDictionary("wallet", std::move(walletInfo));

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards_donate.walletProperties", result);
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_donate.walletProperties", result);
}

void RewardsDonateDOMHandler::OnDonate(const base::ListValue* args) {
Expand All @@ -164,36 +188,42 @@ void RewardsDonateDOMHandler::OnDonate(const base::ListValue* args) {
args->GetBoolean(2, &recurring);

if (publisher_key.empty() || amount < 1) {
// TODO add error
// TODO(nejczdovc) add error
return;
}

rewards_service_->OnDonate(publisher_key, amount, recurring);
}

void RewardsDonateDOMHandler::GetRecurringDonations(const base::ListValue *args) {
void RewardsDonateDOMHandler::GetRecurringDonations(
const base::ListValue *args) {
if (rewards_service_) {
rewards_service_->UpdateRecurringDonationsList();
}
}

void RewardsDonateDOMHandler::OnRecurringDonationUpdated(brave_rewards::RewardsService* rewards_service,
const brave_rewards::ContentSiteList list) {
if (web_ui()->CanCallJavascript()) {
auto publishers = std::make_unique<base::ListValue>();
for (auto const& item : list) {
auto publisher = std::make_unique<base::DictionaryValue>();
publisher->SetString("publisherKey", item.id);
publisher->SetInteger("monthlyDate", item.reconcile_stamp);
publishers->Append(std::move(publisher));
}
void RewardsDonateDOMHandler::OnRecurringDonationUpdated(
brave_rewards::RewardsService* rewards_service,
const brave_rewards::ContentSiteList list) {
if (!web_ui()->CanCallJavascript()) {
return;
}

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards_donate.recurringDonations", *publishers);
auto publishers = std::make_unique<base::ListValue>();
for (auto const& item : list) {
auto publisher = std::make_unique<base::DictionaryValue>();
publisher->SetString("publisherKey", item.id);
publisher->SetInteger("monthlyDate", item.reconcile_stamp);
publishers->Append(std::move(publisher));
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_donate.recurringDonations", *publishers);
}

void RewardsDonateDOMHandler::OnPublisherBanner(brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {
void RewardsDonateDOMHandler::OnPublisherBanner(
brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {
if (!web_ui()->CanCallJavascript()) {
return;
}
Expand All @@ -220,7 +250,8 @@ void RewardsDonateDOMHandler::OnPublisherBanner(brave_rewards::RewardsService* r
}
result.SetDictionary("social", std::move(social));

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards_donate.publisherBanner", result);
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_donate.publisherBanner", result);
}

} // namespace
Expand All @@ -245,5 +276,23 @@ BraveDonateUI::BraveDonateUI(content::WebUI* web_ui, const std::string& name)
handler->Init();
}

void RewardsDonateDOMHandler::GetReconcileStamp(const base::ListValue *args) {
if (rewards_service_) {
rewards_service_->GetReconcileStamp(base::Bind(
&RewardsDonateDOMHandler::OnReconcileStamp,
weak_factory_.GetWeakPtr()));
}
}

void RewardsDonateDOMHandler::OnReconcileStamp(uint64_t reconcile_stamp) {
if (!web_ui()->CanCallJavascript()) {
return;
}

const std::string stamp = std::to_string(reconcile_stamp);
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards_donate.reconcileStamp",
base::Value(stamp));
}

BraveDonateUI::~BraveDonateUI() {
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ export const getRecurringDonations = () => action(types.GET_RECURRING_DONATIONS)
export const onRecurringDonations = (recurringDonations: RewardsDonate.RecurringDonation[]) => action(types.ON_RECURRING_DONATIONS, {
recurringDonations
})

export const getReconcileStamp = () => action(types.GET_RECONCILE_STAMP)

export const onReconcileStamp = (stamp: number) => action(types.ON_RECONCILE_STAMP, {
stamp
})
7 changes: 6 additions & 1 deletion components/brave_rewards/resources/donate/brave_donate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,16 @@ window.cr.define('brave_rewards_donate', function () {
getActions().onRecurringDonations(list)
}

function reconcileStamp (stamp: number) {
getActions().onReconcileStamp(stamp)
}

return {
initialize,
publisherBanner,
walletProperties,
recurringDonations
recurringDonations,
reconcileStamp
}
})

Expand Down
16 changes: 4 additions & 12 deletions components/brave_rewards/resources/donate/components/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,28 +30,20 @@ export class App extends React.Component<Props, {}> {
generateDonationOverlay = (publisher: RewardsDonate.Publisher) => {
let domain = ''
let monthlyDate
let recurringDonation
const {
currentTipAmount,
currentTipRecurring,
recurringDonations
reconcileStamp
} = this.props.rewardsDonateData

const publisherKey = publisher && publisher.publisherKey

if (!publisherKey ||
(currentTipRecurring && !recurringDonations)) {
if (!publisherKey) {
return null
}

if (recurringDonations) {
recurringDonation = recurringDonations.find((donation: RewardsDonate.RecurringDonation) => {
return donation.publisherKey === publisherKey
})
}

if (recurringDonation && recurringDonation.monthlyDate) {
monthlyDate = new Date(recurringDonation.monthlyDate * 1000).toLocaleDateString()
if (currentTipRecurring && reconcileStamp) {
monthlyDate = new Date(reconcileStamp * 1000).toLocaleDateString()
}

if (publisher.provider && publisher.name) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class Banner extends React.Component<Props, State> {
componentDidMount () {
this.actions.getWalletProperties()
this.actions.getRecurringDonations()
this.actions.getReconcileStamp()
}

get actions () {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ export const enum types {
GET_WALLET_PROPERTIES = '@@rewards/GET_WALLET_PROPERTIES',
ON_DONATE = '@@rewards/ON_DONATE',
GET_RECURRING_DONATIONS = '@@rewards/GET_RECURRING_DONATIONS',
ON_RECURRING_DONATIONS = '@@rewards/ON_RECURRING_DONATIONS'
ON_RECURRING_DONATIONS = '@@rewards/ON_RECURRING_DONATIONS',
GET_RECONCILE_STAMP = '@@rewards/GET_RECONCILE_STAMP',
ON_RECONCILE_STAMP = '@@rewards/ON_RECONCILE_STAMP'
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,17 @@ export const defaultState: RewardsDonate.State = {
balance: 0,
choices: [],
probi: '0'
}
},
reconcileStamp: 0
}

const publishersReducer: Reducer<RewardsDonate.State> = (state: RewardsDonate.State = defaultState, action) => {
const payload = action.payload

switch (action.type) {
case types.ON_CLOSE_DIALOG:
state = { ...state }
state.currentTipRecurring = false
chrome.send('dialogClose')
break
case types.ON_PUBLISHER_BANNER:
Expand Down Expand Up @@ -78,6 +81,15 @@ const publishersReducer: Reducer<RewardsDonate.State> = (state: RewardsDonate.St
state.recurringDonations = recurringDonations
}
break
case types.GET_RECONCILE_STAMP: {
chrome.send('brave_rewards_donate.getReconcileStamp')
break
}
case types.ON_RECONCILE_STAMP: {
state = { ...state }
state.reconcileStamp = action.payload.stamp
break
}
}

return state
Expand Down
1 change: 1 addition & 0 deletions components/definitions/rewardsDonate.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ declare namespace RewardsDonate {
currentTipAmount?: string
currentTipRecurring?: boolean
recurringDonations?: RecurringDonation[]
reconcileStamp: number
}

interface ApplicationState {
Expand Down

0 comments on commit 039d62f

Please sign in to comment.