From d6bfd1000164da3e7d92eab90b2a56248c0b4970 Mon Sep 17 00:00:00 2001 From: cjeongmin Date: Thu, 30 May 2024 17:53:22 +0900 Subject: [PATCH] fix: Change the display based on location permission --- src/app/pages/main-page.tsx | 50 ++++++++++++++++------ src/app/pages/mobile/mobile-main-page.tsx | 52 ++++++++++++++++------- 2 files changed, 74 insertions(+), 28 deletions(-) diff --git a/src/app/pages/main-page.tsx b/src/app/pages/main-page.tsx index c10d472..71e73be 100644 --- a/src/app/pages/main-page.tsx +++ b/src/app/pages/main-page.tsx @@ -144,6 +144,8 @@ export function MainPage() { const router = useRouter(); + const [permission, setPermission] = useState(); + const handleScrollRight = () => { if (scrollRef.current !== null) { scrollRef.current.scrollBy({ left: 300, behavior: 'smooth' }); @@ -156,6 +158,22 @@ export function MainPage() { } }; + useEffect(() => { + (async () => { + try { + const permissionStatus = await navigator.permissions.query({ + name: 'geolocation', + }); + + permissionStatus.onchange = () => { + setPermission(permissionStatus.state); + }; + } catch (error) { + console.error('Error checking geolocation permission:', error); + } + })(); + }, []); + useEffect(() => { getGeolocation({ onSuccess: position => { @@ -257,21 +275,27 @@ export function MainPage() { }; }, [createdMarkers, map]); + const renderIndicator = () => { + if (permission != null && permission !== 'granted') + return

(위치 권한이 필요합니다)

; + if (map == null) + return ( + + ); + return <>; + }; + return ( - - {map == null && ( - - )} - + {renderIndicator()} {auth?.user?.name}님의 추천 메이트 diff --git a/src/app/pages/mobile/mobile-main-page.tsx b/src/app/pages/mobile/mobile-main-page.tsx index 356b922..dca7bba 100644 --- a/src/app/pages/mobile/mobile-main-page.tsx +++ b/src/app/pages/mobile/mobile-main-page.tsx @@ -149,6 +149,8 @@ export function MobileMainPage() { const router = useRouter(); + const [permission, setPermission] = useState(); + useEffect(() => { getGeolocation({ onSuccess: position => { @@ -171,6 +173,22 @@ export function MobileMainPage() { }); }, []); + useEffect(() => { + (async () => { + try { + const permissionStatus = await navigator.permissions.query({ + name: 'geolocation', + }); + + permissionStatus.onchange = () => { + setPermission(permissionStatus.state); + }; + } catch (error) { + console.error('Error checking geolocation permission:', error); + } + })(); + }, []); + const [createdMarkers, setCreatedMarkers] = useState([]); useEffect(() => { @@ -250,23 +268,27 @@ export function MobileMainPage() { }; }, [createdMarkers, map]); + const renderIndicator = () => { + if (permission != null && permission !== 'granted') + return

(위치 권한이 필요합니다)

; + if (map == null) + return ( + + ); + return <>; + }; + return ( - - {map == null && ( - <> - - - )} - + {renderIndicator()}

{auth?.user?.name}님의 추천 메이트