From 5ce61649f0b2936d38cc9a8ed55836d05f3a95ff Mon Sep 17 00:00:00 2001 From: Patrick Stadler Date: Thu, 1 Nov 2018 10:11:10 +0100 Subject: [PATCH] clean up DOM on unmount (see #11) --- README.md | 42 ++++++++++++++++++++---------------------- example/src/App.js | 39 +++++++++++++++++++-------------------- lib/main.js | 2 +- src/Lightbox.js | 15 ++++++++++++--- 4 files changed, 52 insertions(+), 46 deletions(-) diff --git a/README.md b/README.md index 9cff73d..243f0c5 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,7 @@ const config = { currency: 'CHF', sign: '30916165706580013', production: false, - paymentmethod: ['ECA', 'VIS', 'PFC', 'TWI'], + paymentmethod: ['ECA', 'VIS', 'PFC', 'AMX', 'TWI'], themeConfiguration: { brandColor: '#aa9374' } @@ -34,15 +34,10 @@ const config = { export default class App extends Component { constructor(props) { super(props) + this.state = { showLightbox: false } - - this.start = this.start.bind(this) - } - - start() { - this.setState({ showLightbox: true }) } render() { @@ -50,39 +45,42 @@ export default class App extends Component {

Datatrans Lightbox Demo

{this.state.showLightbox - ? 'Lightbox was rendered and cannot be reused.' + ? : } - - {this.state.showLightbox && - - }
+ } - onLoaded() { + start = () => { + this.setState({ showLightbox: true }) + } + + onLoaded = () => { console.log('Loaded') } - onOpened() { + onOpened = () => { console.log('Opened') } - onCancelled() { + onCancelled = () => { console.log('Cancelled') + this.setState({ showLightbox: false }) } - onError(data) { + onError = (data) => { console.log('Error:', data) + this.setState({ showLightbox: false }) } } - ``` ## Properties diff --git a/example/src/App.js b/example/src/App.js index 20ddf9b..18f1aab 100644 --- a/example/src/App.js +++ b/example/src/App.js @@ -17,15 +17,10 @@ const config = { export default class App extends Component { constructor(props) { super(props) + this.state = { showLightbox: false } - - this.start = this.start.bind(this) - } - - start() { - this.setState({ showLightbox: true }) } render() { @@ -33,35 +28,39 @@ export default class App extends Component {

Datatrans Lightbox Demo

{this.state.showLightbox - ? 'Lightbox was rendered and cannot be reused.' + ? : } - - {this.state.showLightbox && - - }
+ } - onLoaded() { + start = () => { + this.setState({ showLightbox: true }) + } + + onLoaded = () => { console.log('Loaded') } - onOpened() { + onOpened = () => { console.log('Opened') } - onCancelled() { + onCancelled = () => { console.log('Cancelled') + this.setState({ showLightbox: false }) } - onError(data) { + onError = (data) => { console.log('Error:', data) + this.setState({ showLightbox: false }) } } diff --git a/lib/main.js b/lib/main.js index f5545a7..c74460d 100644 --- a/lib/main.js +++ b/lib/main.js @@ -1 +1 @@ -!function(e,t){"object"==typeof exports&&"object"==typeof module?module.exports=t(require("react")):"function"==typeof define&&define.amd?define(["react"],t):"object"==typeof exports?exports.Lightbox=t(require("react")):e.Lightbox=t(e.React)}(this,function(e){return function(e){function t(r){if(n[r])return n[r].exports;var o=n[r]={i:r,l:!1,exports:{}};return e[r].call(o.exports,o,o.exports,t),o.l=!0,o.exports}var n={};return t.m=e,t.c=n,t.i=function(e){return e},t.d=function(e,n,r){t.o(e,n)||Object.defineProperty(e,n,{configurable:!1,enumerable:!0,get:r})},t.n=function(e){var n=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(n,"a",n),n},t.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},t.p="",t(t.s=9)}([function(e,t,n){"use strict";Object.defineProperty(t,"__esModule",{value:!0});var r=n(1),o=function(e){return e&&e.__esModule?e:{default:e}}(r);t.default=o.default},function(e,t,n){"use strict";function r(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function o(e,t){if(!e)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return!t||"object"!=typeof t&&"function"!=typeof t?e:t}function u(e,t){if("function"!=typeof t&&null!==t)throw new TypeError("Super expression must either be null or a function, not "+typeof t);e.prototype=Object.create(t&&t.prototype,{constructor:{value:e,enumerable:!1,writable:!0,configurable:!0}}),t&&(Object.setPrototypeOf?Object.setPrototypeOf(e,t):e.__proto__=t)}Object.defineProperty(t,"__esModule",{value:!0});var i=function(){function e(e,t){for(var n=0;n { closed: props.onCancelled, error: props.onError } - console.log(config) window.Datatrans.startPayment(config) } - export default class Lightbox extends Component { componentDidMount() { const scriptSource = getUrl(this.props.production) - if (!document.querySelectorAll('script[src="' + scriptSource + '"]')) { + if (document.querySelector('script[src="' + scriptSource + '"]')) { startPayment(this.props) return @@ -41,6 +39,17 @@ export default class Lightbox extends Component { document.body.appendChild(script) } + componentWillUnmount() { + // make sure to always clean things up + if (window.Datatrans) { + window.setTimeout(() => { + try { + window.Datatrans.close() + } catch (err) {} // eslint-disable-line no-empty + }, 1) + } + } + render() { return null }