import { Component } from 'preact'; import Pages from './Pages'; import SigninPage from '../pages/SigninPage'; import SignupPage from '../pages/SignupPage'; import SignupCompletePage from '../pages/SignupCompletePage'; import RequestPasswordResetPage from '../pages/RequestPasswordResetPage'; import PasswordResetSentPage from '../pages/PasswordResetSentPage'; import ResetPasswordPage from '../pages/ResetPasswordPage'; import StripeSubscribePage from '../pages/StripeSubscribePage'; import { IconClose } from '../components/icons'; import StripeUpgradePage from '../pages/StripeUpgradePage'; export default class Modal extends Component { constructor(props, context) { super(); this.state = { error: null, showLoggedIn: false, showSpinner: false, containerClass: 'gm-page-overlay' } } loadConfig() { if (this.state.loadingConfig) { return; } this.context.members.getConfig().then(({paymentConfig, siteConfig}) => { this.setState({ paymentConfig, siteConfig, loadingConfig: false }); }).catch((error) => { this.setState({ error, loadingConfig: false }); }); } componentWillMount() { this.loadConfig(); } handleAction(promise) { promise.then((success) => { this.close(success); }, (error) => { this.setState({ error }); }); } renderSignupPage({error, stripeConfig, members, signup, closeModal, siteConfig, showSpinner}) { if (stripeConfig) { const createAccountWithSubscription = (data) => { this.setState({showSpinner: true}); return members.signup(data).then((success) => { return members.createSubscription(data).then((success) => { this.setState({showSpinner: false}); window.location.hash = 'signup-complete'; }, (error) => { this.setState({ error: "Unable to confirm payment", showSpinner: false }); }); }, (error) => { this.setState({ error: "Unable to signup", showSpinner: false }); }) }; return } return ( ); } 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 } render(props, state) { const { containerClass, error, loadingConfig, paymentConfig, siteConfig, showLoggedIn, showSpinner } = state; const { members } = this.context; const closeModal = () => this.close(); const clearError = () => this.setState({ error: null }); const signup = (data) => { this.setState({showSpinner: true}); return members.signup(data).then((success) => { this.setState({showSpinner: false}); window.location.hash = 'signup-complete'; }, (error) => { this.setState({ error, showSpinner: false }); }) }; const signin = (data) => members.signin(data).then((success) => { this.setState({ error: null }); const clearShowLoggedIn = () => { this.setState({showLoggedIn: false}); this.close(); } this.setState({showLoggedIn: true}, () => { window.setTimeout(clearShowLoggedIn, 1500) }); }, (error) => { this.setState({ error }); }); const requestReset = (data) => members.requestPasswordReset(data).then((success) => { window.location.hash = 'password-reset-sent'; }, (error) => { this.setState({ error }); }); const resetPassword = (data) => this.handleAction(members.resetPassword(data)); const stripeConfig = paymentConfig && paymentConfig.find(({ adapter }) => adapter === 'stripe'); if (loadingConfig) { return ( Loading... ); } return ( {this.renderSignupPage({ error, stripeConfig, members, signup, closeModal, siteConfig, showSpinner})} {this.renderUpgradePage(props, state)} ); } 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); } }