Skip to content

Commit

Permalink
add scroll observer
Browse files Browse the repository at this point in the history
  • Loading branch information
SejoB committed Nov 26, 2024
1 parent 78f8c17 commit c12247f
Show file tree
Hide file tree
Showing 13 changed files with 169 additions and 258 deletions.
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"react-async-loader": "^0.1.2",
"react-dom": "^17.0.2",
"react-i18next": "^14.1.1",
"react-intersection-observer": "^9.13.1",
"react-leaflet": "3.2.5",
"react-leaflet-google-layer": "^2.2.0",
"react-router-dom": "^6.23.0",
Expand Down Expand Up @@ -133,7 +134,7 @@
"prettier --write"
]
},
"overrides": {
"react-refresh": "0.11.0"
}
"overrides": {
"react-refresh": "0.11.0"
}
}
4 changes: 1 addition & 3 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { FC, useEffect } from 'react';
import { BrowserRouter as Router, Route, Routes, useParams } from 'react-router-dom';
import { BrowserRouter as Router, Route, Routes } from 'react-router-dom';
import HomePage from './pages/HomePage';
import { Footer } from './components/organisms/Footer';
import { Box, createStyles, makeStyles, Theme, ThemeProvider } from '@material-ui/core';
Expand Down Expand Up @@ -31,8 +31,6 @@ const App: FC = () => {
const classes = useStyles();
const store = useStore();
const theme = useTheme();
const { newsId } = useParams();
console.log('🚀 ~ newsId:', newsId);
const appDir = i18n.dir();

useEffect(() => {
Expand Down
111 changes: 0 additions & 111 deletions src/components/atoms/InfiniteScroll.tsx

This file was deleted.

1 change: 0 additions & 1 deletion src/components/atoms/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export { default as Textbox } from './Textbox';
export { default as ErrorBoundary } from './ErrorBoundary';
export { default as MetaTag } from './MetaTag';
export { default as Loader } from './Loader';
export { default as InfiniteScroll } from './InfiniteScroll';
export { default as Dialog } from './Dialog';
export { default as AppBar } from './AppBar';
export { default as Logo } from './Logo';
Expand Down
2 changes: 1 addition & 1 deletion src/components/molecules/NewsFlashComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ const NewsFlashComp: FC<IProps> = ({ news }) => {
const verificationIcon = getVerificationIcon(news.newsflash_location_qualification);
const criticalIcon = news.critical && <CriticalIcon className={classes.icon} />;
const {newsId} = useParams()
const newsID = newsId ? parseInt(newsId) : ''
const newsID = newsId ? parseInt(newsId) : '';
const className = news.id === newsID ? classes.activeNewsFlash : '';
const date = news.date == null ? '' : dateFormat(new Date(news.date.replace(/-/g, '/')), locale);
const handleLocationEditorOpen = () => setOpen(true);
Expand Down
44 changes: 23 additions & 21 deletions src/components/organisms/News.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { FC, useEffect, useRef } from 'react';
import { FC, useRef } from 'react';
import { Typography } from 'components/atoms';
import { Box, makeStyles } from '@material-ui/core';
import { useStore } from 'store/storeConfig';
Expand All @@ -8,6 +8,9 @@ import { observer } from 'mobx-react-lite';
import LocationSearchIndicator from 'components/molecules/LocationSearchIndicator';
import { IRouteProps } from 'models/Route';
import NewsFlashComp from 'components/molecules/NewsFlashComp';
import { useScrollObserver } from 'hooks/useScrollObserver.hooks';
import { Direction } from 'models/ScrollObserver.model';
import { combineRefs } from 'utils/element.util';

const useStyles = makeStyles({
container: {},
Expand All @@ -19,41 +22,40 @@ const useStyles = makeStyles({
},
});

<img src="" alt="" />;
interface InfiniteScrollProps {
onScroll: (direction: Direction) => void;
}

const News: FC = () => {
const News: FC<InfiniteScrollProps> = ({ onScroll }) => {
const store: RootStore = useStore();
const classes = useStyles();
const { gpsId, street, city, newsId = '' } = useParams<IRouteProps>();
const { newsFlashStore } = store;
const selectedItemRef = useRef<HTMLDivElement>(null);
const containerRef = useRef<HTMLDivElement>(null);

useEffect(() => {
if (newsId && newsFlashStore.newsFlashCollection.data.length > 0 && !newsFlashStore.newsFlashLoading) {
// Find selected item index
const itemIndex = newsFlashStore.newsFlashCollection.data.findIndex((item) => item.id.toString() === newsId);

if (itemIndex !== -1) {
setTimeout(() => {
selectedItemRef.current?.scrollIntoView({
behavior: 'smooth',
block: 'center',
});
}, 100);
}
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const { firstItemRef, lastItemRef, selectedItemRef } = useScrollObserver({
newsId,
onScroll,
containerRef,
newsData: newsFlashStore.newsFlashCollection.data,
newsLoading: newsFlashStore.newsFlashLoading,
});

return (
<div ref={containerRef} className={classes.newsFeed}>
{gpsId && <LocationSearchIndicator searchType={'gps'} />}
{street && city && <LocationSearchIndicator searchType={'cityAndStreet'} />}
{newsFlashStore.newsFlashCollection.data.length > 0 ? (
newsFlashStore.newsFlashCollection.data.map((news, index) => {
const isFirst = index === 0;
const isLast = index === newsFlashStore.newsFlashCollection.data.length - 1;
const selectedItem = news.id === +newsId ? selectedItemRef : undefined;

return (
<div key={news.id} ref={selectedItemRef}>
<div
key={news.id}
ref={combineRefs(isFirst ? firstItemRef : undefined, isLast ? lastItemRef : undefined, selectedItem)}
>
<NewsFlashComp news={news} />
</div>
);
Expand Down
20 changes: 10 additions & 10 deletions src/components/organisms/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ import { Typography, ErrorBoundary } from 'components/atoms';
import { observer } from 'mobx-react-lite';
import { useStore } from 'store/storeConfig';
import RootStore from 'store/root.store';
import { InfiniteScroll } from 'components/atoms';
import SideBarMap from 'components/molecules/SideBarMap';
import { useTranslation } from 'react-i18next';
import { Direction } from 'hooks/useScrollObserver.hooks';
import { Direction } from 'models/ScrollObserver.model';

interface IProps {}

Expand All @@ -36,19 +35,22 @@ const SideBar: FC<IProps> = () => {
const mapTitle = `${t('sideBar')}`;
const location = newsFlashStore.activeNewsFlashLocation;
const loading = newsFlashStore.newsFlashLoading;
const initialPageNumber = newsFlashStore.newsFlashInitialPageNumber;
const currentPageNumber = newsFlashStore.newsFlashPageNumber;
const lastPrevPage = newsFlashStore.newsFlashLastPrevPage;
const totalPages = newsFlashStore.newsFlashCollection.pagination.totalPages;

const fetchMoreNewsItems = useCallback(
(direction: Direction) => {
if (direction === Direction.PREV && initialPageNumber !== 1 && currentPageNumber > 1) {
if (loading) return;
if (direction === Direction.PREV && currentPageNumber > 1 && lastPrevPage > 1) {
newsFlashStore.filterNewsFlashCollection(direction);
return;
}
if (direction === Direction.NEXT && totalPages > currentPageNumber) {
newsFlashStore.filterNewsFlashCollection(direction);
} else if (totalPages > currentPageNumber) {
newsFlashStore.filterNewsFlashCollection(Direction.NEXT);
}
},
[currentPageNumber, initialPageNumber, newsFlashStore, totalPages],
[currentPageNumber, lastPrevPage, loading, newsFlashStore, totalPages],
);

return (
Expand All @@ -60,9 +62,7 @@ const SideBar: FC<IProps> = () => {
<NewsFlashFilterPanel />
</ErrorBoundary>
</Box>
<InfiniteScroll onScroll={fetchMoreNewsItems} loading={newsFlashStore.newsFlashLoading}>
<News />
</InfiniteScroll>
<News onScroll={fetchMoreNewsItems} />
</Box>
<Box borderTop={`1px solid ${silverSmokeColor}`} flexShrink={0} flexGrow={0} p={1}>
<Typography.Body4 children={mapTitle} />
Expand Down
Loading

0 comments on commit c12247f

Please sign in to comment.