From a2327632a55620b8cf32ccac8e8ea489d86fa67c Mon Sep 17 00:00:00 2001 From: Ray Schamp Date: Sat, 24 Oct 2015 18:34:00 -0400 Subject: [PATCH] Handle relaunch requests from the iframe Only attach the message listener when the modal is displaying. This prevents multiple listeners being set up by multiple registration components on the page. Also, scope the `onMessage` handler to that component's iframe, so that we don't respond to other component's messages. --- src/components/registration/registration.jsx | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/components/registration/registration.jsx b/src/components/registration/registration.jsx index 51b73ff61..b9b309a3c 100644 --- a/src/components/registration/registration.jsx +++ b/src/components/registration/registration.jsx @@ -13,13 +13,27 @@ var Registration = React.createClass({ }, onMessage: function (e) { if (e.origin != window.location.origin) return; + if (e.source != this.refs.registrationIframe.contentWindow) return; if (e.data == 'registration-done') this.props.onRegistrationDone(); + if (e.data == 'registration-relaunch') { + this.refs.registrationIframe.contentWindow.location.reload(); + } + }, + toggleMessageListener: function (present) { + if (present) { + window.addEventListener('message', this.onMessage); + } else { + window.removeEventListener('message', this.onMessage); + } }, componentDidMount: function () { - window.addEventListener('message', this.onMessage); + if (this.props.isOpen) this.toggleMessageListener(true); + }, + componentDidUpdate: function (prevProps) { + this.toggleMessageListener(this.props.isOpen && !prevProps.isOpen); }, componentWillUnmount: function () { - window.removeEventListener('message', this.onMessage); + this.toggleMessageListener(false); }, render: function () { var frameSettings = { @@ -32,7 +46,7 @@ var Registration = React.createClass({ onRequestClose={this.props.onRequestClose} className="registration" frameSettings={frameSettings}> -