konulu-konum/src/components/LockedContent.tsx

95 lines
3.0 KiB
TypeScript
Raw Normal View History

2024-01-17 21:50:12 +00:00
import { Button } from "@/components/ui/button"
import { Card, CardContent } from "@/components/ui/card"
import distance from "@/utils/distance"
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
import { LockClosedIcon, LockOpen1Icon } from "@radix-ui/react-icons"
import { useEffect, useState } from "react"
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
import "../styles/locked-content.css"
2024-01-16 14:57:13 +00:00
const LocationButton = () => {
2024-01-17 21:50:12 +00:00
const [atTarget, setAtTarget] = useState(false)
const [contentVisible, setContentVisible] = useState(false)
const [hasPermission, setHasPermission] = useState(false)
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
const startWatchingLocation = () => {
setHasPermission(true)
navigator.geolocation.watchPosition(
(position: GeolocationPosition) => {
const pos = {
lat: position.coords.latitude,
lng: position.coords.longitude
}
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
const totalDistanceInKM = distance(pos.lat, pos.lng, pos.lat, pos.lng).toFixed(0)
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
if (totalDistanceInKM === "0") {
setAtTarget(true)
}
},
() => null,
{ enableHighAccuracy: true, maximumAge: 30000, timeout: 27000 }
)
}
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
useEffect(() => {
navigator.permissions.query({ name: "geolocation" }).then(permissionStatus => {
if (permissionStatus.state === "granted") {
setHasPermission(true)
startWatchingLocation()
}
})
}, [])
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
if (contentVisible) {
return <div className='module-unlocked w-full h-[450px] p-4'></div>
2024-01-16 14:57:13 +00:00
} else {
return (
2024-01-17 21:50:12 +00:00
<div className='module w-full h-[450px] p-4'>
{atTarget ? (
<div className='module-inside flex flex-col justify-center items-center gap-2'>
<div>
<Button size={"lg"} onClick={() => setContentVisible(true)}>
<LockOpen1Icon className='mr-2 h-4 w-4' />
2024-01-16 14:57:13 +00:00
İçeriğin Kilidi ıldı
2024-01-17 21:50:12 +00:00
</Button>
</div>
<Card className='p-2'>
<CardContent className='pb-0 text-center'>İçeriği görmek için butona bas!</CardContent>
</Card>
2024-01-16 14:57:13 +00:00
</div>
2024-01-17 21:50:12 +00:00
) : (
<div className='module-inside flex flex-col justify-center items-center gap-4'>
<Button size={"lg"}>
<LockClosedIcon className='mr-2 h-4 w-4' /> İçerik Kilitli
</Button>
<Card className='p-2'>
{hasPermission ? (
<CardContent className='pb-0 text-center'>İçeriği görmek için konuma gitmelisin!</CardContent>
) : (
<div className='flex flex-col gap-2'>
<CardContent className='pb-0 text-center'>
Ne kadar yaklaştığını görmek için aşağıdaki butona bas.
</CardContent>
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
<Button
size={"sm"}
className='bg-green-700 hover:bg-green-600'
onClick={() => startWatchingLocation()}
>
Konum İzni Ver
</Button>
</div>
)}
</Card>
</div>
)}
2024-01-16 14:57:13 +00:00
</div>
2024-01-17 21:50:12 +00:00
)
2024-01-16 14:57:13 +00:00
}
2024-01-17 21:50:12 +00:00
}
2024-01-16 14:57:13 +00:00
2024-01-17 21:50:12 +00:00
export default LocationButton