From 10ce772bcb52cc47f1f2e0c20c8d28e1e7bf39c9 Mon Sep 17 00:00:00 2001 From: jessop Date: Tue, 6 Oct 2020 20:59:38 -0400 Subject: [PATCH] initial page view sets referring channel --- ui/component/router/index.js | 5 +++++ ui/component/router/view.jsx | 16 +++++++++++++++- ui/redux/actions/user.js | 16 ++++++++++------ ui/redux/selectors/rewards.js | 4 ++++ 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/ui/component/router/index.js b/ui/component/router/index.js index 15d18e16f..4dd917ccb 100644 --- a/ui/component/router/index.js +++ b/ui/component/router/index.js @@ -4,6 +4,9 @@ import { selectHasNavigated, selectScrollStartingPosition, selectWelcomeVersion import Router from './view'; import { normalizeURI, makeSelectTitleForUri } from 'lbry-redux'; import { doSetHasNavigated } from 'redux/actions/app'; +import { doUserSetReferrer } from 'redux/actions/user'; +import { selectHasUnclaimedRefereeReward } from 'redux/selectors/rewards'; + const select = state => { const { pathname, hash } = state.router.location; const urlPath = pathname + hash; @@ -28,11 +31,13 @@ const select = state => { isAuthenticated: selectUserVerifiedEmail(state), welcomeVersion: selectWelcomeVersion(state), hasNavigated: selectHasNavigated(state), + hasUnclaimedRefereeReward: selectHasUnclaimedRefereeReward(state), }; }; const perform = dispatch => ({ setHasNavigated: () => dispatch(doSetHasNavigated()), + setReferrer: referrer => dispatch(doUserSetReferrer(referrer)), }); export default connect(select, perform)(Router); diff --git a/ui/component/router/view.jsx b/ui/component/router/view.jsx index f0027f50c..fe62fdf15 100644 --- a/ui/component/router/view.jsx +++ b/ui/component/router/view.jsx @@ -50,7 +50,7 @@ import NotificationsPage from 'page/notifications'; import SignInWalletPasswordPage from 'page/signInWalletPassword'; import YoutubeSyncPage from 'page/youtubeSync'; import { LINKED_COMMENT_QUERY_PARAM } from 'constants/comment'; -import { parseURI } from 'lbry-redux'; +import { parseURI, isURIValid } from 'lbry-redux'; import { SITE_TITLE, WELCOME_VERSION } from 'config'; const dynamicRoutes = Object.values(SIDEBAR_ROUTES).filter( @@ -105,6 +105,8 @@ type Props = { welcomeVersion: number, hasNavigated: boolean, setHasNavigated: () => void, + setReferrer: string => void, + hasUnclaimedRefereeReward: boolean, }; function AppRouter(props: Props) { @@ -118,6 +120,8 @@ function AppRouter(props: Props) { welcomeVersion, hasNavigated, setHasNavigated, + hasUnclaimedRefereeReward, + setReferrer, } = props; const { entries } = history; const entryIndex = history.index; @@ -135,6 +139,16 @@ function AppRouter(props: Props) { return unlisten; }, [hasNavigated, setHasNavigated]); + useEffect(() => { + if (!hasNavigated && hasUnclaimedRefereeReward) { + const valid = isURIValid(uri); + if (valid) { + const { path } = parseURI(uri); + setReferrer(path); + } + } + }, [hasNavigated, uri, hasUnclaimedRefereeReward, setReferrer]); + useEffect(() => { if (uri) { const { channelName, streamName } = parseURI(uri); diff --git a/ui/redux/actions/user.js b/ui/redux/actions/user.js index 5e47d476d..4d8049db1 100644 --- a/ui/redux/actions/user.js +++ b/ui/redux/actions/user.js @@ -1,4 +1,4 @@ -import { Lbry, doFetchChannelListMine, batchActions, makeSelectClaimForUri, parseURI } from 'lbry-redux'; +import { Lbry, doFetchChannelListMine, batchActions, makeSelectClaimForUri, isURIValid } from 'lbry-redux'; import * as ACTIONS from 'constants/action_types'; import { doClaimRewardType, doRewardList } from 'redux/actions/rewards'; import { selectEmailToVerify, selectPhoneToVerify, selectUserCountryCode, selectUser } from 'redux/selectors/user'; @@ -640,10 +640,8 @@ export function doUserSetReferrer(referrer, shouldClaim) { }); let claim; let referrerCode; - - const { isChannel } = parseURI(referrer); - - if (isChannel) { + const isValid = isURIValid(referrer); + if (isValid) { const uri = `lbry://${referrer}`; claim = makeSelectClaimForUri(uri)(getState()); if (!claim) { @@ -657,7 +655,13 @@ export function doUserSetReferrer(referrer, shouldClaim) { }); } } - referrerCode = claim && claim.permanent_url && claim.permanent_url.replace('lbry://', ''); + if (claim) { + if (claim.signing_channel) { + referrerCode = claim.signing_channel.permanent_url.replace('lbry://', ''); + } else { + referrerCode = claim.permanent_url.replace('lbry://', ''); + } + } } if (!referrerCode) { diff --git a/ui/redux/selectors/rewards.js b/ui/redux/selectors/rewards.js index 9d3893ca1..c70b3cdc8 100644 --- a/ui/redux/selectors/rewards.js +++ b/ui/redux/selectors/rewards.js @@ -57,3 +57,7 @@ export const selectReferralReward = createSelector( selectUnclaimedRewards, unclaimedRewards => unclaimedRewards.filter(reward => reward.reward_type === REWARDS.TYPE_REFERRAL)[0] ); + +export const selectHasUnclaimedRefereeReward = createSelector(selectUnclaimedRewards, unclaimedRewards => + unclaimedRewards.some(reward => reward.reward_type === REWARDS.TYPE_REFEREE) +);