From 4f36820d34ee57d8e1da929e54d82428b13924be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Antoni=20Nu=C3=B1ez=20Romeu?= Date: Mon, 6 Jul 2026 18:47:43 +0200 Subject: [PATCH] feat: save search history when sorting pharmacies by distance --- apps/frontend/src/views/PublicView.jsx | 30 ++++++++++++++++++++++++++ apps/frontend/src/views/SearchView.jsx | 30 ++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/apps/frontend/src/views/PublicView.jsx b/apps/frontend/src/views/PublicView.jsx index 1124e52..aa383c9 100644 --- a/apps/frontend/src/views/PublicView.jsx +++ b/apps/frontend/src/views/PublicView.jsx @@ -92,6 +92,21 @@ function PublicView({ setUserPosition({ lat: savedLat, lon: savedLon }); setPositionSource('profile'); setSortByDistance(true); + // Save to search history + try { + fetch('/api/search-history', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + credentials: 'include', + body: JSON.stringify({ + address: currentUser?.address, + latitude: savedLat, + longitude: savedLon, + }), + }); + } catch (err) { + console.error('Error saving search history:', err); + } return; } if (userPosition) { @@ -104,6 +119,21 @@ function PublicView({ setUserPosition(pos); setPositionSource('browser'); setSortByDistance(true); + // Save to search history + try { + fetch('/api/search-history', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + credentials: 'include', + body: JSON.stringify({ + address: 'Ubicación actual', + latitude: pos.lat, + longitude: pos.lon, + }), + }); + } catch (err) { + console.error('Error saving search history:', err); + } } catch (err) { console.error('[location] error', err); let msg; diff --git a/apps/frontend/src/views/SearchView.jsx b/apps/frontend/src/views/SearchView.jsx index e559fc2..6cd007e 100644 --- a/apps/frontend/src/views/SearchView.jsx +++ b/apps/frontend/src/views/SearchView.jsx @@ -120,6 +120,21 @@ function SearchView({ currentUser, onLoginRequest }) { setUserPosition({ lat: savedLat, lon: savedLon }); setPositionSource('profile'); setSortByDistance(true); + // Save to search history + try { + fetch('/api/search-history', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + credentials: 'include', + body: JSON.stringify({ + address: currentUser?.address, + latitude: savedLat, + longitude: savedLon, + }), + }); + } catch (err) { + console.error('Error saving search history:', err); + } return; } if (userPosition) { @@ -132,6 +147,21 @@ function SearchView({ currentUser, onLoginRequest }) { setUserPosition(pos); setPositionSource('browser'); setSortByDistance(true); + // Save to search history + try { + fetch('/api/search-history', { + method: 'POST', + headers: { 'Content-Type': 'application/json' }, + credentials: 'include', + body: JSON.stringify({ + address: 'Ubicación actual', + latitude: pos.lat, + longitude: pos.lon, + }), + }); + } catch (err) { + console.error('Error saving search history:', err); + } } catch (err) { let msg = 'No se pudo obtener tu ubicación'; if (err && typeof err.code === 'number') {