Browse Source

refactor: clean up code formatting and improve scroll detection logic in DuaComponent

master
sina_sajjadi 3 weeks ago
parent
commit
af3417b0d5
  1. 23
      src/pages/duas/[slug].tsx

23
src/pages/duas/[slug].tsx

@ -85,15 +85,13 @@ const DuaComponent: React.FC<DuaComponentProps> = ({
setDuaParts((prev) => [...prev, ...duaResponse.data.results]); setDuaParts((prev) => [...prev, ...duaResponse.data.results]);
console.log(duaResponse); console.log(duaResponse);
getAudio(id); getAudio(id);
} catch (error) { } catch (error) {
router.replace("/404")
router.replace("/404");
console.log("Error fetching Dua parts:", error); console.log("Error fetching Dua parts:", error);
} finally { } finally {
setLoading(false); setLoading(false);
setFetching(false); // Reset fetching state after fetching is done setFetching(false); // Reset fetching state after fetching is done
} }
}, },
[slug, fetching, duaParts, getAudio] [slug, fetching, duaParts, getAudio]
); // Dependencies for fetchData ); // Dependencies for fetchData
@ -224,29 +222,30 @@ const DuaComponent: React.FC<DuaComponentProps> = ({
} }
}, [recitingPart, duaParts]); }, [recitingPart, duaParts]);
const handleScroll = useCallback( const handleScroll = useCallback(
(e: React.UIEvent<HTMLDivElement>) => { (e: React.UIEvent<HTMLDivElement>) => {
const target = e.currentTarget; const target = e.currentTarget;
const isBottom =
target.scrollHeight - target.scrollTop === target.clientHeight;
const isNearBottom =
target.scrollHeight - target.scrollTop - target.clientHeight <= 900; // Trigger when near bottom (100px from the bottom)
if (isBottom && !fetching) {
if (isNearBottom && !fetching) {
// Only fetch when not already fetching // Only fetch when not already fetching
fetchData(true); fetchData(true);
} }
console.log(target.scrollHeight, target.scrollTop + 100, target.clientHeight);
setScrollPosition(target.scrollTop); // Update scroll position setScrollPosition(target.scrollTop); // Update scroll position
}, },
[fetching] [fetching]
); );
console.log(duaParts[0]?.seo_field); console.log(duaParts[0]?.seo_field);
if (!slug) { if (!slug) {
return null; // Handling the case where slug is not available return null; // Handling the case where slug is not available
} }
const title = duaParts[0]?.seo_field?.title // Title derived from slug
const title = duaParts[0]?.seo_field?.title; // Title derived from slug
const description = duaParts[0]?.seo_field?.description; // You can customize this further const description = duaParts[0]?.seo_field?.description; // You can customize this further
const keywords = "dua, islam, prayer, supplication, dua parts"; // Keywords for SEO const keywords = "dua, islam, prayer, supplication, dua parts"; // Keywords for SEO
@ -294,7 +293,11 @@ const DuaComponent: React.FC<DuaComponentProps> = ({
padding: "1px", padding: "1px",
}} }}
> >
<div className={`p-3 rounded-3xl ${recitingPart === item ? "bg-[#F9F2F1]" :"bg-white" }`}>
<div
className={`p-3 rounded-3xl ${
recitingPart === item ? "bg-[#F9F2F1]" : "bg-white"
}`}
>
{item.text && ( {item.text && (
<div <div
className={`mb-4 text-right ${ className={`mb-4 text-right ${

Loading…
Cancel
Save