Skip to content

Commit

Permalink
Adds initialized to panel flow
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed Aug 4, 2020
1 parent 0f9b0cb commit dae1ca4
Show file tree
Hide file tree
Showing 23 changed files with 331 additions and 107 deletions.
29 changes: 27 additions & 2 deletions browser/brave_rewards/extension_rewards_service_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,38 @@ void ExtensionRewardsServiceObserver::OnWalletInitialized(
return;
}

// ledger::Result::WALLET_CREATED
if (result == 12) {
auto args = std::make_unique<base::ListValue>();
std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::WalletCreated::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
return;
}

if (result != 3 && result != 0) {
// Report back all errors except when ledger_state is missing
std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::WalletCreationFailed::Create(
result).release());

std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::WalletCreationFailed::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
return;
}

std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnWalletInitialized::Create(
extensions::api::brave_rewards::Initialized::Create(
result).release());

std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnWalletInitialized::kEventName,
extensions::api::brave_rewards::Initialized::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}
Expand Down
18 changes: 18 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1174,5 +1174,23 @@ void BraveRewardsGetAnonWalletStatusFunction::OnGetAnonWalletStatus(
Respond(OneArgument(std::make_unique<base::Value>(static_cast<int>(result))));
}

BraveRewardsIsInitializedFunction::
~BraveRewardsIsInitializedFunction() = default;

ExtensionFunction::ResponseAction
BraveRewardsIsInitializedFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
RewardsServiceFactory::GetForProfile(profile);

if (!rewards_service) {
return RespondNow(Error("Rewards service is not initialized"));
}

const bool initialized = rewards_service->IsInitialized();
return RespondNow(
OneArgument(std::make_unique<base::Value>(initialized)));
}

} // 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 @@ -466,6 +466,16 @@ class BraveRewardsGetAnonWalletStatusFunction
void OnGetAnonWalletStatus(const uint32_t result);
};

class BraveRewardsIsInitializedFunction : public ExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.isInitialized", UNKNOWN)

protected:
~BraveRewardsIsInitializedFunction() override;

ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

