alpinesmuseum-public/assets/js/components/AppContent/CatalogList.js

104 lines
2.7 KiB
JavaScript

import React, { Component, createRef } from 'react';
import PropTypes from 'prop-types';
import CatalogItem from './CatalogItem';
import shuffleSeed from 'shuffle-seed';
import reduxLang from '../../middleware/lang';
import autobind from 'autobind-decorator';
import 'intersection-observer';
const rand = () => Math.floor(Math.random() * 1000000) + 1;
@autobind
class _CatalogList extends Component {
constructor(props) {
super(props);
this.bottomElement = createRef();
this.observer = undefined;
this.state = {
pageNumber: 1
};
}
componentDidMount() {
this.props.rebuildLayout();
if (this.props.items.length) {
// Add an observer to the intersection with the bottom element
// (to know when the user scrolled to the end)
setImmediate(this.observeIntersection);
}
}
observeIntersection() {
// if there is an old obeserver
if (this.observer) {
// we don't need it anymore so we disconnect itfl
this.observer.disconnect();
}
if (!this.bottomElement.current) {
// we don't need the observer if there is no "load more" button
// (due to the fact that there are not a lot of elements)
return;
}
this.observer = new IntersectionObserver(entries => {
let isIntersecting = false;
entries.forEach(entry => {
if (entry.isIntersecting) {
isIntersecting = true;
}
});
if (isIntersecting) {
this.loadMore();
}
});
this.observer.observe(this.bottomElement.current);
}
loadMore() {
this.setState({ pageNumber: this.state.pageNumber + 1 });
setImmediate(this.props.rebuildLayout);
}
render() {
const { items, ads, openDetail, showingSearchResults, t, isInternal } = this.props;
const ITEMS_PER_PAGE = 20;
let displayed_items = [];
displayed_items = items.slice(0, this.state.pageNumber * ITEMS_PER_PAGE);
let hasMore = true;
if (displayed_items.length === items.length) {
hasMore = false;
}
if (showingSearchResults && !displayed_items.length) {
return <div className='CatalogList--noResults medium-text'>{t('nothing_found')}</div>;
}
return (
<>
<div className='grid-container'>
{displayed_items.map((item, i) => (
<CatalogItem item={item} key={i} index={i} openDetail={openDetail} isInternal={isInternal} />
))}
</div>
{hasMore && displayed_items.length && (
<button
className='load-more-btn medium-text'
ref={this.bottomElement}
onClick={this.loadMore}
>
{t('load_more')}
</button>
)}
</>
);
}
}
_CatalogList.propTypes = {
items: PropTypes.array.isRequired,
openDetail: PropTypes.func.isRequired,
showingSearchResults: PropTypes.bool.isRequired,
t: PropTypes.func.isRequired
};
const CatalogList = reduxLang('AppContent')(_CatalogList);
export default CatalogList;