Skip to content
This repository was archived by the owner on Jun 28, 2021. It is now read-only.

Commit 3c52b54

Browse files
authored
React loadable (#701)
* React Loadable for components * Eslint happy
1 parent 6198194 commit 3c52b54

File tree

13 files changed

+587
-11599
lines changed

13 files changed

+587
-11599
lines changed

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@
2929
"autoprefixer": "6.6.1",
3030
"autoprefixer-loader": "3.2.0",
3131
"babel-cli": "6.11.4",
32-
"babel-core": "6.13.2",
33-
"babel-loader": "6.2.4",
32+
"babel-core": "^6.24.0",
33+
"babel-loader": "^6.4.1",
3434
"babel-plugin-add-module-exports": "0.1.4",
35-
"babel-plugin-system-import-transformer": "2.2.1",
35+
"babel-plugin-syntax-dynamic-import": "^6.18.0",
36+
"babel-plugin-system-import-transformer": "^3.1.0",
3637
"babel-plugin-transform-decorators-legacy": "1.3.4",
3738
"babel-plugin-transform-react-constant-elements": "6.9.1",
3839
"babel-plugin-transform-react-display-name": "6.8.0",
@@ -41,7 +42,7 @@
4142
"babel-plugin-transform-runtime": "6.12.0",
4243
"babel-plugin-typecheck": "3.9.0",
4344
"babel-polyfill": "6.13.0",
44-
"babel-preset-es2015": "6.13.2",
45+
"babel-preset-es2015": "^6.24.0",
4546
"babel-preset-react": "6.11.1",
4647
"babel-preset-stage-0": "6.5.0",
4748
"babel-preset-stage-2": "6.13.0",
@@ -99,6 +100,7 @@
99100
"react-hot-loader": "3.0.0-beta.6",
100101
"react-inlinesvg": "0.5.4",
101102
"react-intl": "2.1.5",
103+
"react-loadable": "^3.0.1",
102104
"react-metrics": "1.2.1",
103105
"react-paginate": "4.1.0",
104106
"react-redux": "5.0.1",

src/components/Audioplayer/index.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import React, { Component, PropTypes } from 'react';
44
import { connect } from 'react-redux';
55
import { camelize } from 'humps';
6+
import Loadable from 'react-loadable';
67

78
import LocaleFormattedMessage from 'components/LocaleFormattedMessage';
89

@@ -15,13 +16,18 @@ import { surahType, segmentType } from 'types';
1516
// Redux
1617
import * as AudioActions from 'redux/actions/audioplayer';
1718

19+
import ComponentLoader from 'components/ComponentLoader';
1820
import Track from './Track';
1921
import Segments from './Segments';
2022
import ScrollButton from './ScrollButton';
21-
import RepeatDropdown from './RepeatDropdown';
2223

2324
const style = require('./style.scss');
2425

26+
const RepeatDropdown = Loadable({
27+
loader: () => import('./RepeatDropdown'),
28+
LoadingComponent: ComponentLoader
29+
});
30+
2531
export class Audioplayer extends Component {
2632
static propTypes = {
2733
className: PropTypes.string,
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import React, { PropTypes } from 'react';
2+
3+
const ComponentLoader = ({ isLoading, error, pastDelay }) => {
4+
if (isLoading) {
5+
return pastDelay ? <div>Loading...</div> : null;
6+
} else if (error) {
7+
return <div>Error! Component failed to load</div>;
8+
}
9+
10+
return null;
11+
};
12+
13+
ComponentLoader.propTypes = {
14+
isLoading: PropTypes.bool,
15+
error: PropTypes.any, // eslint-disable-line
16+
pastDelay: PropTypes.bool
17+
};
18+
19+
export default ComponentLoader;

src/components/PageView/index.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import React, { PropTypes } from 'react';
2+
3+
import Line from 'components/Line';
4+
import PageBreak from 'components/PageBreak';
5+
6+
const PageView = ({ lines, keys, currentVerse, options, isPlaying, audioActions }) => {
7+
const elements = keys.map((lineNum, index) => {
8+
const nextNum = keys[index + 1];
9+
const pageNum = lineNum.split('-')[0];
10+
const line = lines[lineNum];
11+
12+
if (index + 1 !== keys.length && pageNum !== nextNum.split('-')[0]) {
13+
return [
14+
<Line
15+
line={line}
16+
key={lineNum}
17+
currentVerse={currentVerse}
18+
tooltip={options.tooltip}
19+
audioActions={audioActions}
20+
isPlaying={isPlaying}
21+
/>,
22+
<PageBreak pageNum={parseInt(pageNum, 10) + 1} />
23+
];
24+
}
25+
26+
return (
27+
<Line
28+
line={line}
29+
key={lineNum}
30+
currentVerse={currentVerse}
31+
tooltip={options.tooltip}
32+
audioActions={audioActions}
33+
isPlaying={isPlaying}
34+
/>
35+
);
36+
});
37+
38+
return (
39+
<div>{elements}</div>
40+
);
41+
};
42+
43+
PageView.propTypes = {
44+
keys: PropTypes.array, // eslint-disable-line
45+
lines: PropTypes.object.isRequired, // eslint-disable-line
46+
audioActions: PropTypes.object.isRequired, // eslint-disable-line
47+
currentVerse: PropTypes.string,
48+
bookmarks: PropTypes.object.isRequired, // eslint-disable-line
49+
options: PropTypes.object.isRequired, // eslint-disable-line
50+
isPlaying: PropTypes.bool
51+
};
52+
53+
export default PageView;

src/components/TopOptions/index.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
import React from 'react';
2-
import Title from 'quran-components/lib/SurahTitle';
32
import Share from 'components/Share';
43
import { surahType } from 'types';
54

6-
const styles = require('./style.scss');
7-
85
const TopOptions = ({ chapter }) => (
96
<div className="row">
107
<div className="col-md-4 hidden-xs hidden-sm">
11-
<Title chapterNumber={chapter.id} className={styles.title} color={'#2CA4AB'} />
8+
{/* NOTE: Caused about 7000 lines of code to accept all titles SVG */}
9+
{/* <Title chapterNumber={chapter.id} className={styles.title} color={'#2CA4AB'} /> */}
1210
</div>
1311
<div className="col-md-8 text-right">
1412
<ul className="list-inline">

src/components/Verse/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,28 @@
11
import React, { Component, PropTypes } from 'react';
22
import Link from 'react-router/lib/Link';
3-
import { Element } from 'react-scroll';
3+
import Element from 'react-scroll/lib/components/Element';
44
import { connect } from 'react-redux';
5+
import Loadable from 'react-loadable';
56

67
import { verseType, matchType, surahType } from 'types';
7-
import Share from 'components/Share';
8-
import Copy from 'components/Copy';
8+
import ComponentLoader from 'components/ComponentLoader';
99
import LocaleFormattedMessage from 'components/LocaleFormattedMessage';
1010
import Word from 'components/Word';
1111
import Translation from 'components/Translation';
1212
import debug from 'helpers/debug';
1313

1414
const styles = require('./style.scss');
1515

16+
const Copy = Loadable({
17+
loader: () => import('components/Copy'),
18+
LoadingComponent: ComponentLoader
19+
});
20+
21+
const Share = Loadable({
22+
loader: () => import('components/Share'),
23+
LoadingComponent: ComponentLoader
24+
});
25+
1626
class Verse extends Component {
1727
static propTypes = {
1828
isSearched: PropTypes.bool,

src/containers/Surah/index.js

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,14 @@ import { asyncConnect } from 'redux-connect';
88
import { push } from 'react-router-redux';
99

1010
import Helmet from 'react-helmet';
11+
import Loadable from 'react-loadable';
1112

1213
// components
1314
import Loader from 'quran-components/lib/Loader';
1415
import LazyLoad from 'components/LazyLoad';
15-
import PageBreak from 'components/PageBreak';
16-
import Audioplayer from 'components/Audioplayer';
17-
import SurahInfo from 'components/SurahInfo';
1816
import Verse from 'components/Verse';
19-
import Line from 'components/Line';
17+
import ComponentLoader from 'components/ComponentLoader';
2018
import Bismillah from 'components/Bismillah';
21-
import TopOptions from 'components/TopOptions';
2219
import LocaleFormattedMessage from 'components/LocaleFormattedMessage';
2320

2421
// utils
@@ -42,6 +39,24 @@ const LoaderStyle = { width: '10em', height: '10em' };
4239

4340
const style = require('./style.scss');
4441

42+
const PageView = Loadable({
43+
loader: () => import('components/PageView'),
44+
LoadingComponent: ComponentLoader
45+
});
46+
47+
const Audioplayer = Loadable({
48+
loader: () => import('components/Audioplayer'),
49+
LoadingComponent: ComponentLoader
50+
});
51+
const SurahInfo = Loadable({
52+
loader: () => import('components/SurahInfo'),
53+
LoadingComponent: ComponentLoader
54+
});
55+
const TopOptions = Loadable({
56+
loader: () => import('components/TopOptions'),
57+
LoadingComponent: ComponentLoader
58+
});
59+
4560
class Surah extends Component {
4661
static propTypes = {
4762
chapter: surahType.isRequired,
@@ -310,36 +325,16 @@ class Surah extends Component {
310325
const { lines, options, currentVerse, isPlaying, actions } = this.props;
311326
const keys = Object.keys(lines);
312327

313-
return keys.map((lineNum, index) => {
314-
const nextNum = keys[index + 1];
315-
const pageNum = lineNum.split('-')[0];
316-
const line = lines[lineNum];
317-
318-
if (index + 1 !== keys.length && pageNum !== nextNum.split('-')[0]) {
319-
return [
320-
<Line
321-
line={line}
322-
key={lineNum}
323-
currentVerse={currentVerse}
324-
tooltip={options.tooltip}
325-
audioActions={actions.audio}
326-
isPlaying={isPlaying}
327-
/>,
328-
<PageBreak pageNum={parseInt(pageNum, 10) + 1} />
329-
];
330-
}
331-
332-
return (
333-
<Line
334-
line={line}
335-
key={lineNum}
336-
currentVerse={currentVerse}
337-
tooltip={options.tooltip}
338-
audioActions={actions.audio}
339-
isPlaying={isPlaying}
340-
/>
341-
);
342-
});
328+
return (
329+
<PageView
330+
lines={lines}
331+
keys={keys}
332+
options={options}
333+
currentVerse={currentVerse}
334+
audioActions={actions.audio}
335+
isPlaying={isPlaying}
336+
/>
337+
);
343338
}
344339

345340
render() {

src/routes.js

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,25 @@ export default (store) => {
3939
return (
4040
<Route path="/" component={App} onEnter={shouldAuth}>
4141
<IndexRoute components={Home} />
42-
<Route path="/donations" getComponent={(nextState, cb) => System.import('./containers/Donations').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
43-
<Route path="/contributions" getComponent={(nextState, cb) => System.import('./containers/Donations').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
42+
<Route path="/donations" getComponent={(nextState, cb) => import('./containers/Donations').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
43+
<Route path="/contributions" getComponent={(nextState, cb) => import('./containers/Donations').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
4444

45-
<Route path="/about" getComponent={(nextState, cb) => System.import('./containers/About').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
45+
<Route path="/about" getComponent={(nextState, cb) => import('./containers/About').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
4646

47-
<Route path="/contact" getComponent={(nextState, cb) => System.import('./containers/Contact').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
48-
<Route path="/contactus" getComponent={(nextState, cb) => System.import('./containers/Contact').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
47+
<Route path="/contact" getComponent={(nextState, cb) => import('./containers/Contact').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
48+
<Route path="/contactus" getComponent={(nextState, cb) => import('./containers/Contact').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
4949

50-
<Route path="/mobile" getComponent={(nextState, cb) => System.import('./containers/MobileLanding').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
51-
<Route path="/apps" getComponent={(nextState, cb) => System.import('./containers/MobileLanding').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
50+
<Route path="/mobile" getComponent={(nextState, cb) => import('./containers/MobileLanding').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
51+
<Route path="/apps" getComponent={(nextState, cb) => import('./containers/MobileLanding').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
5252

53-
<Route path="/error/:errorKey" getComponent={(nextState, cb) => System.import('./containers/Error').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
53+
<Route path="/error/:errorKey" getComponent={(nextState, cb) => import('./containers/Error').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
5454

55-
<Route path="/search" getComponent={(nextState, cb) => System.import('./containers/Search').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
55+
<Route path="/search" getComponent={(nextState, cb) => import('./containers/Search').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
5656

57-
<Route path="/login" getComponent={(nextState, cb) => System.import('./containers/Login').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
57+
<Route path="/login" getComponent={(nextState, cb) => import('./containers/Login').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
5858

5959
<Route onEnter={requireLogin}>
60-
<Route path="/profile" getComponent={(nextState, cb) => System.import('./containers/Profile').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
60+
<Route path="/profile" getComponent={(nextState, cb) => import('./containers/Profile').then(module => cb(null, module.default)).catch(err => console.trace(err))} />
6161
</Route>
6262

6363
<Redirect from="/:chapterId:(:range)" to="/:chapterId(/:range)" />
@@ -66,9 +66,9 @@ export default (store) => {
6666
path="/:chapterId(/:range)"
6767
getComponents={(nextState, cb) =>
6868
Promise.all([
69-
System.import('./containers/Surah'),
70-
System.import('./components/GlobalNav/Surah'),
71-
System.import('./components/GlobalSidebar/Surah'),
69+
import('./containers/Surah'),
70+
import('./components/GlobalNav/Surah'),
71+
import('./components/GlobalSidebar/Surah'),
7272
])
7373
.then(modules => cb(
7474
null,

0 commit comments

Comments
 (0)