Expand Down
38 changes: 35 additions & 3 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,26 @@
},
"events": [
{
"name": "onWalletInitialized",
"name": "walletCreated",
"type": "function",
"description": "Fired when wallet is created",
"parameters": []
},
{
"name": "walletCreationFailed",
"type": "function",
"description": "Fired when wallet creation failed",
"parameters": [{
"name": "result",
"type": "integer"
}
]
},
{
"name": "initialized",
"type": "function",
"description": "Fired when wallet is initialized",
"parameters": [
{
"parameters": [{
"name": "result",
"type": "integer"
}
Expand Down Expand Up @@ -1103,6 +1118,23 @@
]
}
]
},
{
"name": "isInitialized",
"type": "function",
"description": "Is ledger process initialized",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "initialized",
"type": "boolean"
}
]
}
]
}
]
}
Expand Down
4 changes: 4 additions & 0 deletions components/brave_new_tab_ui/actions/rewards_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,7 @@ export const onPromotionFinish = (result: NewTab.RewardsResult, promotion: NewTa
export const setOnlyAnonWallet = (onlyAnonWallet: boolean) => action(types.SET_ONLY_ANON_WALLET, {
onlyAnonWallet
})

export const onCompleteReset = (success: boolean) => action(types.ON_COMPLETE_RESET, {
success
})
62 changes: 47 additions & 15 deletions components/brave_new_tab_ui/apiEventsToStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export function wireApiEventsToStore () {
.then((initialData) => {
if (initialData.preferences.showRewards) {
rewardsInitData()
setRewardsFetchInterval()
}
binanceInitData()
getActions().setInitialData(initialData)
Expand All @@ -55,10 +54,16 @@ export function rewardsInitData () {
getRewardsPreInitialData()
.then((preInitialRewardsData) => {
getActions().setPreInitialRewardsData(preInitialRewardsData)

chrome.braveRewards.getWalletExists((exists: boolean) => {
getActions().onWalletExists(exists)
if (exists) {
if (!preInitialRewardsData.enabledMain) {
return
}

fetchCreatedWalletData()
getActions().onWalletExists(exists)
setRewardsFetchInterval()
}
})
})
Expand All @@ -83,32 +88,55 @@ function binanceInitData () {

function setRewardsFetchInterval () {
window.setInterval(() => {
chrome.braveRewards.getWalletExists((exists: boolean) => {
if (exists) {
fetchCreatedWalletData()
chrome.braveRewards.getRewardsMainEnabled((enabledMain: boolean) => {
if (!enabledMain) {
return
}
chrome.braveRewards.getWalletExists((exists: boolean) => {
if (exists) {
fetchCreatedWalletData()
}
})
})
}, 30000)
}

function fetchCreatedWalletData () {
getRewardsInitialData()
.then((initialRewardsData) => {
getActions().setInitialRewardsData(initialRewardsData)
})
.catch(e => {
console.error('Error fetching initial rewards data: ', e)
chrome.braveRewards.isInitialized((initialized: boolean) => {
if (!initialized) {
return
}

getRewardsInitialData()
.then((initialRewardsData) => {
getActions().setInitialRewardsData(initialRewardsData)
})
.catch(e => {
console.error('Error fetching initial rewards data: ', e)
})
})
}

chrome.braveRewards.onWalletInitialized.addListener((result: any | NewTab.RewardsResult) => {
chrome.braveRewards.walletCreated.addListener(() => {
getActions().onWalletInitialized(12)
})

chrome.braveRewards.walletCreationFailed.addListener((result: any | NewTab.RewardsResult) => {
getActions().onWalletInitialized(result)
})

chrome.braveRewards.initialized.addListener((result: any | NewTab.RewardsResult) => {
rewardsInitData()
})

chrome.braveRewards.onEnabledMain.addListener((enabledMain: boolean) => {
chrome.braveRewards.getAdsEnabled((enabledAds: boolean) => {
getActions().onEnabledMain(enabledMain, enabledAds)
})
if (enabledMain) {
chrome.braveRewards.getAdsEnabled((enabledAds: boolean) => {
getActions().onEnabledMain(enabledMain, enabledAds)
})
} else {
getActions().onEnabledMain(false, false)
}
})

chrome.braveRewards.onAdsEnabled.addListener((enabled: boolean) => {
Expand All @@ -122,3 +150,7 @@ chrome.braveRewards.onPromotions.addListener((result: number, promotions: NewTab
chrome.braveRewards.onPromotionFinish.addListener((result: number, promotion: NewTab.Promotion) => {
getActions().onPromotionFinish(result, promotion)
})

chrome.braveRewards.onCompleteReset.addListener((properties: { success: boolean }) => {
getActions().onCompleteReset(properties.success)
})
3 changes: 2 additions & 1 deletion components/brave_new_tab_ui/constants/rewards_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,6 @@ export const enum types {
SET_INITIAL_REWARDS_DATA = '@@rewards/SET_INITIAL_REWARDS_DATA',
SET_PRE_INITIAL_REWARDS_DATA = '@@rewards/SET_PRE_INITIAL_REWARDS_DATA',
ON_WIDGET_POSITION_CHANGED = '@@rewards/ON_WIDGET_POSITION_CHANGED',
SET_ONLY_ANON_WALLET = '@@rewards/SET_ONLY_ANON_WALLET'
SET_ONLY_ANON_WALLET = '@@rewards/SET_ONLY_ANON_WALLET',
ON_COMPLETE_RESET = '@@rewards/ON_COMPLETE_RESET'
}
27 changes: 17 additions & 10 deletions components/brave_new_tab_ui/reducers/rewards_reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,18 +34,13 @@ const rewardsReducer: Reducer<NewTab.State | undefined> = (state: NewTab.State,
state.rewardsState.walletCorrupted = true
break
case NewTab.RewardsResult.WALLET_CREATED:
state.rewardsState.enabledMain = true
state.rewardsState.walletCreated = true
state.rewardsState.walletCreateFailed = false
state.rewardsState.walletCreating = false
state.rewardsState.walletCorrupted = false
chrome.braveRewards.saveAdsSetting('adsEnabled', 'true')
break
case NewTab.RewardsResult.LEDGER_OK:
state.rewardsState.walletCreateFailed = true
state.rewardsState.walletCreating = false
state.rewardsState.walletCreated = false
state.rewardsState.walletCorrupted = false
break
}
break
}
Expand Down Expand Up @@ -148,10 +143,7 @@ const rewardsReducer: Reducer<NewTab.State | undefined> = (state: NewTab.State,
break

case types.ON_WALLET_EXISTS:
if (!payload.exists || state.rewardsState.walletCreated) {
break
}
state.rewardsState.walletCreated = true
state.rewardsState.walletCreated = payload.exists
break

case types.SET_PRE_INITIAL_REWARDS_DATA:
Expand Down Expand Up @@ -195,6 +187,21 @@ const rewardsReducer: Reducer<NewTab.State | undefined> = (state: NewTab.State,
}
}
break
case types.ON_COMPLETE_RESET:
state = { ...state }
state = {
...state,
rewardsState: {
...state.rewardsState,
enabledMain: false,
enabledAds: false,
walletCreated: false,
walletCreating: false,
walletCreateFailed: false,
walletCorrupted: false
}
}
break

default:
break
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,6 @@ class SettingsPage extends React.Component<Props, State> {

onToggle = () => {
this.setState({ mainToggle: !this.state.mainToggle })
if (this.props.rewardsData.initializing) {
return
}

this.actions.toggleEnableMain(!this.props.rewardsData.enabledMain)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,14 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
break
}
case types.TOGGLE_ENABLE_MAIN: {
if (state.initializing) {
if (state.initializing && state.enabledMain) {
break
}

state = { ...state }
const key = 'enabledMain'
const enable = action.payload.enable

if (enable) {
state.initializing = true
}
state.initializing = true

state[key] = enable
chrome.send('brave_rewards.saveSetting', [key, enable.toString()])
Expand Down Expand Up @@ -169,6 +166,11 @@ const rewardsReducer: Reducer<Rewards.State | undefined> = (state: Rewards.State
break
}

if (!enabled) {
state.balance = defaultState.balance
state.promotions = []
}

state.enabledMain = enabled
break
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ import { types } from '../constants/rewards_panel_types'

export const createWallet = () => action(types.CREATE_WALLET, {})

export const onWalletInitialized = (result: RewardsExtension.Result) => action(types.ON_WALLET_INITIALIZED, {
export const walletCreated = () => action(types.WALLET_CREATED)

export const walletCreationFailed = (result: RewardsExtension.Result) => action(types.WALLET_CREATION_FAILED, {
result
})

Expand Down Expand Up @@ -153,3 +155,13 @@ export const onAllNotificationsDeleted = () => action(types.ON_ALL_NOTIFICATIONS
export const onCompleteReset = (success: boolean) => action(types.ON_COMPLETE_RESET, {
success
})

export const toggleEnableMain = (enable: boolean) => action(types.TOGGLE_ENABLE_MAIN, {
enable
})

export const initialized = () => action(types.INITIALIZED)

export const walletExists = (exists: boolean) => action(types.WALLET_EXISTS, {
exists
})
Loading

0 comments on commit dae1ca4

Please sign in to comment.