2019-05-07 11:29:07 +03:00
|
|
|
import {Component} from 'preact';
|
2019-02-07 12:39:55 +03:00
|
|
|
|
2019-04-18 16:53:36 +03:00
|
|
|
const gatewayProtocol = require('@tryghost/members-gateway-protocol');
|
2019-02-07 12:39:55 +03:00
|
|
|
|
|
|
|
export default class MembersProvider extends Component {
|
|
|
|
constructor() {
|
|
|
|
super();
|
|
|
|
this.setGatewayFrame = gatewayFrame => this.gatewayFrame = gatewayFrame;
|
2019-02-14 19:59:41 +03:00
|
|
|
this.ready = new Promise((resolve) => {
|
|
|
|
this.setReady = resolve;
|
2019-05-07 11:29:07 +03:00
|
|
|
});
|
2019-02-07 12:39:55 +03:00
|
|
|
this.gateway = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
getChildContext() {
|
|
|
|
return {
|
|
|
|
members: {
|
|
|
|
createSubscription: this.createMethod('createSubscription'),
|
|
|
|
signin: this.createMethod('signin'),
|
|
|
|
signup: this.createMethod('signup'),
|
|
|
|
requestPasswordReset: this.createMethod('requestPasswordReset'),
|
2019-02-14 19:59:41 +03:00
|
|
|
resetPassword: this.createMethod('resetPassword'),
|
2019-05-07 11:29:07 +03:00
|
|
|
getConfig: this.createMethod('getConfig')
|
2019-02-07 12:39:55 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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', () => {
|
2019-05-07 11:29:07 +03:00
|
|
|
this.gateway = gatewayProtocol(gatewayFrame);
|
2019-02-14 19:59:41 +03:00
|
|
|
this.setReady();
|
2019-02-07 12:39:55 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
createMethod(method) {
|
|
|
|
return (options) => {
|
2019-02-14 19:59:41 +03:00
|
|
|
return this.ready.then(() => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
this.gateway.call(method, options, (err, successful) => {
|
|
|
|
if (err || !successful) {
|
|
|
|
reject(err || !successful);
|
|
|
|
}
|
|
|
|
resolve(successful);
|
2019-05-07 11:29:07 +03:00
|
|
|
});
|
2019-02-14 19:59:41 +03:00
|
|
|
});
|
|
|
|
});
|
2019-05-07 11:29:07 +03:00
|
|
|
};
|
2019-02-07 12:39:55 +03:00
|
|
|
}
|
|
|
|
}
|