lbry-desktop/src/renderer/modal/modalRouter/view.jsx

142 lines
4.4 KiB
React
Raw Normal View History

import React from 'react';
import ModalError from 'modal/modalError';
import ModalAuthFailure from 'modal/modalAuthFailure';
import ModalDownloading from 'modal/modalDownloading';
2017-12-08 11:08:50 +01:00
import ModalAutoUpdateDownloaded from "modal/modalAutoUpdateDownloaded";
import ModalAutoUpdateConfirm from "modal/modalAutoUpdateDownloaded";
import ModalUpgrade from 'modal/modalUpgrade';
import ModalWelcome from 'modal/modalWelcome';
import ModalFirstReward from 'modal/modalFirstReward';
import ModalRewardApprovalRequired from 'modal/modalRewardApprovalRequired';
import ModalCreditIntro from 'modal/modalCreditIntro';
import ModalRemoveFile from 'modal/modalRemoveFile';
import ModalTransactionFailed from 'modal/modalTransactionFailed';
import ModalFileTimeout from 'modal/modalFileTimeout';
import ModalAffirmPurchase from 'modal/modalAffirmPurchase';
import ModalRevokeClaim from 'modal/modalRevokeClaim';
import ModalEmailCollection from '../modalEmailCollection';
import * as modals from 'constants/modal_types';
class ModalRouter extends React.PureComponent {
2017-08-26 05:21:26 +02:00
constructor(props) {
super(props);
this.state = {
lastTransitionModal: null,
lastTransitionPage: null,
};
}
componentWillMount() {
2017-08-21 05:06:26 +02:00
this.showTransitionModals(this.props);
}
componentWillReceiveProps(nextProps) {
2017-08-21 05:06:26 +02:00
this.showTransitionModals(nextProps);
}
2017-08-21 05:06:26 +02:00
showTransitionModals(props) {
2017-09-08 05:15:05 +02:00
const { modal, modalProps, openModal, page } = props;
2017-08-21 05:06:26 +02:00
if (modal) {
return;
}
2017-08-26 05:21:26 +02:00
const transitionModal = [
this.checkShowWelcome,
this.checkShowEmail,
2017-08-26 05:21:26 +02:00
this.checkShowCreditIntro,
].reduce((acc, func) => (!acc ? func.bind(this)(props) : acc), false);
2017-08-26 05:21:26 +02:00
if (
transitionModal &&
(transitionModal != this.state.lastTransitionModal || page != this.state.lastTransitionPage)
2017-08-26 05:21:26 +02:00
) {
openModal(transitionModal);
this.setState({
lastTransitionModal: transitionModal,
lastTransitionPage: page,
});
}
2017-08-21 05:06:26 +02:00
}
checkShowWelcome(props) {
2017-08-26 05:21:26 +02:00
const { isWelcomeAcknowledged, user } = props;
if (!isWelcomeAcknowledged && user && !user.is_reward_approved && !user.is_identity_verified) {
2017-08-26 05:21:26 +02:00
return modals.WELCOME;
}
}
checkShowEmail(props) {
const { isEmailCollectionAcknowledged, isVerificationCandidate, user } = props;
2017-08-21 05:06:26 +02:00
if (
!isEmailCollectionAcknowledged &&
isVerificationCandidate &&
2017-08-21 05:06:26 +02:00
user &&
!user.has_verified_email
2017-08-21 05:06:26 +02:00
) {
return modals.EMAIL_COLLECTION;
}
}
checkShowCreditIntro(props) {
const { balance, page, isCreditIntroAcknowledged } = props;
2017-08-26 05:21:26 +02:00
if (
balance <= 0 &&
!isCreditIntroAcknowledged &&
(['send', 'publish'].includes(page) || this.isPaidShowPage(props))
) {
2017-08-26 05:21:26 +02:00
return modals.INSUFFICIENT_CREDITS;
}
}
isPaidShowPage(props) {
const { page, showPageCost } = props;
return page === 'show' && showPageCost > 0;
2017-08-26 05:21:26 +02:00
}
render() {
2017-09-08 05:15:05 +02:00
const { modal, modalProps } = this.props;
2017-10-03 23:55:41 +02:00
switch (modal) {
case modals.UPGRADE:
2017-09-08 05:15:05 +02:00
return <ModalUpgrade {...modalProps} />;
case modals.DOWNLOADING:
2017-09-08 05:15:05 +02:00
return <ModalDownloading {...modalProps} />;
2017-12-08 11:08:50 +01:00
case modals.AUTO_UPDATE_DOWNLOADED:
return <ModalAutoUpdateDownloaded {...modalProps} />;
case modals.AUTO_UPDATE_CONFIRM:
return <ModalAutoUpdateConfirm {...modalProps} />;
case modals.ERROR:
2017-09-08 05:15:05 +02:00
return <ModalError {...modalProps} />;
2017-09-07 23:18:33 +02:00
case modals.FILE_TIMEOUT:
2017-09-08 05:15:05 +02:00
return <ModalFileTimeout {...modalProps} />;
case modals.INSUFFICIENT_CREDITS:
return <ModalCreditIntro {...modalProps} />;
case modals.WELCOME:
2017-09-08 05:15:05 +02:00
return <ModalWelcome {...modalProps} />;
case modals.FIRST_REWARD:
2017-09-08 05:15:05 +02:00
return <ModalFirstReward {...modalProps} />;
case modals.AUTHENTICATION_FAILURE:
2017-09-08 05:15:05 +02:00
return <ModalAuthFailure {...modalProps} />;
2017-08-31 06:15:13 +02:00
case modals.TRANSACTION_FAILED:
2017-09-08 05:15:05 +02:00
return <ModalTransactionFailed {...modalProps} />;
2017-08-26 05:21:26 +02:00
case modals.REWARD_APPROVAL_REQUIRED:
2017-09-08 05:15:05 +02:00
return <ModalRewardApprovalRequired {...modalProps} />;
2017-09-07 23:18:33 +02:00
case modals.CONFIRM_FILE_REMOVE:
2017-09-08 05:15:05 +02:00
return <ModalRemoveFile {...modalProps} />;
2017-09-07 23:18:33 +02:00
case modals.AFFIRM_PURCHASE:
2017-09-08 05:15:05 +02:00
return <ModalAffirmPurchase {...modalProps} />;
2017-10-27 21:42:54 +02:00
case modals.CONFIRM_CLAIM_REVOKE:
return <ModalRevokeClaim {...modalProps} />;
case modals.EMAIL_COLLECTION:
return <ModalEmailCollection {...modalProps} />;
default:
return null;
}
}
}
export default ModalRouter;