lbry-desktop/ui/component/invited/view.jsx

133 lines
3.6 KiB
React
Raw Normal View History

2020-01-06 23:53:27 +01:00
// @flow
import * as PAGES from 'constants/pages';
import React, { useEffect } from 'react';
import { useParams } from 'react-router';
import Button from 'component/button';
import ClaimPreview from 'component/claimPreview';
import Card from 'component/common/card';
import { parseURI } from 'lbry-redux';
type Props = {
user: any,
fetchUser: () => void,
claimReward: () => void,
setReferrer: string => void,
setReferrerPending: boolean,
setReferrerError: string,
channelSubscribe: (sub: Subscription) => void,
history: { push: string => void },
2020-01-06 23:53:27 +01:00
};
function Invited(props: Props) {
const {
user,
fetchUser,
claimReward,
setReferrer,
setReferrerPending,
setReferrerError,
channelSubscribe,
history,
} = props;
2020-01-06 23:53:27 +01:00
// useParams requires react-router-dom ^v5.1.0
const { referrer } = useParams();
const refUri = 'lbry://' + referrer.replace(':', '#');
const referrerIsChannel = parseURI(refUri).isChannel;
const rewardsApproved = user && user.is_reward_approved;
const hasVerifiedEmail = user && user.has_verified_email;
useEffect(() => {
fetchUser();
}, []);
useEffect(() => {
if (!setReferrerPending && hasVerifiedEmail) {
2020-01-06 23:53:27 +01:00
claimReward();
}
}, [setReferrerPending, hasVerifiedEmail]);
useEffect(() => {
if (referrer) {
setReferrer(referrer.replace(':', '#'));
}
}, [referrer]);
function handleDone() {
if (hasVerifiedEmail && referrerIsChannel) {
channelSubscribe({
channelName: parseURI(refUri).claimName,
uri: refUri,
});
}
history.push(`/$/${PAGES.DISCOVER}`);
}
2020-01-06 23:53:27 +01:00
if (setReferrerError) {
return (
<Card
title={__(`Welcome!`)}
subtitle={__(
`Something went wrong with this referral link. Take a look around. You can get earn rewards by setting your referrer later.`
)}
2020-01-06 23:53:27 +01:00
actions={
<>
<p className="error-text">{__('Not a valid referral')}</p>
<p className="error-text">{setReferrerError}</p>
<div className="card__actions">
<Button button="primary" label={__('Explore')} onClick={handleDone} />
2020-01-06 23:53:27 +01:00
</div>
</>
}
/>
);
}
if (!rewardsApproved) {
return (
<Card
title={__(`You're invited!`)}
subtitle={__(`A referral reward is waiting for you. Just complete sign-in to claim it.`)}
actions={
<>
{referrerIsChannel && (
<div key={refUri} className="claim-preview--channel">
<ClaimPreview key={refUri} uri={refUri} actions={''} type={'small'} />
</div>
)}
<div className="card__actions">
<Button
button="primary"
label={hasVerifiedEmail ? __('Verify') : __('Sign in')}
navigate={`/$/${PAGES.AUTH}?redirect=/$/${PAGES.INVITE}/${referrer}`}
/>
<Button button="primary" label={__('Skip')} onClick={handleDone} />
2020-01-06 23:53:27 +01:00
</div>
</>
}
/>
);
}
return (
<Card
title={__(`Welcome!`)}
subtitle={__(`You can visit your referrer, or discover new stuff.`)}
actions={
<>
{referrerIsChannel && (
<div key={refUri} className="claim-preview--channel">
<ClaimPreview key={refUri} uri={refUri} actions={''} type={'small'} />
</div>
)}
<div className="card__actions">
<Button button="primary" label={__('Done!')} onClick={handleDone} />
2020-01-06 23:53:27 +01:00
</div>
</>
}
/>
);
}
export default Invited;