lbry-desktop/src/ui/component/userSignIn/view.jsx

148 lines
4.6 KiB
React
Raw Normal View History

2019-08-27 16:43:42 +02:00
// @flow
import React from 'react';
2019-09-26 18:07:11 +02:00
import { withRouter } from 'react-router';
2019-08-27 16:43:42 +02:00
import UserEmailNew from 'component/userEmailNew';
import UserEmailVerify from 'component/userEmailVerify';
2019-09-26 18:07:11 +02:00
import UserFirstChannel from 'component/userFirstChannel';
import { DEFAULT_BID_FOR_FIRST_CHANNEL } from 'component/userFirstChannel/view';
import { rewards as REWARDS } from 'lbryinc';
import usePrevious from 'util/use-previous';
import UserVerify from 'component/userVerify';
import Spinner from 'component/spinner';
import YoutubeTransferWelcome from 'component/youtubeTransferWelcome';
import SyncPassword from 'component/syncPassword';
/*
- Brand new user
- Brand new user, not auto approved
- Second device (first time user), first device has a password + rewards not approved
- Second device (first time user), first device has a password + rewards approved
*/
2019-08-27 16:43:42 +02:00
type Props = {
user: ?User,
emailToVerify: ?string,
2019-09-26 18:07:11 +02:00
channels: ?Array<string>,
balance: ?number,
fetchingChannels: boolean,
claimingReward: boolean,
claimReward: () => void,
fetchUser: () => void,
2019-09-26 18:07:11 +02:00
claimedRewards: Array<Reward>,
history: { replace: string => void },
location: { search: string },
youtubeChannels: Array<any>,
syncIsPending: boolean,
getSyncError: ?string,
hasSyncedSuccessfully: boolean,
2019-08-27 16:43:42 +02:00
};
2019-09-26 18:07:11 +02:00
function useFetched(fetching) {
const wasFetching = usePrevious(fetching);
const [fetched, setFetched] = React.useState(false);
2019-08-27 16:43:42 +02:00
2019-09-26 18:07:11 +02:00
React.useEffect(() => {
if (wasFetching && !fetching) {
setFetched(true);
2019-08-27 16:43:42 +02:00
}
2019-09-26 18:07:11 +02:00
}, [wasFetching, fetching, setFetched]);
return fetched;
}
function UserSignIn(props: Props) {
const {
emailToVerify,
user,
claimingReward,
claimedRewards,
channels,
claimReward,
balance,
history,
location,
fetchUser,
youtubeChannels,
syncEnabled,
syncIsPending,
getSyncError,
syncHash,
fetchingChannels,
} = props;
2019-09-26 18:07:11 +02:00
const { search } = location;
const urlParams = new URLSearchParams(search);
const redirect = urlParams.get('redirect');
const hasVerifiedEmail = user && user.has_verified_email;
const rewardsApproved = user && user.is_reward_approved;
const hasFetchedReward = useFetched(claimingReward);
// const hasFetchedSync = useFetched(syncIsPending);
// const hasTriedSyncForReal = syncEnabled && hasFetchedSync;
2019-09-26 18:07:11 +02:00
const channelCount = channels ? channels.length : 0;
const hasClaimedEmailAward = claimedRewards.some(reward => reward.reward_type === REWARDS.TYPE_CONFIRM_EMAIL);
const hasYoutubeChannels = youtubeChannels && youtubeChannels.length;
const hasTransferrableYoutubeChannels = hasYoutubeChannels && youtubeChannels.some(channel => channel.transferable);
const hasPendingYoutubeTransfer =
hasYoutubeChannels && youtubeChannels.some(channel => channel.transfer_state === 'pending_transfer');
2019-09-26 18:07:11 +02:00
React.useEffect(() => {
if (
hasVerifiedEmail &&
balance !== undefined &&
!hasClaimedEmailAward &&
!hasFetchedReward &&
(!syncEnabled || (syncEnabled && syncHash))
) {
claimReward();
2019-09-26 18:07:11 +02:00
}
}, [hasVerifiedEmail, claimReward, balance, hasClaimedEmailAward, hasFetchedReward, syncEnabled, syncHash]);
2019-09-26 18:07:11 +02:00
React.useEffect(() => {
fetchUser();
}, [fetchUser]);
2019-09-26 18:07:11 +02:00
const SIGN_IN_FLOW = [
!emailToVerify && !hasVerifiedEmail && <UserEmailNew />,
emailToVerify && !hasVerifiedEmail && <UserEmailVerify />,
hasVerifiedEmail && !rewardsApproved && <UserVerify />,
getSyncError && !syncHash && <SyncPassword />,
hasVerifiedEmail && balance > DEFAULT_BID_FOR_FIRST_CHANNEL && channelCount === 0 && !hasYoutubeChannels && (
<UserFirstChannel />
),
hasVerifiedEmail && hasYoutubeChannels && (hasTransferrableYoutubeChannels || hasPendingYoutubeTransfer) && (
<YoutubeTransferWelcome />
),
hasVerifiedEmail &&
balance === 0 &&
!getSyncError &&
(fetchingChannels ||
!hasFetchedReward ||
claimingReward ||
syncIsPending ||
(syncEnabled && !syncHash) ||
// Just claimed the email award, wait until the balance updates to move forward
(balance === 0 && hasFetchedReward && hasClaimedEmailAward)) && (
<div className="main--empty">
<Spinner />
</div>
),
];
2019-09-26 18:07:11 +02:00
let componentToRender;
for (var i = SIGN_IN_FLOW.length - 1; i > -1; i--) {
const Component = SIGN_IN_FLOW[i];
if (Component) {
componentToRender = Component;
break;
}
2019-09-26 18:07:11 +02:00
}
if (!componentToRender) {
2019-09-26 18:07:11 +02:00
history.replace(redirect || '/');
2019-08-27 16:43:42 +02:00
}
return <section className="main--contained">{componentToRender}</section>;
2019-08-27 16:43:42 +02:00
}
2019-09-26 18:07:11 +02:00
export default withRouter(UserSignIn);