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

Commit 90458ac

Browse files
authored
Fixes #693 rerendering (#695)
* Fixes #693 Rerendering on the client * ESlint
1 parent 1fc9bb8 commit 90458ac

File tree

5 files changed

+56
-48
lines changed

5 files changed

+56
-48
lines changed

src/components/Verse/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Verse extends Component {
8585
const array = match || verse.translations || [];
8686

8787
return array.map((translation, index) => (
88-
<Translation translation={translation} index={index} />
88+
<Translation translation={translation} index={index} key={index} />
8989
));
9090
}
9191

@@ -274,7 +274,7 @@ class Verse extends Component {
274274

275275
render() {
276276
const { verse, iscurrentVerse } = this.props;
277-
debug('component:Verse', `Render ${this.props.verse.ayahNum}`);
277+
debug('component:Verse', `Render ${this.props.verse.verseKey}`);
278278

279279
return (
280280
<Element

src/containers/Surah/connect.js

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,40 @@ import {
77

88
import {
99
clearCurrent,
10-
load as loadAyahs
10+
load as loadAyahs,
11+
isLoaded
1112
} from 'redux/actions/verses.js';
1213

13-
import { debug, isLoaded } from 'helpers';
14+
import { debug } from 'helpers';
1415

15-
const ayahRangeSize = 30;
16+
const determinePage = (range) => {
17+
let from;
18+
let to;
19+
20+
if (range) {
21+
if (range.includes('-')) {
22+
[from, to] = range.split('-').map(value => parseInt(value, 10));
23+
24+
if (isNaN(from) || isNaN(to)) return {};
25+
26+
return {
27+
offset: from - 1,
28+
limit: to - from
29+
};
30+
}
31+
32+
const offset = parseInt(range, 10);
33+
34+
if (isNaN(offset)) return {};
35+
36+
return {
37+
offset: offset - 1,
38+
limit: 1
39+
};
40+
}
41+
42+
return {};
43+
};
1644

1745
export const chaptersConnect = ({ store: { getState, dispatch } }) => {
1846
debug('component:Surah:chaptersConnect', 'Init');
@@ -43,48 +71,24 @@ export const chapterInfoConnect = ({ store: { dispatch }, params }) => {
4371
export const versesConnect = ({ store: { dispatch, getState }, params }) => {
4472
debug('component:Surah:versesConnect', 'Init');
4573

46-
const range = params.range;
4774
const chapterId = parseInt(params.chapterId, 10);
48-
49-
let from;
50-
let to;
51-
52-
if (range) {
53-
if (range.includes('-')) {
54-
[from, to] = range.split('-');
55-
} else {
56-
// Single ayah. For example /2/30
57-
from = range;
58-
to = range;
59-
}
60-
61-
if (isNaN(from) || isNaN(to)) {
62-
// Something wrong happened like /2/SOMETHING
63-
// going to rescue by giving beginning of surah.
64-
[from, to] = [1, ayahRangeSize];
65-
}
66-
} else {
67-
[from, to] = [1, ayahRangeSize];
68-
}
69-
70-
from = parseInt(from, 10);
71-
to = parseInt(to, 10);
75+
const paging = determinePage(params.range);
7276

7377
if (chapterId !== getState().chapters.current) {
7478
dispatch(setCurrentSurah(chapterId));
7579
}
7680

77-
if (!isLoaded(getState(), chapterId, from, to)) {
81+
if (!isLoaded(getState(), chapterId, paging)) {
7882
debug('component:Surah:versesConnect', 'Not loaded');
7983

8084
dispatch(clearCurrent(chapterId)); // In the case where you go to same surah but later ayahs.
8185

8286
if (__CLIENT__) {
83-
dispatch(loadAyahs(chapterId, from, to, getState().options));
87+
dispatch(loadAyahs(chapterId, paging, getState().options));
8488
return true;
8589
}
8690

87-
return dispatch(loadAyahs(chapterId, from, to, getState().options));
91+
return dispatch(loadAyahs(chapterId, paging, getState().options));
8892
}
8993

9094
return true;

src/containers/Surah/index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,10 @@ class Surah extends Component {
148148
const size = 10;
149149
const from = range[1];
150150
const to = (from + size);
151+
const paging = { offset: from, limit: to - from };
151152

152153
if (!isEndOfSurah && !verseIds.has(to)) {
153-
actions.verse.load(chapter.chapterNumber, from, to, options).then(() => {
154+
actions.verse.load(chapter.chapterNumber, paging, options).then(() => {
154155
this.setState({ lazyLoading: false });
155156
return callback && callback();
156157
});

src/helpers/index.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1 @@
1-
export function isLoaded(globalState, chapterId, from, to) {
2-
return (
3-
globalState.verses.entities[chapterId] &&
4-
globalState.verses.entities[chapterId][`${chapterId}:${from}`] &&
5-
globalState.verses.entities[chapterId][`${chapterId}:${to}`]
6-
);
7-
}
8-
export { default as debug } from './debug';
1+
export { default as debug } from './debug'; // eslint-disable-line

src/redux/actions/verses.js

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import cookie from 'react-cookie';
21
import { versesSchema } from 'redux/schemas';
32

43
import {
@@ -11,24 +10,27 @@ import {
1110
CLEAR_CURRENT_WORD
1211
} from 'redux/constants/verses.js';
1312

14-
// For safe measure
13+
// NOTE: For safe measure
1514
const defaultOptions = {
1615
audio: 8,
1716
translations: [20]
1817
};
1918

20-
export function load(id, from, to, options = defaultOptions) {
19+
// NOTE: From the API!
20+
const perPage = 10;
21+
22+
export function load(id, paging, options = defaultOptions) {
2123
const { audio, translations } = options;
2224

23-
cookie.save('lastVisit', JSON.stringify({ chapterId: id, verseId: from }));
25+
// TODO: move this to module/verses
26+
// cookie.save('lastVisit', JSON.stringify({ chapterId: id, verseId: from }));
2427

2528
return {
2629
types: [LOAD, LOAD_SUCCESS, LOAD_FAIL],
2730
schema: { verses: [versesSchema] },
2831
promise: client => client.get(`/api/v3/chapters/${id}/verses`, {
2932
params: {
30-
from,
31-
to,
33+
...paging,
3234
recitation: audio,
3335
translations
3436
}
@@ -63,3 +65,11 @@ export function setCurrentWord(id) {
6365
id
6466
};
6567
}
68+
69+
export function isLoaded(globalState, chapterId, paging) {
70+
return (
71+
globalState.verses.entities[chapterId] &&
72+
globalState.verses.entities[chapterId][`${chapterId}:${paging.offset || 1}`] &&
73+
globalState.verses.entities[chapterId][`${chapterId}:${paging.offset + paging.limit || perPage}`]
74+
);
75+
}

0 commit comments

Comments
 (0)