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

165 lines
5.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';
import ModalAutoUpdateDownloaded from 'modal/modalAutoUpdateDownloaded';
import ModalAutoUpdateConfirm from 'modal/modalAutoUpdateConfirm';
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 'modal/modalEmailCollection';
import ModalPhoneCollection from 'modal/modalPhoneCollection';
import ModalFirstSubscription from 'modal/modalFirstSubscription';
2018-03-26 23:32:43 +02:00
import ModalSendTip from '../modalSendTip';
import ModalPublish from '../modalPublish';
import ModalSearch from '../modalSearch';
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
) {
2018-04-19 18:51:18 +02:00
openModal({ id: transitionModal });
2017-08-26 05:21:26 +02:00
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() {
2018-04-19 18:51:18 +02:00
const { notification, notificationProps } = this.props;
2018-04-19 18:51:18 +02:00
if (!notification) {
return null;
}
2018-04-23 07:04:11 +02:00
if (notification.error) {
return <ModalError {...notification} {...notificationProps} />;
}
2018-04-19 18:51:18 +02:00
switch (notification.id) {
case modals.UPGRADE:
2018-04-19 18:51:18 +02:00
return <ModalUpgrade {...notificationProps} />;
case modals.DOWNLOADING:
2018-04-19 18:51:18 +02:00
return <ModalDownloading {...notificationProps} />;
2017-12-08 11:08:50 +01:00
case modals.AUTO_UPDATE_DOWNLOADED:
2018-04-19 18:51:18 +02:00
return <ModalAutoUpdateDownloaded {...notificationProps} />;
case modals.AUTO_UPDATE_CONFIRM:
2018-04-19 18:51:18 +02:00
return <ModalAutoUpdateConfirm {...notificationProps} />;
case modals.ERROR:
2018-04-19 18:51:18 +02:00
return <ModalError {...notificationProps} />;
2017-09-07 23:18:33 +02:00
case modals.FILE_TIMEOUT:
2018-04-19 18:51:18 +02:00
return <ModalFileTimeout {...notificationProps} />;
case modals.INSUFFICIENT_CREDITS:
2018-04-19 18:51:18 +02:00
return <ModalCreditIntro {...notificationProps} />;
case modals.WELCOME:
2018-04-19 18:51:18 +02:00
return <ModalWelcome {...notificationProps} />;
case modals.FIRST_REWARD:
2018-04-19 18:51:18 +02:00
return <ModalFirstReward {...notificationProps} />;
case modals.AUTHENTICATION_FAILURE:
2018-04-19 18:51:18 +02:00
return <ModalAuthFailure {...notificationProps} />;
2017-08-31 06:15:13 +02:00
case modals.TRANSACTION_FAILED:
2018-04-19 18:51:18 +02:00
return <ModalTransactionFailed {...notificationProps} />;
2017-08-26 05:21:26 +02:00
case modals.REWARD_APPROVAL_REQUIRED:
2018-04-19 18:51:18 +02:00
return <ModalRewardApprovalRequired {...notificationProps} />;
2017-09-07 23:18:33 +02:00
case modals.CONFIRM_FILE_REMOVE:
2018-04-19 18:51:18 +02:00
return <ModalRemoveFile {...notificationProps} />;
2017-09-07 23:18:33 +02:00
case modals.AFFIRM_PURCHASE:
2018-04-19 18:51:18 +02:00
return <ModalAffirmPurchase {...notificationProps} />;
2017-10-27 21:42:54 +02:00
case modals.CONFIRM_CLAIM_REVOKE:
2018-04-19 18:51:18 +02:00
return <ModalRevokeClaim {...notificationProps} />;
2018-01-15 14:32:01 +01:00
case modals.PHONE_COLLECTION:
2018-04-19 18:51:18 +02:00
return <ModalPhoneCollection {...notificationProps} />;
case modals.EMAIL_COLLECTION:
2018-04-19 18:51:18 +02:00
return <ModalEmailCollection {...notificationProps} />;
case modals.FIRST_SUBSCRIPTION:
2018-04-19 18:51:18 +02:00
return <ModalFirstSubscription {...notificationProps} />;
2018-03-26 23:32:43 +02:00
case modals.SEND_TIP:
2018-04-19 18:51:18 +02:00
return <ModalSendTip {...notificationProps} />;
2018-03-26 23:32:43 +02:00
case modals.PUBLISH:
2018-04-19 18:51:18 +02:00
return <ModalPublish {...notificationProps} />;
2018-03-26 23:32:43 +02:00
case modals.SEARCH:
2018-04-19 18:51:18 +02:00
return <ModalSearch {...notificationProps} />;
default:
return null;
}
}
}
export default ModalRouter;