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

Commit dfb3a3b

Browse files
committed
Extract connect from the surah container (#347)
1 parent fbac673 commit dfb3a3b

File tree

2 files changed

+84
-77
lines changed

2 files changed

+84
-77
lines changed

src/containers/Surah/connect.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import { isAllLoaded, loadAll, setCurrent as setCurrentSurah } from '../../redux/modules/surahs';
2+
import { clearCurrent, isLoaded, load as loadAyahs } from '../../redux/modules/ayahs';
3+
4+
import debug from 'helpers/debug';
5+
6+
const ayahRangeSize = 30;
7+
8+
export const surahsConnect = ({ store: { getState, dispatch } }) => {
9+
debug('component:Surah:surahsConnect', 'Init');
10+
11+
if (!isAllLoaded(getState())) {
12+
debug('component:Surah:surahsConnect', 'Surahs not loaded');
13+
14+
return dispatch(loadAll());
15+
}
16+
17+
return true;
18+
}
19+
20+
export const ayahsConnect = ({ store: { dispatch, getState }, params }) => {
21+
debug('component:Surah:ayahsConnect', 'Init');
22+
23+
const range = params.range;
24+
const surahId = parseInt(params.surahId, 10);
25+
26+
let from;
27+
let to;
28+
29+
if (range) {
30+
if (range.includes('-')) {
31+
[from, to] = range.split('-');
32+
} else {
33+
// Single ayah. For example /2/30
34+
from = range;
35+
to = parseInt(range, 10) + ayahRangeSize;
36+
}
37+
38+
if (isNaN(from) || isNaN(to)) {
39+
// Something wrong happened like /2/SOMETHING
40+
// going to rescue by giving beginning of surah.
41+
[from, to] = [1, ayahRangeSize];
42+
}
43+
} else {
44+
[from, to] = [1, ayahRangeSize];
45+
}
46+
47+
from = parseInt(from, 10);
48+
to = parseInt(to, 10);
49+
50+
if (surahId !== getState().surahs.current) {
51+
dispatch(setCurrentSurah(surahId));
52+
}
53+
54+
if (!isLoaded(getState(), surahId, from, to)) {
55+
debug('component:Surah:ayahsConnect', 'Not loaded');
56+
57+
dispatch(clearCurrent(surahId)); // In the case where you go to same surah but later ayahs.
58+
59+
return dispatch(loadAyahs(surahId, from, to, getState().options));
60+
}
61+
62+
return true;
63+
}

src/containers/Surah/index.js

Lines changed: 21 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -36,80 +36,26 @@ import descriptions from './descriptions';
3636

3737
const style = require('./style.scss');
3838

39+
40+
import { surahsConnect, ayahsConnect } from './connect';
41+
3942
import {
40-
clearCurrent,
41-
isLoaded,
4243
load as loadAyahs,
4344
setCurrentAyah,
4445
setCurrentWord,
4546
clearCurrentWord
4647
} from '../../redux/modules/ayahs';
47-
import { isAllLoaded, loadAll, setCurrent as setCurrentSurah } from '../../redux/modules/surahs';
48+
4849
import { setOption, toggleReadingMode } from '../../redux/modules/options';
4950

5051
let lastScroll = 0;
51-
const ayahRangeSize = 30;
5252

5353
@asyncConnect([
5454
{
55-
promise({ store: { getState, dispatch } }) {
56-
debug('component:Surah', 'All Surahs Promise');
57-
if (!isAllLoaded(getState())) {
58-
debug('component:Surah', 'All Surahs Promise, Surahs not loaded');
59-
return dispatch(loadAll());
60-
}
61-
62-
return true;
63-
}
55+
promise: surahsConnect,
6456
},
6557
{
66-
promise({ store: { dispatch, getState }, params }) {
67-
debug('component:Surah', 'Ayahs Promise');
68-
const range = params.range;
69-
const surahId = parseInt(params.surahId, 10);
70-
const { options } = getState();
71-
let from;
72-
let to;
73-
74-
if (range) {
75-
if (range.includes('-')) {
76-
[from, to] = range.split('-');
77-
} else {
78-
// Single ayah. For example /2/30
79-
from = range;
80-
to = parseInt(range, 10) + ayahRangeSize;
81-
}
82-
83-
if (isNaN(from) || isNaN(to)) {
84-
// Something wrong happened like /2/SOMETHING
85-
// going to rescue by giving beginning of surah.
86-
[from, to] = [1, ayahRangeSize];
87-
}
88-
} else {
89-
[from, to] = [1, ayahRangeSize];
90-
}
91-
92-
if (isNaN(surahId)) {
93-
// Should have an alert or something to tell user there is an error.
94-
return dispatch(push('/'));
95-
}
96-
97-
from = parseInt(from, 10);
98-
to = parseInt(to, 10);
99-
100-
if (surahId !== getState().surahs.current) {
101-
dispatch(setCurrentSurah(surahId));
102-
}
103-
104-
if (!isLoaded(getState(), surahId, from, to)) {
105-
debug('component:Surah', 'Ayahs Promise, Ayahs not loaded');
106-
dispatch(clearCurrent(surahId)); // In the case where you go to same surah but later ayahs.
107-
108-
return dispatch(loadAyahs(surahId, from, to, options));
109-
}
110-
111-
return true;
112-
}
58+
promise: ayahsConnect
11359
}
11460
])
11561
@connect(
@@ -142,9 +88,9 @@ const ayahRangeSize = 30;
14288
};
14389
},
14490
{
145-
loadAyahsDispatch: loadAyahs,
146-
setOptionDispatch: setOption,
147-
toggleReadingModeDispatch: toggleReadingMode,
91+
loadAyahs,
92+
setOption,
93+
toggleReadingMode,
14894
setCurrentAyah,
14995
setCurrentWord,
15096
clearCurrentWord,
@@ -166,9 +112,9 @@ export default class Surah extends Component {
166112
push: PropTypes.func.isRequired,
167113
params: PropTypes.object.isRequired,
168114
ayahs: PropTypes.object.isRequired,
169-
loadAyahsDispatch: PropTypes.func.isRequired,
170-
setOptionDispatch: PropTypes.func.isRequired,
171-
toggleReadingModeDispatch: PropTypes.func.isRequired,
115+
loadAyahs: PropTypes.func.isRequired,
116+
setOption: PropTypes.func.isRequired,
117+
toggleReadingMode: PropTypes.func.isRequired,
172118
setCurrentAyah: PropTypes.func.isRequired,
173119
setCurrentWord: PropTypes.func.isRequired,
174120
clearCurrentWord: PropTypes.func.isRequired,
@@ -300,7 +246,7 @@ export default class Surah extends Component {
300246
}
301247

302248
handleOptionChange = (payload) => {
303-
const { setOptionDispatch, loadAyahsDispatch, surah, ayahIds, options } = this.props;
249+
const { setOption, loadAyahs, surah, ayahIds, options } = this.props; // eslint-disable-line no-shadow
304250
const from = ayahIds.first();
305251
const to = ayahIds.last();
306252

@@ -310,15 +256,15 @@ export default class Surah extends Component {
310256
}
311257

312258
handleFontSizeChange = (payload) => {
313-
const { setOptionDispatch } = this.props;
259+
const { setOption } = this.props; // eslint-disable-line no-shadow
314260

315-
return setOptionDispatch(payload);
261+
return setOption(payload);
316262
}
317263

318264
handleSurahInfoToggle = (payload) => {
319-
const { setOptionDispatch } = this.props;
265+
const { setOption } = this.props; // eslint-disable-line no-shadow
320266

321-
return setOptionDispatch(payload);
267+
return setOption(payload);
322268
}
323269

324270
handleNavbar = () => {
@@ -353,10 +299,8 @@ export default class Surah extends Component {
353299
1000)); // then scroll to it
354300
}
355301

356-
357302
handleLazyLoadAyahs = (callback) => {
358-
const { loadAyahsDispatch, ayahIds, surah, isEndOfSurah, options } = this.props;
359-
303+
const { loadAyahs, ayahIds, surah, isEndOfSurah, options } = this.props; // eslint-disable-line no-shadow
360304
const range = [ayahIds.first(), ayahIds.last()];
361305
let size = 10;
362306

@@ -368,7 +312,7 @@ export default class Surah extends Component {
368312
const to = (from + size);
369313

370314
if (!isEndOfSurah && !ayahIds.has(to)) {
371-
loadAyahsDispatch(surah.id, from, to, options).then(() => {
315+
loadAyahs(surah.id, from, to, options).then(() => {
372316
this.setState({lazyLoading: false});
373317
if (callback) {
374318
callback();
@@ -457,7 +401,7 @@ export default class Surah extends Component {
457401
}
458402

459403
renderTopOptions() {
460-
const { toggleReadingModeDispatch, options } = this.props;
404+
const { toggleReadingMode, options } = this.props; // eslint-disable-line no-shadow
461405

462406
return (
463407
<Row>
@@ -480,7 +424,7 @@ export default class Surah extends Component {
480424
<li>
481425
<ReadingModeToggle
482426
isToggled={options.isReadingMode}
483-
onReadingModeToggle={toggleReadingModeDispatch}
427+
onReadingModeToggle={toggleReadingMode}
484428
/>
485429
</li>
486430
</ul>

0 commit comments

Comments
 (0)