Missing files from commit
This commit is contained in:
@@ -3,7 +3,7 @@ import SearchBar from '../components/SearchBar';
|
||||
import MedicineResults from '../components/MedicineResults';
|
||||
import PharmacyList from '../components/PharmacyList';
|
||||
import PharmacyMap from '../components/PharmacyMap';
|
||||
import { haversineKm, getUserPosition } from '../utils/geo';
|
||||
import { haversineKm, getUserPosition, hasCachedPosition } from '../utils/geo';
|
||||
import './SearchView.css';
|
||||
|
||||
const suggestions = [
|
||||
@@ -26,6 +26,15 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '' }) {
|
||||
const [locationError, setLocationError] = useState('');
|
||||
const [recentSearches, setRecentSearches] = useState([]);
|
||||
|
||||
// Precache position on mount — makes first "sort by distance" nearly instant
|
||||
useEffect(() => {
|
||||
if (hasCachedPosition()) {
|
||||
getUserPosition().then(pos => {
|
||||
setUserPosition(pos);
|
||||
}).catch(() => {});
|
||||
}
|
||||
}, []);
|
||||
|
||||
// Fetch recent searches when user is logged in
|
||||
useEffect(() => {
|
||||
if (!currentUser) {
|
||||
@@ -59,7 +68,7 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '' }) {
|
||||
timestamp: Date.now(),
|
||||
},
|
||||
...filtered,
|
||||
].slice(0, 10);
|
||||
].slice(0, 5);
|
||||
});
|
||||
}, [currentUser]);
|
||||
|
||||
@@ -139,6 +148,7 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '' }) {
|
||||
}
|
||||
if (userPosition) {
|
||||
setSortByDistance(true);
|
||||
setPositionSource('cached');
|
||||
return;
|
||||
}
|
||||
setLocating(true);
|
||||
@@ -165,9 +175,9 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '' }) {
|
||||
} catch (err) {
|
||||
let msg = 'No se pudo obtener tu ubicación';
|
||||
if (err && typeof err.code === 'number') {
|
||||
if (err.code === 1) msg = 'Permiso de ubicación denegado';
|
||||
else if (err.code === 2) msg = 'Ubicación no disponible';
|
||||
else if (err.code === 3) msg = 'La solicitud de ubicación expiró';
|
||||
if (err.code === 1) msg = 'Permiso de ubicación denegado. Permite el acceso a la ubicación en tu navegador.';
|
||||
else if (err.code === 2) msg = 'Ubicación no disponible. Verifica que el GPS esté activado.';
|
||||
else if (err.code === 3) msg = 'La ubicación tardó demasiado. Intenta de nuevo o verifica tu conexión.';
|
||||
}
|
||||
setLocationError(msg);
|
||||
} finally {
|
||||
@@ -316,8 +326,16 @@ function SearchView({ currentUser, onLoginRequest, initialQuery = '' }) {
|
||||
{sortByDistance && positionSource === 'profile' && (
|
||||
<span className="location-source">Usando tu dirección guardada</span>
|
||||
)}
|
||||
{sortByDistance && positionSource === 'cached' && (
|
||||
<span className="location-source">Usando ubicación reciente</span>
|
||||
)}
|
||||
{locationError && (
|
||||
<span className="location-error">{locationError}</span>
|
||||
<span className="location-error">
|
||||
{locationError}
|
||||
<button className="retry-location-btn" onClick={handleSortByDistance}>
|
||||
Reintentar
|
||||
</button>
|
||||
</span>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
Reference in New Issue
Block a user