Skip to content

Commit 298624e

Browse files
committed
Use new point type
1 parent 87b5bbb commit 298624e

File tree

25 files changed

+83
-148
lines changed

25 files changed

+83
-148
lines changed

src/app/[locale]/(app)/explore/places/[placeId]/_components/place-details.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ export const PlaceDetails: FC<{
9999
features={place.features}
100100
externalLinks={place.externalLinks}
101101
googleMapsId={place.googleMapsId}
102-
geoUri={`geo:${place.location.lat},${place.location.lng}`}
102+
geoUri={`geo:${place.location.x},${place.location.y}`}
103103
className="mt-4"
104104
/>
105105

src/app/[locale]/(app)/explore/places/[placeId]/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { unstable_setRequestLocale } from 'next-intl/server'
33
import { notFound } from 'next/navigation'
44
import { type FC } from 'react'
55
import { makeImageUrl } from '~/helpers/images'
6+
import { toLatLng } from '~/helpers/spatial-data/point'
67
import {
78
LocaleRouteParams,
89
locales,
@@ -98,7 +99,7 @@ const PlacePage: FC<LocaleRouteParams> = async ({ params }) => {
9899
return (
99100
<>
100101
<OverrideMainMap
101-
center={place.location}
102+
center={toLatLng(place.location)}
102103
zoom={18}
103104
veryEmphasizedMarkers={veryEmphasizedMarkers}
104105
/>

src/app/[locale]/(app)/missions/_components/place-prevew-modal.tsx

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,10 @@ export const PlacePreviewModal: FC<
3232
const t = useTranslations('missions')
3333
const isFar =
3434
place &&
35-
(place.location.lng < 3.170467 ||
36-
place.location.lng > 3.24022 ||
37-
place.location.lat < 41.920697 ||
38-
place.location.lat > 41.984129)
35+
(place.location.x < 3.170467 ||
36+
place.location.x > 3.24022 ||
37+
place.location.y < 41.920697 ||
38+
place.location.y > 41.984129)
3939

4040
return (
4141
<Modal isOpen={isOpen} onOpenChange={onOpenChange}>
@@ -70,8 +70,8 @@ export const PlacePreviewModal: FC<
7070
markers={[
7171
{
7272
placeId: place.id,
73-
lat: place.location.lat,
74-
lng: place.location.lng,
73+
lat: place.location.y,
74+
lng: place.location.x,
7575
icon: place.mainCategory.icon,
7676
color: 'red',
7777
size: 'sm',

src/app/[locale]/(app)/missions/_components/verificate-button.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,14 @@ import { useDisclosure } from '@nextui-org/use-disclosure'
55
import { IconDiscountCheckFilled } from '@tabler/icons-react'
66
import { useTranslations } from 'next-intl'
77
import { FC } from 'react'
8-
import type { MapPoint } from '~/helpers/spatial-data/point'
98
import { VerificationRequirements } from '~/server/db/constants/verifications'
109
import {
1110
OnVerificate,
1211
VerificatePlaceVisitModal,
1312
} from './verificate-place-visit-modal'
1413

1514
export type VerificateButtonProps = {
16-
expectedLocation: MapPoint
15+
expectedLocation: { x: number; y: number }
1716
placeId: number
1817
isAlreadyVisited: boolean
1918
isAlreadyVerified: boolean

src/app/[locale]/(app)/missions/_components/verificate-place-visit-modal.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import { AlertBox } from '~/components/generic/alert-box'
2727
import { DividerWithText } from '~/components/generic/divider-with-text'
2828
import { ExplanationCard } from '~/components/generic/explanation-card'
2929
import { LinkButton } from '~/components/links/link-button'
30-
import type { MapPoint } from '~/helpers/spatial-data/point'
3130
import { useRouter } from '~/navigation'
3231
import {
3332
VerificationRequirements,
@@ -39,15 +38,15 @@ import { useLocationValidator } from '../_hooks/useLocationValidator'
3938
export type OnVerificate = (
4039
hasBeenVerified: boolean,
4140
verificationData: {
42-
location: MapPoint | null
41+
location: { x: number; y: number } | null
4342
accuracy: number | null
4443
}
4544
) => Promise<void> | void
4645

4746
export const VerificatePlaceVisitModal: FC<
4847
Omit<ModalProps, 'children'> & {
4948
onVerificate?: OnVerificate
50-
expectedLocation: MapPoint
49+
expectedLocation: { x: number; y: number }
5150
placeId: number
5251
isAlreadyVisited: boolean
5352
verificationRequirements: VerificationRequirements | null

src/app/[locale]/(app)/missions/_hooks/useLocationValidator.ts

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import haversine from 'haversine-distance'
22
import { useCallback, useEffect, useState } from 'react'
3-
import type { MapPoint } from '~/helpers/spatial-data/point'
3+
import { toLatLng } from '~/helpers/spatial-data/point'
44
import { useDevicePermissions } from '~/helpers/useDevicePermissions'
55

66
/** In meters */
@@ -19,7 +19,7 @@ type ErrorCodes =
1919
| 'permission-not-granted-yet'
2020

2121
export function useLocationValidator(
22-
expectedLocation: MapPoint,
22+
expectedLocation: { x: number; y: number },
2323
maxLocationDistance?: number | null
2424
) {
2525
const [deviceLocationError, setDeviceLocationError] =
@@ -46,18 +46,21 @@ export function useLocationValidator(
4646
}, [locationPermission])
4747

4848
const validateLocation = useCallback<
49-
() => Promise<{ location: MapPoint; accuracy: number } | null>
49+
() => Promise<{
50+
location: { x: number; y: number }
51+
accuracy: number
52+
} | null>
5053
>(async () => {
5154
setLoadingDeviceLocation(true)
5255

5356
const { error, data } = await new Promise<
5457
| {
5558
error: null
56-
data: { location: MapPoint; accuracy: number }
59+
data: { location: { x: number; y: number }; accuracy: number }
5760
}
5861
| {
5962
error: ErrorCodes
60-
data: { location: MapPoint; accuracy: number } | null
63+
data: { location: { x: number; y: number }; accuracy: number } | null
6164
}
6265
>((resolve) => {
6366
if (!('geolocation' in navigator)) {
@@ -71,15 +74,15 @@ export function useLocationValidator(
7174
(position) => {
7275
const deviceGeolocation = {
7376
location: {
74-
lat: position.coords.latitude,
75-
lng: position.coords.longitude,
77+
x: position.coords.latitude,
78+
y: position.coords.longitude,
7679
},
7780
accuracy: position.coords.accuracy,
7881
} as const
7982

8083
const distance = haversine(
81-
deviceGeolocation.location,
82-
expectedLocation
84+
toLatLng(deviceGeolocation.location),
85+
toLatLng(expectedLocation)
8386
)
8487
if (distance > (maxLocationDistance ?? MAX_DISTANCE_TO_PLACE)) {
8588
return resolve({

src/app/[locale]/admin/places/_components/place-form.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ export const PlaceForm: FC<{
6060
description: place.description ?? undefined,
6161
mainCategory: place.mainCategory.id,
6262
categories: place.categories.map((c) => c.category.id).join(','),
63-
location: `${place.location.lat}, ${place.location.lng}`,
63+
location: `${place.location.x}, ${place.location.y}`,
6464
importance: place.importance,
6565
mainImageId: place.mainImage?.id ?? undefined,
6666
content: place.content ?? undefined,

src/components/features/features-block.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ export const FeaturesBlock: FC<{
197197
}}
198198
/>
199199
)}
200-
{geoUri && (
200+
{!!geoUri && (
201201
<LinkFeatureItem
202202
link={{
203203
url: geoUri,

src/components/map/map-elements/map-marker.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import moize from 'moize'
22
import { FC, memo, useMemo, useState } from 'react'
33
import { useMap, useMapEvent } from 'react-leaflet'
4-
import { MapPoint } from '~/helpers/spatial-data/point'
54
import { useRouter } from '~/navigation'
65
import { NextMarker } from '../leaflet-components/next-js-ready/marker'
76
import {
@@ -16,8 +15,8 @@ type MapMarkerSize =
1615
| 'not-emphasized'
1716
export type MapMarkerInvariable = {
1817
placeId: number
19-
lat: MapPoint['lat']
20-
lng: MapPoint['lng']
18+
lat: number
19+
lng: number
2120
url?: string
2221
icon: PlaceMarkerLeafletIconProps['icon']
2322
color: PlaceMarkerLeafletIconProps['color']

src/components/map/map-elements/map-point-selector.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ export const MapPointSelector: FC<{
4949
value={value}
5050
label={label}
5151
className="shadow-lg"
52-
placeholder="Lat, Lng"
52+
placeholder="Lng, Lat"
5353
/>
5454
{reset && (
5555
<Button
@@ -67,7 +67,7 @@ export const MapPointSelector: FC<{
6767
className="h-full w-full rounded-md"
6868
onValueChange={(newValue) => {
6969
const newValueString = newValue
70-
? `${newValue.lat}, ${newValue.lng}`
70+
? `${newValue.lng}, ${newValue.lat}`
7171
: undefined
7272

7373
onChange({ target: { value: newValueString } })

src/components/providers/main-map-provider.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,8 @@ export const MainMapProvider: FC<PropsWithChildren> = memo(({ children }) => {
5454
const { data: places } = trpc.map.getAllPlaces.useQuery()
5555
const markers = places?.map((place) => ({
5656
placeId: place.id,
57-
lat: place.location.lat,
58-
lng: place.location.lng,
57+
lat: place.location.y,
58+
lng: place.location.x,
5959
icon: place.mainCategory.icon,
6060
color: place.mainCategory.color,
6161
url: `/explore/places/${place.id}`,

src/helpers/spatial-data/point.ts

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -84,16 +84,6 @@ export function getPoint(
8484
return null
8585
}
8686

87-
export function calculateLocation<
88-
L extends PointString | null | undefined,
89-
P extends { location: L },
90-
>(place: P) {
91-
return {
92-
...place,
93-
location: getPoint(place.location),
94-
}
95-
}
96-
97-
export function pointToString(value: MapPoint): PointString {
98-
return `POINT(${value.lng} ${value.lat})`
87+
export function toLatLng(point: { x: number; y: number }) {
88+
return { lat: point.y, lng: point.x }
9989
}

src/schemas/places.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,13 @@ export const createPlaceSchema = z.object({
4141
.string()
4242
.min(3, 'Required')
4343
.transform((value) => {
44-
const [lat, lng] = value.split(',')
45-
return { lat: Number(lat), lng: Number(lng) }
44+
const [x, y] = value.split(',')
45+
return { x: Number(x), y: Number(y) }
4646
})
4747
.pipe(
4848
z.object({
49-
lat: z.number(),
50-
lng: z.number(),
49+
x: z.number(),
50+
y: z.number(),
5151
})
5252
),
5353
importance: z.coerce.number().gt(0).lt(MAX_IMPORTANCE).optional().nullable(),

src/schemas/verifications.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ export const verificateVisitSchema = z.object({
55
placeId: numericIdSchema,
66
deviceLocation: z
77
.object({
8-
lat: z.number(),
9-
lng: z.number(),
8+
x: z.number(),
9+
y: z.number(),
1010
})
1111
.nullable(),
1212
deviceLocationAccuracy: z.number().nullable(),

src/server/api/router/admin/places.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import 'server-only'
22

33
import { and, asc, eq, isNull, like, or, sql } from 'drizzle-orm'
4-
import { calculateLocation, pointToString } from '~/helpers/spatial-data/point'
54
import {
65
createPlaceSchema,
76
editPlaceSchema,
@@ -18,7 +17,6 @@ import {
1817
placesToPlaceCategories,
1918
} from '~/server/db/schema'
2019
import { ascNullsEnd } from '~/server/helpers/order-by'
21-
import { selectPoint } from '~/server/helpers/spatial-data/point'
2220
import {
2321
flattenTranslationsOnExecute,
2422
withTranslations,
@@ -143,9 +141,7 @@ const getPlace = flattenTranslationsOnExecute(
143141
content: true,
144142
importance: true,
145143
googleMapsId: true,
146-
},
147-
extras: {
148-
location: selectPoint('location', places.location),
144+
location: true,
149145
},
150146
where: (place, { eq }) =>
151147
eq(place.id, sql`${sql.placeholder('id')}::integer`),
@@ -207,7 +203,8 @@ export const placesAdminRouter = router({
207203
locale: input.locale,
208204
id: input.id,
209205
})
210-
return result ? calculateLocation(result) : undefined
206+
if (!result) return undefined
207+
return result
211208
}),
212209
listCategories: adminProcedure
213210
.input(listCategoriesSchema)
@@ -234,7 +231,7 @@ export const placesAdminRouter = router({
234231
googleMapsId: input.googleMapsId,
235232
mainCategoryId: input.mainCategory,
236233
mainImageId: input.mainImageId,
237-
location: pointToString(input.location),
234+
location: input.location,
238235
importance: input.importance,
239236
content: input.content,
240237
verificationRequirementsId: 1,
@@ -293,7 +290,7 @@ export const placesAdminRouter = router({
293290
googleMapsId: input.googleMapsId,
294291
mainCategoryId: input.mainCategory,
295292
mainImageId: input.mainImageId,
296-
location: pointToString(input.location),
293+
location: input.location,
297294
importance: input.importance,
298295
content: input.content,
299296
featuresId: featuresId,

src/server/api/router/map.ts

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import 'server-only'
22

33
import { calculatePath } from '~/helpers/spatial-data/multi-line'
4-
import { calculateLocation } from '~/helpers/spatial-data/point'
54
import { db } from '~/server/db/db'
6-
import { places, routes } from '~/server/db/schema'
5+
import { routes } from '~/server/db/schema'
76
import { selectMultiLine } from '~/server/helpers/spatial-data/multi-line'
8-
import { selectPoint } from '~/server/helpers/spatial-data/point'
97
import { publicProcedure, router } from '~/server/trpc'
108

119
const getAllPlacesForMap = db.query.places
@@ -14,9 +12,7 @@ const getAllPlacesForMap = db.query.places
1412
id: true,
1513
name: true,
1614
importance: true,
17-
},
18-
extras: {
19-
location: selectPoint('location', places.location),
15+
location: true,
2016
},
2117
with: {
2218
mainCategory: {
@@ -54,7 +50,7 @@ const getAllRoutesForMap = db.query.routes
5450

5551
export const mapRouter = router({
5652
getAllPlaces: publicProcedure.query(async () => {
57-
return (await getAllPlacesForMap.execute()).map(calculateLocation)
53+
return await getAllPlacesForMap.execute()
5854
}),
5955
getAllRoutes: publicProcedure.query(async () => {
6056
return (await getAllRoutesForMap.execute()).map(calculatePath)

0 commit comments

Comments
 (0)