Ghost/ghost/members-auth-pages/components/MembersProvider.js
Fabien O'Carroll b1a1f61d5d Refactored auth pages for future flows (#10458)
no-issue

* Used camelCase for gateway method calls
* Added some components for building blocks of forms
* Added input specific components
* Added Form component
    This handles collecting the data to submit and sharing state between forms
* Added Pages component to handle urls
* Added the pages for the popup
* Added MembersProvider component
    This is designed to give its children access to gateway methods
* Added Modal component
    This wraps the pages and handles dispatching form submissions to the members gateway
* Refactored index.js to use new components/pages
* Fixed default page from Signup -> Signin
2019-05-07 17:15:50 +02:00

55 lines
1.6 KiB
JavaScript

import { Component } from 'preact';
const layer0 = require('../layer0');
export default class MembersProvider extends Component {
constructor() {
super();
this.setGatewayFrame = gatewayFrame => this.gatewayFrame = gatewayFrame;
this.gateway = null;
}
getChildContext() {
return {
members: {
createSubscription: this.createMethod('createSubscription'),
signin: this.createMethod('signin'),
signup: this.createMethod('signup'),
requestPasswordReset: this.createMethod('requestPasswordReset'),
resetPassword: this.createMethod('resetPassword')
}
};
}
render({apiUrl, children}) {
const src = `${apiUrl}/members/gateway`;
return (
<div>
{ children }
<iframe src={src} ref={this.setGatewayFrame} id="members-gateway" style="display: none;"/>
</div>
);
}
componentDidMount() {
const gatewayFrame = this.gatewayFrame;
gatewayFrame.addEventListener('load', () => {
this.gateway = layer0(gatewayFrame)
});
}
createMethod(method) {
return (options) => {
return new Promise((resolve, reject) =>
this.gateway.call(method, options, (err, successful) => {
if (err || !successful) {
reject(err || !successful);
}
resolve(successful);
})
);
}
}
}