2019-02-07 12:39:55 +03:00
|
|
|
import { Component } from 'preact';
|
|
|
|
|
|
|
|
import Pages from './Pages';
|
|
|
|
|
|
|
|
import SigninPage from '../pages/SigninPage';
|
|
|
|
import SignupPage from '../pages/SignupPage';
|
|
|
|
import RequestPasswordResetPage from '../pages/RequestPasswordResetPage';
|
|
|
|
import PasswordResetSentPage from '../pages/PasswordResetSentPage';
|
|
|
|
import ResetPasswordPage from '../pages/ResetPasswordPage';
|
2019-02-14 19:59:41 +03:00
|
|
|
import StripePaymentPage from '../pages/StripePaymentPage';
|
2019-02-15 11:35:03 +03:00
|
|
|
import { IconClose } from '../components/icons';
|
2019-02-22 09:30:00 +03:00
|
|
|
import StripeSubscribePage from '../pages/StripeSubscribePage';
|
2019-02-07 12:39:55 +03:00
|
|
|
|
|
|
|
export default class Modal extends Component {
|
|
|
|
constructor(props, context) {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
error: null,
|
|
|
|
containerClass: 'gm-page-overlay'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 19:59:41 +03:00
|
|
|
loadConfig() {
|
|
|
|
if (this.state.loadingConfig) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.context.members.getConfig().then(paymentConfig => {
|
2019-02-15 11:35:03 +03:00
|
|
|
this.setState({ paymentConfig, loadingConfig: false });
|
2019-02-14 19:59:41 +03:00
|
|
|
}).catch((error) => {
|
2019-02-15 11:35:03 +03:00
|
|
|
this.setState({ error, loadingConfig: false });
|
2019-02-14 19:59:41 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillMount() {
|
|
|
|
this.loadConfig();
|
|
|
|
}
|
|
|
|
|
2019-02-07 12:39:55 +03:00
|
|
|
handleAction(promise) {
|
|
|
|
promise.then((success) => {
|
|
|
|
this.close(success);
|
|
|
|
}, (error) => {
|
2019-02-15 11:35:03 +03:00
|
|
|
this.setState({ error });
|
2019-02-07 12:39:55 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-02-15 11:35:03 +03:00
|
|
|
renderSignupPage({error, stripeConfig, members, signup, closeModal}) {
|
|
|
|
|
2019-02-14 19:59:41 +03:00
|
|
|
if (stripeConfig) {
|
2019-02-15 11:35:03 +03:00
|
|
|
const createAccountWithSubscription = (data) => this.handleAction(
|
|
|
|
members.signup(data).then(() => {
|
|
|
|
members.createSubscription(data);
|
|
|
|
})
|
|
|
|
);
|
|
|
|
return <StripePaymentPage stripeConfig={stripeConfig} error={error} hash="signup" handleSubmit={createAccountWithSubscription} handleClose={closeModal} />
|
2019-02-14 19:59:41 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
return (
|
2019-02-15 11:35:03 +03:00
|
|
|
<SignupPage error={error} hash="signup" handleSubmit={signup} handleClose={closeModal} />
|
2019-02-22 09:30:00 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderUpgradePage(props, state) {
|
|
|
|
const { error, paymentConfig } = state;
|
|
|
|
const { members } = this.context;
|
|
|
|
const closeModal = () => this.close();
|
|
|
|
const createSubscription = (data) => this.handleAction(
|
|
|
|
members.createSubscription(data)
|
|
|
|
);
|
|
|
|
const stripeConfig = paymentConfig && paymentConfig.find(({adapter}) => adapter === 'stripe');
|
|
|
|
return <StripeSubscribePage frameLocation={props.frameLocation} stripeConfig={stripeConfig} error={error} hash="upgrade" handleSubmit={createSubscription} handleClose={closeModal}/>
|
|
|
|
|
2019-02-14 19:59:41 +03:00
|
|
|
}
|
|
|
|
|
2019-02-07 12:39:55 +03:00
|
|
|
render(props, state) {
|
2019-02-14 19:59:41 +03:00
|
|
|
const { containerClass, error, loadingConfig, paymentConfig } = state;
|
2019-02-07 12:39:55 +03:00
|
|
|
const { members } = this.context;
|
|
|
|
|
|
|
|
const closeModal = () => this.close();
|
2019-02-15 11:35:03 +03:00
|
|
|
const clearError = () => this.setState({ error: null });
|
2019-02-07 12:39:55 +03:00
|
|
|
|
|
|
|
const signin = (data) => this.handleAction(members.signin(data));
|
|
|
|
const signup = (data) => this.handleAction(members.signup(data));
|
|
|
|
const requestReset = (data) => this.handleAction(members.requestPasswordReset(data));
|
|
|
|
const resetPassword = (data) => this.handleAction(members.resetPassword(data));
|
2019-02-15 11:35:03 +03:00
|
|
|
const stripeConfig = paymentConfig && paymentConfig.find(({ adapter }) => adapter === 'stripe');
|
2019-02-07 12:39:55 +03:00
|
|
|
|
2019-02-14 19:59:41 +03:00
|
|
|
if (loadingConfig) {
|
|
|
|
return (
|
|
|
|
<Pages className={containerClass} onChange={clearError} onClick={closeModal}>
|
|
|
|
Loading...
|
|
|
|
</Pages>
|
|
|
|
);
|
|
|
|
}
|
2019-02-07 12:39:55 +03:00
|
|
|
return (
|
2019-02-15 11:35:03 +03:00
|
|
|
<Pages className={containerClass} onChange={clearError} onClick={closeModal} stripeConfig={stripeConfig}>
|
|
|
|
<SigninPage error={error} hash="" handleSubmit={signup} />
|
|
|
|
<SigninPage error={error} hash="signin" handleSubmit={signin} />
|
|
|
|
{this.renderSignupPage({error, stripeConfig, members, signup, closeModal})}
|
2019-02-22 09:30:00 +03:00
|
|
|
{this.renderUpgradePage(props, state)}
|
2019-02-15 11:35:03 +03:00
|
|
|
<RequestPasswordResetPage error={error} hash="request-password-reset" handleSubmit={requestReset} />
|
|
|
|
<PasswordResetSentPage error={error} hash="password-reset-sent" handleSubmit={requestReset} />
|
|
|
|
<ResetPasswordPage error={error} hash="reset-password" handleSubmit={resetPassword} />
|
2019-02-07 12:39:55 +03:00
|
|
|
</Pages>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
close(success) {
|
|
|
|
this.setState({
|
|
|
|
containerClass: 'gm-page-overlay close'
|
|
|
|
});
|
|
|
|
|
|
|
|
window.setTimeout(() => {
|
|
|
|
this.setState({
|
|
|
|
containerClass: 'gm-page-overlay'
|
|
|
|
});
|
|
|
|
window.parent.postMessage({
|
|
|
|
msg: 'pls-close-auth-popup',
|
|
|
|
success
|
|
|
|
}, '*');
|
|
|
|
}, 700);
|
|
|
|
}
|
|
|
|
}
|