-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(notifications): enable auto page loading and sorting (#411)
- Loading branch information
Showing
7 changed files
with
231 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/components/pages/NotificationCenter/NotificationPager.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2024 BMW Group AG | ||
* Copyright (c) 2021, 2024 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an 'AS IS' BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
import { Box } from '@mui/material' | ||
import { CircleProgress } from '@catena-x/portal-shared-components' | ||
import { notificationFetchSelector, setPage } from 'features/notification/slice' | ||
import { useRef } from 'react' | ||
import { useDispatch, useSelector } from 'react-redux' | ||
import useOnScreen from 'utils/useOnScreen' | ||
|
||
export default function NotificationPager() { | ||
const fetchArgs = useSelector(notificationFetchSelector) | ||
const dispatch = useDispatch() | ||
const ref = useRef<HTMLDivElement>(null) | ||
const isVisible = useOnScreen(ref) | ||
|
||
const triggerLoad = () => { | ||
setTimeout(() => dispatch(setPage(fetchArgs.page + 1)), 700) | ||
return true | ||
} | ||
|
||
return ( | ||
<Box | ||
sx={{ | ||
width: '100%', | ||
display: 'flex', | ||
justifyContent: 'center', | ||
alignItems: 'center', | ||
minHeight: 100, | ||
}} | ||
> | ||
{' '} | ||
{isVisible && ( | ||
<CircleProgress | ||
size={40} | ||
step={1} | ||
interval={0.1} | ||
colorVariant={'primary'} | ||
variant={'indeterminate'} | ||
thickness={8} | ||
/> | ||
)} | ||
<div style={{ marginTop: 200 }} ref={ref}> | ||
{isVisible && triggerLoad()} | ||
</div> | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/******************************************************************************** | ||
* Copyright (c) 2021, 2023 BMW Group AG | ||
* Copyright (c) 2021, 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available at | ||
* https://www.apache.org/licenses/LICENSE-2.0. | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
********************************************************************************/ | ||
|
||
import { type RefObject, useEffect, useMemo, useState } from 'react' | ||
|
||
export default function useOnScreen(ref: RefObject<HTMLElement>) { | ||
const [isIntersecting, setIsIntersecting] = useState<boolean>(false) | ||
|
||
const observer = useMemo( | ||
() => | ||
new IntersectionObserver(([entry]) => { | ||
setIsIntersecting(entry.isIntersecting) | ||
}), | ||
[ref] | ||
) | ||
|
||
useEffect(() => { | ||
if (!ref.current) return | ||
observer.observe(ref.current) | ||
return () => { | ||
observer.disconnect() | ||
} | ||
}, []) | ||
|
||
return isIntersecting | ||
} |