-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
76a6d49
commit d79a01e
Showing
7 changed files
with
205 additions
and
4 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
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,16 @@ | ||
export function TailwindIndicator() { | ||
if (process.env.NODE_ENV === "production") return null; | ||
|
||
return ( | ||
<div className="fixed bottom-1 left-1 z-50 h-6 w-6 rounded-full bg-gray-800 flex justify-center items-center font-mono text-xs text-white"> | ||
<div className="block sm:hidden">xs</div> | ||
<div className="hidden sm:block md:hidden lg:hidden xl:hidden 2xl:hidden"> | ||
sm | ||
</div> | ||
<div className="hidden md:block lg:hidden xl:hidden 2xl:hidden">md</div> | ||
<div className="hidden lg:block xl:hidden 2xl:hidden">lg</div> | ||
<div className="hidden xl:block 2xl:hidden">xl</div> | ||
<div className="hidden 2xl:block">2xl</div> | ||
</div> | ||
); | ||
} |
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,40 @@ | ||
import * as React from "react"; | ||
|
||
interface Args extends IntersectionObserverInit { | ||
freezeOnceVisible?: boolean; | ||
} | ||
|
||
export function useIntersectionObserver( | ||
elementRef: React.RefObject<Element>, | ||
{ | ||
threshold = 0, | ||
root = null, | ||
rootMargin = "0%", | ||
freezeOnceVisible = false, | ||
}: Args | ||
): IntersectionObserverEntry | undefined { | ||
const [entry, setEntry] = React.useState<IntersectionObserverEntry>(); | ||
|
||
const frozen = entry?.isIntersecting && freezeOnceVisible; | ||
|
||
const updateEntry = ([entry]: IntersectionObserverEntry[]): void => { | ||
setEntry(entry); | ||
}; | ||
|
||
React.useEffect(() => { | ||
const node = elementRef?.current; // DOM Ref | ||
const hasIOSupport = !!window.IntersectionObserver; | ||
if (!hasIOSupport || frozen || !node) return; | ||
|
||
const observerParams = { threshold, root, rootMargin }; | ||
const observer = new IntersectionObserver(updateEntry, observerParams); | ||
console.log(node); | ||
observer.observe(node); | ||
|
||
return () => observer.disconnect(); | ||
|
||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [elementRef?.current, threshold, root, rootMargin, frozen]); | ||
|
||
return entry; | ||
} |
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,125 @@ | ||
import ExampleExplanation from "@/components/ExampleExplanation"; | ||
import { Button } from "@/components/ui/button"; | ||
import { | ||
GetDealsOffsetBasedDocument, | ||
useGetDealsOffsetBasedQuery, | ||
} from "@/generated/graphql"; | ||
import { useIntersectionObserver } from "@/hooks/useIntersectionObserver"; | ||
import { addApolloState, initializeApollo } from "@/utils/apolloClient"; | ||
import { NetworkStatus } from "@apollo/client"; | ||
import { useCallback, useEffect, useRef } from "react"; | ||
|
||
const EXPLANATION = { | ||
title: "Scroll Offset Pagination", | ||
description: "Načítání dat podle pozice na stránce.", | ||
}; | ||
|
||
export default function ScrollPaginationPage() { | ||
const { data, fetchMore, loading, error, networkStatus } = | ||
useGetDealsOffsetBasedQuery({ | ||
variables: { | ||
limit: 2, | ||
offset: 0, | ||
}, | ||
notifyOnNetworkStatusChange: true, | ||
}); | ||
|
||
const loadingMore = networkStatus === NetworkStatus.fetchMore; | ||
|
||
const observerRef = useRef(); | ||
|
||
const lastDealElementRef = useCallback((node) => { | ||
observerRef.current = node; | ||
}, []); | ||
|
||
const intersectionEntry = useIntersectionObserver(observerRef, { | ||
threshold: 0.8, | ||
freezeOnceVisible: false, | ||
}); | ||
|
||
useEffect(() => { | ||
if (intersectionEntry?.isIntersecting) { | ||
loadMore(); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [intersectionEntry?.isIntersecting]); | ||
|
||
const loadMore = () => { | ||
const currentLength = data.dealsOffsetBased.length || 0; | ||
fetchMore({ | ||
variables: { | ||
offset: currentLength, | ||
limit: 2, | ||
}, | ||
}); | ||
}; | ||
|
||
if (data?.dealsOffsetBased.length) { | ||
return ( | ||
<main className="max-w-5xl mx-auto px-3 h-screen"> | ||
<ExampleExplanation | ||
title={EXPLANATION.title} | ||
description={EXPLANATION.description} | ||
className="py-10" | ||
> | ||
<div className="py-1"> | ||
<h3 className="font-semibold">Poznámky</h3> | ||
<ul className="text-xs space-y-2"> | ||
<li> | ||
- v ideálním stavu by bylo potřeba ještě nastavit ať už se | ||
nevolá loadMore v případě, že už nejsou další data k dispozici | ||
</li> | ||
</ul> | ||
</div> | ||
</ExampleExplanation> | ||
<div className="py-10"> | ||
<div> | ||
<span>Počet načtených dealů: {data.dealsOffsetBased.length}</span> | ||
</div> | ||
<div className="py-10"> | ||
{data.dealsOffsetBased.map((deal, index) => ( | ||
<div | ||
key={deal.id} | ||
className="h-72 border p-2" | ||
ref={ | ||
index === data.dealsOffsetBased.length - 1 | ||
? lastDealElementRef | ||
: null | ||
} | ||
> | ||
<h1>{deal.title}</h1> | ||
</div> | ||
))} | ||
</div> | ||
{loadingMore && <div>Načítám další data</div>} | ||
</div> | ||
</main> | ||
); | ||
} | ||
|
||
if (loading) { | ||
return <div>loading</div>; | ||
} | ||
|
||
if (error) { | ||
return <div>Error</div>; | ||
} | ||
|
||
return <div>Nic tu není</div>; | ||
} | ||
|
||
export async function getServerSideProps() { | ||
const apolloClient = initializeApollo(); | ||
|
||
await apolloClient.query({ | ||
query: GetDealsOffsetBasedDocument, | ||
variables: { | ||
limit: 2, | ||
offset: 0, | ||
}, | ||
}); | ||
|
||
return addApolloState(apolloClient, { | ||
props: {}, | ||
}); | ||
} |