Skip to content

Commit e28ef11

Browse files
committed
user friendly error messages and removed result state unused
1 parent fcea8fa commit e28ef11

File tree

1 file changed

+13
-32
lines changed

1 file changed

+13
-32
lines changed

apps/app/src/hooks/useGeoValidation.ts

Lines changed: 13 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ import { useCallback, useEffect, useMemo, useState } from 'react';
22
import { validateWithinRadius } from '@/validation/validateGeolocation';
33
import { VALIDATION_RADIUS_M } from '@/constants/geolocation';
44

5-
type GeoResult = {
6-
isOk: boolean;
7-
distanceMeters: number;
8-
accuracyMeters: number | null;
9-
};
10-
115
export function useGeoValidation(options?: {
126
accuracyLimitMeters?: number;
137
geolocationOptions?: PositionOptions;
@@ -25,58 +19,45 @@ export function useGeoValidation(options?: {
2519

2620
const [isOk, setIsOk] = useState<boolean | null>(null);
2721
const [error, setError] = useState<string | null>(null);
28-
const [result, setResult] = useState<GeoResult | null>(null);
2922

30-
const validate = useCallback(() => {
23+
const validateGeolocation = useCallback(() => {
3124
setError(null);
3225

3326
if (typeof window === 'undefined' || !('geolocation' in navigator)) {
3427
setIsOk(false);
35-
setError('Geolocation is not available in this environment.');
28+
setError('Geolokacija nije uključena ili je nedostupna.');
3629
return;
3730
}
3831

39-
navigator.geolocation.getCurrentPosition(
32+
void navigator.geolocation.getCurrentPosition(
4033
(pos) => {
4134
const accuracy = pos.coords.accuracy ?? null;
4235

4336
// Reject low-accuracy fixes first
4437
if (!accuracy || accuracy > accuracyLimit) {
45-
const msg = `Location accuracy too low (${accuracy ? Math.round(accuracy) : '?'}m).`;
38+
const msg = `Preciznost lokacije je preniska.`;
4639
setIsOk(false);
4740
setError(msg);
48-
setResult(
49-
!accuracy
50-
? null
51-
: { isOk: false, distanceMeters: NaN, accuracyMeters: accuracy },
52-
);
5341
return;
5442
}
5543

56-
const { isOk, distanceMeters } = validateWithinRadius(pos.coords);
57-
58-
const payload: GeoResult = {
59-
isOk,
60-
distanceMeters,
61-
accuracyMeters: accuracy,
62-
};
63-
64-
setResult(payload);
65-
setIsOk(isOk);
66-
console.log('Geo validation result:', payload);
44+
const result = validateWithinRadius(pos.coords);
45+
// for debugging:
46+
// console.log('Geolocation validation result:', result);
47+
setIsOk(result.isOk);
6748
},
68-
(err) => {
49+
() => {
6950
setIsOk(false);
70-
setError(err.message);
51+
setError(`Greška pri dohvaćanju lokacije. Pokušajte ponovno.`);
7152
},
7253
geoOptions,
7354
);
7455
}, [accuracyLimit, geoOptions]);
7556

7657
// Run validation once on mount
7758
useEffect(() => {
78-
validate();
79-
}, [validate]);
59+
validateGeolocation();
60+
}, [validateGeolocation]);
8061

81-
return { validate, isOk, error, result };
62+
return { validateGeolocation, isOk, error };
8263
}

0 commit comments

Comments
 (0)