Skip to content

Commit

Permalink
Merge pull request #12 from datatrans/stop-lightbox-on-unmount
Browse files Browse the repository at this point in the history
clean up DOM on unmount (see #11)
  • Loading branch information
fgrDtrx authored Nov 1, 2018
2 parents f5ec2a8 + 5ce6164 commit d5e4674
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 46 deletions.
42 changes: 20 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
Expand All @@ -34,55 +34,53 @@ 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() {
return <div>
<h1>Datatrans Lightbox Demo</h1>
<div>
{this.state.showLightbox
? 'Lightbox was rendered and cannot be reused.'
? <Lightbox
{...config}
onLoaded={this.onLoaded}
onOpened={this.onOpened}
onCancelled={this.onCancelled}
onError={this.onError}
/>
: <button onClick={this.start}>Start Lightbox</button>
}

{this.state.showLightbox &&
<Lightbox
{...config}
onLoaded={this.onLoaded}
onOpened={this.onOpened}
onCancelled={this.onCancelled}
onError={this.onError} />
}
</div>

</div>
}

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
Expand Down
39 changes: 19 additions & 20 deletions example/src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,51 +17,50 @@ 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() {
return <div>
<h1>Datatrans Lightbox Demo</h1>
<div>
{this.state.showLightbox
? 'Lightbox was rendered and cannot be reused.'
? <Lightbox
{...config}
onLoaded={this.onLoaded}
onOpened={this.onOpened}
onCancelled={this.onCancelled}
onError={this.onError}
/>
: <button onClick={this.start}>Start Lightbox</button>
}

{this.state.showLightbox &&
<Lightbox
{...config}
onLoaded={this.onLoaded}
onOpened={this.onOpened}
onCancelled={this.onCancelled}
onError={this.onError} />
}
</div>

</div>
}

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 })
}
}
2 changes: 1 addition & 1 deletion lib/main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 12 additions & 3 deletions src/Lightbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,13 @@ const startPayment = (props) => {
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
Expand All @@ -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
}
Expand Down

0 comments on commit d5e4674

Please sign in to comment.