From 8849c819169dffe9b3f196ee6d93ae41b5c07607 Mon Sep 17 00:00:00 2001 From: balaharisankar Date: Sat, 10 Aug 2024 16:44:06 +0530 Subject: [PATCH] Scroll to top added --- frontend/src/pages/Home.js | 86 ++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 32 deletions(-) diff --git a/frontend/src/pages/Home.js b/frontend/src/pages/Home.js index 2b7a65a..49d38d0 100644 --- a/frontend/src/pages/Home.js +++ b/frontend/src/pages/Home.js @@ -1,49 +1,71 @@ -import React, { useEffect } from 'react' +import React, { useEffect, useState } from 'react' import NavBar from '../components/HomeComponents/NavBar' import Description from '../components/HomeComponents/Description' import Service from './Service' -import { useState } from 'react' import LoadingPage from './LoadingPage' +import { TiArrowUpThick } from "react-icons/ti"; import { useNavigate } from 'react-router-dom' export default function Home() { - const navigate=useNavigate(); - const [isLoading,setIsLoading]=useState(true); - - useEffect(()=>{ - - const checkIfLoggedIn=async()=>{ - const cookie=localStorage.getItem('jwt'); - const response=await fetch(`${process.env.REACT_APP_API_URL}/api/v1/users/protect`,{ - method:'post', - headers:{ - 'Content-type':'application/json', - 'Authorization':`Bearer ${cookie}` - } - }) - - const data=await response.json(); - if(data.status==='success') - { - navigate('/home/message',{replace:true}) + const [visible, setVisible] = useState(false) + const toggleVisible = () => { + const scrolled = document.documentElement.scrollTop; + if (scrolled > 300) { + setVisible(true) } - else{ - setIsLoading(false); + else if (scrolled <= 300) { + setVisible(false) } + }; + + const scrollToTop = () => { + window.scrollTo({ + top: 0, + behavior: 'smooth' + }); + }; + + window.addEventListener('scroll', toggleVisible); + + const navigate = useNavigate(); + const [isLoading, setIsLoading] = useState(true); + + useEffect(() => { - } + const checkIfLoggedIn = async () => { + const cookie = localStorage.getItem('jwt'); + const response = await fetch(`${process.env.REACT_APP_API_URL}/api/v1/users/protect`, { + method: 'post', + headers: { + 'Content-type': 'application/json', + 'Authorization': `Bearer ${cookie}` + } + }) + + const data = await response.json(); + if (data.status === 'success') { + navigate('/home/message', { replace: true }) + } + else { + setIsLoading(false); + } + + } checkIfLoggedIn() - },[navigate]) + }, [navigate]) return ( -
- {isLoading&&} - {!isLoading&&(<>
- +
+ {isLoading && } + {!isLoading && (<>
+ -
- )} -
+
+ )} + +
) }