Skip to content

Commit 594c007

Browse files
Added lazy loading feature
1 parent bf900bc commit 594c007

File tree

2 files changed

+82
-53
lines changed

2 files changed

+82
-53
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@csvbox/react",
3-
"version": "1.1.1",
3+
"version": "1.1.5-alpha.1",
44
"description": "React adapter for csvbox.io",
55
"author": "csvbox-io",
66
"license": "MIT",

src/index.js

Lines changed: 81 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,66 @@
11
import React, { Component } from 'react';
22
// import styles from './styles.module.css'
3-
43
import { version } from '../package.json';
54

65
export class CSVBoxButton extends Component {
76

87
constructor(props) {
98
super(props)
109
this.holder = React.createRef();
11-
this.iframe = React.createRef();
10+
// this.iframe = React.createRef();
1211
this.openModal = this.openModal.bind(this)
1312
this.isModalShown = false;
13+
this.shouldOpenModalOnReady = false;
1414
this.uuid = this.generateUuid();
1515
this.state = {
16-
disabled: true
16+
isLoading: true
1717
};
18+
this.iframe = null;
1819
}
1920

2021
componentDidMount() {
22+
const { lazy } = this.props;
23+
if(lazy) {
24+
this.enableInitator();
25+
} else {
26+
this.initImporter();
27+
}
28+
}
29+
30+
initImporter() {
2131

22-
const { onImport } = this.props;
2332
const { user } = this.props;
2433
const { dynamicColumns } = this.props;
2534
const { options } = this.props;
2635
const { onReady } = this.props;
36+
const { onImport } = this.props;
2737
const { onSubmit } = this.props;
2838
const { onClose } = this.props;
39+
const { licenseKey } = this.props;
40+
const { dataLocation } = this.props;
41+
const { customDomain } = this.props;
42+
const { language } = this.props;
2943

30-
window.addEventListener("message", (event) => {
44+
let domain = customDomain ? customDomain : "app.csvbox.io";
45+
46+
if(dataLocation) {
47+
domain = `${dataLocation}-${domain}`;
48+
}
3149

32-
// console.log("message", event);
50+
let iframeUrl = `https://${domain}/embed/${licenseKey}`;
51+
52+
iframeUrl += `?library-version=${version}`;
53+
iframeUrl += "&framework=react";
54+
55+
if(dataLocation) {
56+
iframeUrl += "&preventRedirect";
57+
}
58+
59+
if(language) {
60+
iframeUrl += "&language=" + language;
61+
}
62+
63+
window.addEventListener("message", (event) => {
3364

3465
if (event.data === "mainModalHidden") {
3566
if (this.holder && this.holder.current) {
@@ -118,35 +149,64 @@ export class CSVBoxButton extends Component {
118149
this.onImport?.(false);
119150
}
120151
}
121-
122-
123152
}
124153
}, false);
125-
let iframe = this.iframe.current;
126154

127155
let self = this;
128156

129-
iframe.onload = function () {
157+
let iframe = document.createElement("iframe");
158+
this.iframe = iframe;
159+
iframe.setAttribute("src", iframeUrl);
160+
iframe.frameBorder = 0;
161+
iframe.classList.add('csvbox-iframe');
130162

131-
onReady?.();
163+
iframe.style.height = "100%";
164+
iframe.style.width = "100%";
165+
iframe.style.position = "absolute";
166+
iframe.style.top = "0px";
167+
iframe.style.left = "0px";
132168

133-
self.enableInitator();
169+
window.addEventListener("message", this.onMessageEvent, false);
134170

171+
iframe.onload = function () {
172+
self.enableInitator();
135173
iframe.contentWindow.postMessage({
136174
"customer" : user ? user : null,
137175
"columns" : dynamicColumns ? dynamicColumns : null,
138176
"options" : options ? options : null,
139177
"unique_token": self.uuid
140178
}, "*");
141-
179+
onReady?.();
180+
if(self.shouldOpenModalOnReady) {
181+
self.openModal();
182+
self.shouldOpenModalOnReady = false;
183+
}
142184
}
185+
this.holder.current.appendChild(iframe);
143186
}
187+
144188
openModal() {
189+
190+
const { lazy } = this.props;
191+
192+
if(lazy) {
193+
if(!this.iframe) {
194+
this.shouldOpenModalOnReady = true;
195+
this.initImporter();
196+
return;
197+
}
198+
}
199+
145200
if(!this.isModalShown) {
146-
this.isModalShown = true;
147-
this.iframe.current.contentWindow.postMessage('openModal', '*');
148-
this.holder.current.style.display = 'block';
201+
if(!this.state.isLoading) {
202+
this.isModalShown = true;
203+
this.iframe.contentWindow.postMessage('openModal', '*');
204+
this.holder.current.style.display = 'block';
205+
} else {
206+
this.shouldOpenModalOnReady = true;
207+
}
149208
}
209+
150210
}
151211

152212
generateUuid() {
@@ -155,31 +215,12 @@ export class CSVBoxButton extends Component {
155215

156216
enableInitator() {
157217
this.setState({
158-
disabled: !this.state.disabled
218+
isLoading: false
159219
})
160220
}
161221

162222
render() {
163223

164-
const { licenseKey } = this.props;
165-
const { dataLocation } = this.props;
166-
const { customDomain } = this.props;
167-
168-
let domain = customDomain ? customDomain : "app.csvbox.io";
169-
170-
if(dataLocation) {
171-
domain = `${dataLocation}-${domain}`;
172-
}
173-
174-
let iframeUrl = `https://${domain}/embed/${licenseKey}`;
175-
176-
iframeUrl += `?library-version=${version}`;
177-
iframeUrl += "&framework=react";
178-
179-
if(dataLocation) {
180-
iframeUrl += "&preventRedirect";
181-
}
182-
183224
const holderStyle = {
184225
display: "none",
185226
zIndex: 2147483647,
@@ -190,30 +231,18 @@ export class CSVBoxButton extends Component {
190231
right: 0
191232
};
192233

193-
const iframeStyle = {
194-
height: "100%",
195-
width: "100%",
196-
position: "absolute",
197-
top: "0px",
198-
left: "0px"
199-
};
200-
201234
if(this.props.render) {
202235
return (
203236
<div>
204-
{this.props.render(this.openModal, !this.state.disabled)}
205-
<div ref={this.holder} style={holderStyle}>
206-
<iframe ref={this.iframe} style={iframeStyle} data-csvbox-token={this.uuid} src={iframeUrl} frameBorder="0" ></iframe>
207-
</div>
237+
{this.props.render(this.openModal, this.state.isLoading)}
238+
<div ref={this.holder} style={holderStyle}></div>
208239
</div>
209240
)
210241
}else{
211242
return (
212243
<div>
213-
<button disabled={this.state.disabled} onClick={this.openModal} data-csvbox-initator data-csvbox-token={this.uuid}>{this.props.children}</button>
214-
<div ref={this.holder} style={holderStyle}>
215-
<iframe ref={this.iframe} style={iframeStyle} data-csvbox-token={this.uuid} src={iframeUrl} frameBorder="0" ></iframe>
216-
</div>
244+
<button disabled={this.state.isLoading} onClick={this.openModal} data-csvbox-initator data-csvbox-token={this.uuid}>{this.props.children}</button>
245+
<div ref={this.holder} style={holderStyle}></div>
217246
</div>
218247
)
219248
}

0 commit comments

Comments
 (0)