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';
|
2019-04-16 18:36:37 +03:00
|
|
|
import SignupCompletePage from '../pages/SignupCompletePage';
|
2019-02-07 12:39:55 +03:00
|
|
|
import RequestPasswordResetPage from '../pages/RequestPasswordResetPage';
|
|
|
|
import PasswordResetSentPage from '../pages/PasswordResetSentPage';
|
|
|
|
import ResetPasswordPage from '../pages/ResetPasswordPage';
|
2019-02-22 09:30:00 +03:00
|
|
|
import StripeSubscribePage from '../pages/StripeSubscribePage';
|
2019-02-26 06:09:16 +03:00
|
|
|
import { IconClose } from '../components/icons';
|
|
|
|
import StripeUpgradePage from '../pages/StripeUpgradePage';
|
2019-02-07 12:39:55 +03:00
|
|
|
|
|
|
|
export default class Modal extends Component {
|
|
|
|
constructor(props, context) {
|
|
|
|
super();
|
|
|
|
this.state = {
|
|
|
|
error: null,
|
2019-04-16 20:09:26 +03:00
|
|
|
showLoggedIn: false,
|
2019-04-16 20:52:15 +03:00
|
|
|
showSpinner: false,
|
2019-02-07 12:39:55 +03:00
|
|
|
containerClass: 'gm-page-overlay'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-14 19:59:41 +03:00
|
|
|
loadConfig() {
|
|
|
|
if (this.state.loadingConfig) {
|
|
|
|
return;
|
|
|
|
}
|
2019-02-26 06:09:16 +03:00
|
|
|
this.context.members.getConfig().then(({paymentConfig, siteConfig}) => {
|
|
|
|
this.setState({ paymentConfig, siteConfig, 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-04-16 20:52:15 +03:00
|
|
|
renderSignupPage({error, stripeConfig, members, signup, closeModal, siteConfig, showSpinner}) {
|
2019-02-15 11:35:03 +03:00
|
|
|
|
2019-02-14 19:59:41 +03:00
|
|
|
if (stripeConfig) {
|
2019-04-16 20:52:15 +03:00
|
|
|
const createAccountWithSubscription = (data) => {
|
|
|
|
this.setState({showSpinner: true});
|
2019-04-16 20:04:55 +03:00
|
|
|
return members.signup(data).then((success) => {
|
|
|
|
return members.createSubscription(data).then((success) => {
|
2019-04-16 20:52:15 +03:00
|
|
|
this.setState({showSpinner: false});
|
|
|
|
window.location.hash = 'signup-complete';
|
|
|
|
}, (error) => {
|
|
|
|
this.setState({ error: "Unable to confirm payment", showSpinner: false });
|
|
|
|
});
|
2019-02-26 06:09:16 +03:00
|
|
|
}, (error) => {
|
2019-04-16 20:52:15 +03:00
|
|
|
this.setState({ error: "Unable to signup", showSpinner: false });
|
|
|
|
})
|
|
|
|
};
|
|
|
|
return <StripeSubscribePage stripeConfig={stripeConfig} error={error} hash="signup" handleSubmit={createAccountWithSubscription} handleClose={closeModal} siteConfig={siteConfig} showSpinner={showSpinner} />
|
2019-02-14 19:59:41 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
return (
|
2019-04-16 20:52:15 +03:00
|
|
|
<SignupPage error={error} hash="signup" handleSubmit={signup} handleClose={closeModal} showSpinner={showSpinner} />
|
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');
|
2019-04-16 20:04:55 +03:00
|
|
|
return <StripeUpgradePage stripeConfig={stripeConfig} error={error} hash="upgrade" handleSubmit={createSubscription} handleClose={closeModal}/>
|
2019-02-22 09:30:00 +03:00
|
|
|
|
2019-02-14 19:59:41 +03:00
|
|
|
}
|
|
|
|
|
2019-02-07 12:39:55 +03:00
|
|
|
render(props, state) {
|
2019-04-16 20:52:15 +03:00
|
|
|
const { containerClass, error, loadingConfig, paymentConfig, siteConfig, showLoggedIn, showSpinner } = 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
|
|
|
|
2019-04-16 20:52:15 +03:00
|
|
|
const signup = (data) => {
|
|
|
|
this.setState({showSpinner: true});
|
2019-04-16 20:04:55 +03:00
|
|
|
return members.signup(data).then((success) => {
|
2019-04-16 20:52:15 +03:00
|
|
|
this.setState({showSpinner: false});
|
|
|
|
window.location.hash = 'signup-complete';
|
|
|
|
}, (error) => {
|
|
|
|
this.setState({ error, showSpinner: false });
|
|
|
|
})
|
|
|
|
};
|
2019-04-16 18:36:37 +03:00
|
|
|
|
2019-04-16 20:09:26 +03:00
|
|
|
// const signin = (data) => this.handleAction(members.signin(data));
|
|
|
|
const signin = (data) => members.signin(data).then((success) => {
|
|
|
|
const clearShowLoggedIn = () => {
|
|
|
|
this.setState({showLoggedIn: false});
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
this.setState({showLoggedIn: true}, () => {
|
|
|
|
window.setTimeout(clearShowLoggedIn, 5000)
|
|
|
|
});
|
|
|
|
}, (error) => {
|
|
|
|
this.setState({ error });
|
|
|
|
});
|
2019-04-16 18:36:37 +03:00
|
|
|
|
2019-02-26 06:09:16 +03:00
|
|
|
const requestReset = (data) => members.requestPasswordReset(data).then((success) => {
|
|
|
|
window.location.hash = 'password-reset-sent';
|
|
|
|
}, (error) => {
|
|
|
|
this.setState({ error });
|
|
|
|
});
|
2019-02-07 12:39:55 +03:00
|
|
|
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-26 06:09:16 +03:00
|
|
|
<Pages className={containerClass} onChange={clearError} onClick={closeModal} stripeConfig={stripeConfig} siteConfig={siteConfig}>
|
2019-04-16 20:09:26 +03:00
|
|
|
<SigninPage error={error} hash="" handleSubmit={signup} showLoggedIn={showLoggedIn} />
|
|
|
|
<SigninPage error={error} hash="signin" handleSubmit={signin} showLoggedIn={showLoggedIn} />
|
2019-04-16 20:52:15 +03:00
|
|
|
{this.renderSignupPage({ error, stripeConfig, members, signup, closeModal, siteConfig, showSpinner})}
|
2019-02-22 09:30:00 +03:00
|
|
|
{this.renderUpgradePage(props, state)}
|
2019-04-16 18:36:37 +03:00
|
|
|
<SignupCompletePage error={ error } hash="signup-complete" handleSubmit={ closeModal } siteConfig={ siteConfig } />
|
2019-02-15 11:35:03 +03:00
|
|
|
<RequestPasswordResetPage error={error} hash="request-password-reset" handleSubmit={requestReset} />
|
2019-02-26 06:09:16 +03:00
|
|
|
<PasswordResetSentPage error={ error } hash="password-reset-sent" handleSubmit={closeModal} />
|
2019-02-15 11:35:03 +03:00
|
|
|
<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);
|
|
|
|
}
|
|
|
|
}
|