Skip to content

Commit a8ec97c

Browse files
committed
feat@timetable-infinite_scroll
1 parent de1eaf6 commit a8ec97c

File tree

3 files changed

+91
-15
lines changed

3 files changed

+91
-15
lines changed

src/components/sections/dictionary/courselist/CourseListSection.jsx

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,6 @@ const REFRESH_LIMIT = 10;
3737
class CourseListSection extends Component {
3838
constructor(props) {
3939
super(props);
40-
this.offSetRef = React.createRef();
41-
}
42-
43-
componentDidMount() {
44-
if (this.offSetRef.current) {
45-
this.offSetRef.current.value = REFRESH_LIMIT;
46-
}
4740
}
4841

4942
onScrollChange = (e) => {

src/components/sections/timetable/lecturelist/LectureListSection.jsx

Lines changed: 84 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import React, { Component } from 'react';
22
import { connect } from 'react-redux';
33
import PropTypes from 'prop-types';
44
import { withTranslation } from 'react-i18next';
5+
import axios from 'axios';
56
import ReactGA from 'react-ga4';
67
import { range } from 'lodash';
78

@@ -21,9 +22,11 @@ import {
2122
deleteLectureFromCart,
2223
setIsLectureListOpenOnMobile,
2324
} from '../../../../redux/actions/timetable/list';
24-
import { openSearch } from '../../../../redux/actions/timetable/search';
25+
import { openSearch, setLastSearchOption } from '../../../../redux/actions/timetable/search';
2526
import { addLectureToTimetable } from '../../../../redux/actions/timetable/timetable';
2627

28+
import { setListLectures } from '../../../../redux/actions/timetable/list';
29+
2730
import { LectureFocusFrom } from '@/shapes/enum';
2831
import { LectureListCode } from '@/shapes/enum';
2932

@@ -59,12 +62,20 @@ import {
5962
import LectureGroupBlockRow from '../../../blocks/LectureGroupBlockRow';
6063
import { TIMETABLE_START_HOUR } from '../../../../common/constants';
6164

65+
const REFRESH_LIMIT = 50;
66+
6267
class LectureListSection extends Component {
6368
constructor(props) {
6469
super(props);
6570
this.arrowRef = React.createRef();
6671
}
6772

73+
onScrollChange = (e) => {
74+
const { scrollTop, scrollHeight, clientHeight } = e;
75+
76+
if (scrollTop + clientHeight >= scrollHeight) this._addLectureGroups();
77+
};
78+
6879
componentDidMount() {
6980
window.addEventListener('resize', this.selectWithArrow);
7081
}
@@ -293,6 +304,64 @@ class LectureListSection extends Component {
293304
return lists[selectedListCode].lectureGroups;
294305
};
295306

307+
_addLectureGroups = async () => {
308+
const {
309+
lastSearchOption,
310+
setLastSearchOption,
311+
year,
312+
semester,
313+
setListLecturesDispatch,
314+
lists,
315+
} = this.props;
316+
317+
const lectures = this._getLectureGroups(LectureListCode.SEARCH, lists);
318+
319+
let offset = 0;
320+
321+
for (let i = 0; i < lectures.length - 1; i++) {
322+
offset += lectures[i].length;
323+
}
324+
325+
const option = {
326+
...lastSearchOption,
327+
offset,
328+
limit: offset + REFRESH_LIMIT,
329+
};
330+
331+
await axios
332+
.get('/api/lectures', {
333+
params: {
334+
year: year,
335+
semester: semester,
336+
...option,
337+
order: ['old_code', 'class_no'],
338+
},
339+
metadata: {
340+
gaCategory: 'Timetable',
341+
gaVariable: 'POST / List',
342+
},
343+
})
344+
.then((response) => {
345+
const newProps = this.props;
346+
if (newProps.year !== year || newProps.semester !== semester) {
347+
return;
348+
}
349+
if (response.data.length > 0) {
350+
const newLectures = lectures;
351+
let spreadedLectures = [];
352+
353+
for (let i = 0; i < newLectures.length - 1; i++) {
354+
spreadedLectures = spreadedLectures.concat(newLectures[i]);
355+
}
356+
357+
spreadedLectures = spreadedLectures.concat(response.data);
358+
359+
setListLecturesDispatch(LectureListCode.SEARCH, spreadedLectures);
360+
}
361+
})
362+
.catch((error) => {});
363+
};
364+
296365
render() {
297366
const { t } = this.props;
298367
const { user, lectureFocus, selectedTimetable, selectedListCode, lastSearchOption, lists } =
@@ -376,7 +445,12 @@ class LectureListSection extends Component {
376445
);
377446
}
378447
return (
379-
<Scroller onScroll={this.selectWithArrow} key={selectedListCode}>
448+
<Scroller
449+
onScroll={(e) => {
450+
this.selectWithArrow();
451+
this.onScrollChange(e);
452+
}}
453+
key={selectedListCode}>
380454
<div className={classNames('block-list')}>
381455
{lectureGroups.map((lg) => (
382456
<LectureGroupBlock
@@ -467,6 +541,12 @@ const mapDispatchToProps = (dispatch) => ({
467541
setIsLectureListOpenOnMobileDispatch: (isLectureListOpenOnMobile) => {
468542
dispatch(setIsLectureListOpenOnMobile(isLectureListOpenOnMobile));
469543
},
544+
setLastSearchOptionDispatch: (lastSearchOption) => {
545+
dispatch(setLastSearchOption(lastSearchOption));
546+
},
547+
setListLecturesDispatch: (code, lectures) => {
548+
dispatch(setListLectures(code, lectures));
549+
},
470550
});
471551

472552
LectureListSection.propTypes = {
@@ -485,8 +565,10 @@ LectureListSection.propTypes = {
485565
clearLectureFocusDispatch: PropTypes.func.isRequired,
486566
addLectureToTimetableDispatch: PropTypes.func.isRequired,
487567
addLectureToCartDispatch: PropTypes.func.isRequired,
568+
setListLecturesDispatch: PropTypes.func.isRequired,
488569
deleteLectureFromCartDispatch: PropTypes.func.isRequired,
489570
setIsLectureListOpenOnMobileDispatch: PropTypes.func.isRequired,
571+
setLastSearchOptionDispatch: PropTypes.func.isRequired,
490572
};
491573

492574
export default withTranslation()(connect(mapStateToProps, mapDispatchToProps)(LectureListSection));

src/components/sections/timetable/lecturelist/LectureSearchSubSection.jsx

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class LectureSearchSubSection extends Component {
5555
};
5656

5757
searchStart = () => {
58-
const LIMIT = 300;
58+
const LIMIT = 50;
5959

6060
const { t } = this.props;
6161
const { selectedTypes, selectedDepartments, selectedLevels, keyword } = this.state;
@@ -96,6 +96,8 @@ class LectureSearchSubSection extends Component {
9696
day: classtimeDay !== null ? classtimeDay : undefined,
9797
begin: classtimeBegin !== null ? classtimeBegin / 30 - 8 * 2 : undefined,
9898
end: classtimeEnd !== null ? classtimeEnd / 30 - 8 * 2 : undefined,
99+
limit: LIMIT,
100+
offset: 0,
99101
};
100102

101103
this.setState(this.INITIAL_STATE);
@@ -113,7 +115,6 @@ class LectureSearchSubSection extends Component {
113115
semester: semester,
114116
...option,
115117
order: ['old_code', 'class_no'],
116-
limit: LIMIT,
117118
},
118119
metadata: {
119120
gaCategory: 'Timetable',
@@ -125,10 +126,10 @@ class LectureSearchSubSection extends Component {
125126
if (newProps.year !== year || newProps.semester !== semester) {
126127
return;
127128
}
128-
if (response.data.length === LIMIT) {
129-
// eslint-disable-next-line no-alert
130-
alert(t('ui.message.tooManySearchResults', { count: LIMIT }));
131-
}
129+
// if (response.data.length === LIMIT) {
130+
// // eslint-disable-next-line no-alert
131+
// alert(t('ui.message.tooManySearchResults', { count: LIMIT }));
132+
// }
132133
setListLecturesDispatch(LectureListCode.SEARCH, response.data);
133134
})
134135
.catch((error) => {});

0 commit comments

Comments
 (0)