Skip to content

Commit 4e7b033

Browse files
authored
Merge pull request #5330 from HSLdevcom/search-fixes
Search fixes
2 parents 8307e02 + dba5a7c commit 4e7b033

8 files changed

Lines changed: 65 additions & 193 deletions

File tree

app/component/IndexPage.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,9 @@ class IndexPage extends React.Component {
204204
});
205205

206206
if (item.type === 'FutureRoute') {
207-
router.push(createUrl(item));
207+
router.push(
208+
createUrl(item, { itinerarySummaryPrefix: PREFIX_ITINERARY_SUMMARY }),
209+
);
208210
} else if (id === 'origin') {
209211
executeAction(storeOrigin, item);
210212
} else {

app/component/itinerary/ItineraryPage.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -826,13 +826,13 @@ export default function ItineraryPage(props, context) {
826826
const itinerarySearch = {
827827
origin: {
828828
address: originArray[0],
829-
...parseLatLon(originArray[1]),
829+
coordinates: { ...parseLatLon(originArray[1]) },
830830
},
831831
destination: {
832832
address: destinationArray[0],
833-
...parseLatLon(destinationArray[1]),
833+
coordinates: { ...parseLatLon(destinationArray[1]) },
834834
},
835-
query,
835+
...query,
836836
};
837837
executeAction(saveFutureRoute, itinerarySearch);
838838
}

app/store/FutureRouteStore.js

Lines changed: 5 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import Store from 'fluxible/addons/BaseStore';
44
import { addFutureRoute } from '@digitransit-store/digitransit-store-future-route';
55
import { getFutureRoutesStorage, setFutureRoutesStorage } from './localStorage';
6-
import { PREFIX_ITINERARY_SUMMARY } from '../util/path';
76

87
class FutureRouteStore extends Store {
98
static storeName = 'FutureRouteStore';
@@ -24,28 +23,11 @@ class FutureRouteStore extends Store {
2423
}
2524

2625
saveFutureRoute(itinSearch) {
27-
const { origin, destination, query } = itinSearch;
28-
const route = {
29-
origin: {
30-
address: origin.address,
31-
coordinates: {
32-
lat: origin.lat,
33-
lon: origin.lon,
34-
},
35-
},
36-
destination: {
37-
address: destination.address,
38-
coordinates: {
39-
lat: destination.lat,
40-
lon: destination.lon,
41-
},
42-
},
43-
arriveBy: query.arriveBy ? query.arriveBy : false,
44-
time: query.time,
45-
};
46-
const storage = addFutureRoute(route, this.getFutureRoutes(), {
47-
itinerarySummaryPrefix: PREFIX_ITINERARY_SUMMARY,
48-
});
26+
if (itinSearch.time < new Date().getTime() / 1000 + 300) {
27+
// saved search must be at least 5 minutes in future
28+
return;
29+
}
30+
const storage = addFutureRoute(itinSearch, this.getFutureRoutes());
4931
setFutureRoutesStorage(storage);
5032
this.emitChange();
5133
}

app/store/localStorage.js

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -201,15 +201,13 @@ export function getFavouriteRoutesStorage() {
201201
return getItemAsJson('favouriteRoutes');
202202
}
203203

204+
const filterOld = ['SelectFromMap', 'SelectFromOwnLocations', 'back'];
205+
204206
export function getOldSearchesStorage() {
205207
const storage = getItemAsJson('saved-searches', '{"items": []}');
206208
return {
207209
...storage,
208-
items: storage.items.filter(
209-
search =>
210-
search.item.address !== 'SelectFromMap' &&
211-
search.item.address !== 'back',
212-
),
210+
items: storage.items.filter(s => !filterOld.includes(s.item.address)),
213211
};
214212
}
215213

app/util/queryUtils.js

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import {
55
getIntermediatePlaces,
66
} from './otpStrings';
77
import { getPathWithEndpointObjects, PREFIX_ITINERARY_SUMMARY } from './path';
8-
import { saveFutureRoute } from '../action/FutureRoutesActions';
98
import { addViaPoint } from '../action/ViaPointActions';
109

1110
/**
@@ -84,14 +83,7 @@ export const updateItinerarySearch = (
8483
destination,
8584
router,
8685
location,
87-
executeAction,
8886
) => {
89-
executeAction(saveFutureRoute, {
90-
origin,
91-
destination,
92-
query: location.query,
93-
});
94-
9587
const newLocation = {
9688
...location,
9789
state: {
@@ -123,11 +115,5 @@ export const onLocationPopup = (item, id, router, match, executeAction) => {
123115
} else {
124116
destination = item;
125117
}
126-
updateItinerarySearch(
127-
origin,
128-
destination,
129-
router,
130-
match.location,
131-
executeAction,
132-
);
118+
updateItinerarySearch(origin, destination, router, match.location);
133119
};

digitransit-store/packages/digitransit-store-future-route/package.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@digitransit-store/digitransit-store-future-route",
3-
"version": "1.0.0",
3+
"version": "1.1.0",
44
"description": "digitransit-store future-route module",
55
"main": "lib/index.js",
66
"publishConfig": {
@@ -21,6 +21,10 @@
2121
"digitransit-store",
2222
"FutureRoute"
2323
],
24+
"peerDependencies": {
25+
"lodash": "4.17.21",
26+
"lodash-es": "4.17.21"
27+
},
2428
"author": "Digitransit Authors",
2529
"license": "(AGPL-3.0 OR EUPL-1.2)"
2630
}
Lines changed: 42 additions & 145 deletions
Original file line numberDiff line numberDiff line change
@@ -1,156 +1,56 @@
11
import sortBy from 'lodash/sortBy';
22

3-
/**
4-
* @example
5-
* const oldRouteCollection = {
6-
* [
7-
* {
8-
* type: 'FutureRoute',
9-
* properties: {
10-
* layer: 'futureRoute',
11-
* origin: {
12-
* name: 'Pasila',
13-
* localadmin: 'Helsinki',
14-
* coordinates: {
15-
* lat: 60.198828,
16-
* lon: 24.933514,
17-
* },
18-
* },
19-
* destination: {
20-
* name: 'Ilmala',
21-
* localadmin: 'Helsinki',
22-
* coordinates: {
23-
* lat: 60.208466,
24-
* lon: 24.919756,
25-
* },
26-
* },
27-
* arriveBy: 'true',
28-
* time: 1600866900,
29-
* url: '/reitti/Pasila%2C%20Helsinki%3A%3A60.198828%2C24.933514/Ilmala%2C%20Helsinki%3A%3A60.208466%2C24.919756?arriveBy=true&time=1600866900',
30-
* },
31-
* },
32-
* {
33-
* type: 'FutureRoute',
34-
* properties: {
35-
* layer: 'futureRoute',
36-
* origin: {
37-
* name: 'Ilmala',
38-
* localadmin: 'Helsinki',
39-
* coordinates: {
40-
* lat: 60.208466,
41-
* lon: 24.919756,
42-
* },
43-
* },
44-
* destination: {
45-
* name: 'Pasila',
46-
* localadmin: 'Helsinki',
47-
* coordinates: {
48-
* lat: 60.198828,
49-
* lon: 24.933514,
50-
* },
51-
* },
52-
* time: 1600877700,
53-
* url: '/reitti/Ilmala%2C%20Helsinki%3A%3A60.208466%2C24.919756/Pasila%2C%20Helsinki%3A%3A60.198828%2C24.933514?arriveBy=true&time=1600877700',
54-
* },
55-
* },
56-
* ],
57-
* }
58-
*
59-
* const newRoute = {
60-
* origin: {
61-
* address: 'Pasila, Helsinki',
62-
* coordinates: { lat: 60.198828, lon: 24.933514 },
63-
* },
64-
* destination: {
65-
* address: 'Myyrmäki, Vantaa',
66-
* coordinates: { lat: 60.261238, lon: 24.854782 },
67-
* },
68-
* arriveBy: false,
69-
* time: 1600888888,
70-
* };
71-
*
72-
* //add newRoute to oldRouteCollection
73-
* const newRouteCollection = addFutureRoute(newRoute, oldRouteCollection, { prefixItinerarySummary: 'reitti' });
74-
*
75-
* const url = createUrl(newRoute, { prefixItinerarySummary: 'reitti' });
76-
* //'/reitti/Pasila%2C%20Helsinki%3A%3A60.198828%2C24.933514/Myyrmäki%2C%20Vantaa%3A%3A60.261238%2C24.854782?time=1600888888'
77-
*/
78-
79-
function extractRoute(routeIn) {
80-
let extractedRoute = routeIn;
81-
if (routeIn.properties) {
82-
const route = routeIn.properties;
83-
const oLoc = route.origin.localadmin ? `, ${route.origin.localadmin}` : '';
84-
const dLoc = route.destination.localadmin
85-
? `, ${route.destination.localadmin}`
86-
: '';
87-
88-
extractedRoute = {
89-
origin: {
90-
address: `${route.origin.name}${oLoc}`,
91-
coordinates: route.origin.coordinates,
92-
},
93-
destination: {
94-
address: `${route.destination.name}${dLoc}`,
95-
coordinates: route.destination.coordinates,
96-
},
97-
arriveBy: route.arriveBy ? route.arriveBy : false,
98-
time: route.time,
99-
};
100-
}
101-
return extractedRoute;
102-
}
103-
1043
const DEFAULT_ITINERARY_PREFIX = 'reitti';
1054

106-
export function createUrl(routeIn, pathOpts) {
107-
const route = extractRoute(routeIn);
5+
export function createUrl(item, pathOpts) {
6+
const props = item.properties;
1087
if (
109-
route &&
110-
route.origin &&
111-
route.origin.address &&
112-
route.origin.coordinates &&
113-
route.destination &&
114-
route.destination.address &&
115-
route.destination.coordinates &&
116-
route.time
8+
props.origin?.coordinates &&
9+
props.destination?.coordinates &&
10+
props.time
11711
) {
12+
const oLoc = props.origin.localadmin ? `, ${props.origin.localadmin}` : '';
13+
const oAddr = `${props.origin.name}${oLoc}`;
14+
const dLoc = props.destination.localadmin
15+
? `, ${props.destination.localadmin}`
16+
: '';
17+
const dAddr = `${props.destination.name}${dLoc}`;
18+
11819
let prefix;
119-
if (pathOpts && pathOpts.itinerarySummaryPrefix) {
20+
if (pathOpts?.itinerarySummaryPrefix) {
12021
prefix = pathOpts.itinerarySummaryPrefix;
12122
} else {
12223
prefix = DEFAULT_ITINERARY_PREFIX;
12324
}
124-
125-
let url = `/${prefix}/`;
126-
// set origin
127-
url += `${encodeURIComponent(
128-
`${route.origin.address}::${route.origin.coordinates.lat},${route.origin.coordinates.lon}`,
129-
)}/`;
130-
// set destination
131-
url += encodeURIComponent(
132-
`${route.destination.address}::${route.destination.coordinates.lat},${route.destination.coordinates.lon}`,
25+
const from = encodeURIComponent(
26+
`${oAddr}::${props.origin.coordinates.lat},${props.origin.coordinates.lon}`,
13327
);
134-
// set arrive by and time
135-
if (route.arriveBy) {
136-
url += `?arriveBy=true&time=${route.time}`;
137-
} else {
138-
url += `?time=${route.time}`;
28+
const to = encodeURIComponent(
29+
`${dAddr}::${props.destination.coordinates.lat},${props.destination.coordinates.lon}`,
30+
);
31+
let url = `/${prefix}/${from}/${to}?time=${props.time}`;
32+
if (props.arriveBy) {
33+
url += '&arriveBy=true';
13934
}
14035
return url;
14136
}
14237
return '';
14338
}
14439

145-
export function addFutureRoute(newRoute, routeCollection, pathOpts) {
146-
if (newRoute && newRoute.time > new Date().getTime() / 1000) {
147-
const originAddress = newRoute.origin.address.split(', ');
40+
function searchId(props) {
41+
return `${props.origin.name}, ${props.origin.localadmin} - ${props.destination.name}, ${props.destination.localadmin}`;
42+
}
43+
44+
export function addFutureRoute(item, collection) {
45+
const now = new Date().getTime() / 1000;
46+
if (item && item.time > now) {
47+
const originAddress = item.origin.address.split(', ');
14848
const originName = originAddress[0];
14949
originAddress.shift();
15050
const originLocalAdmin =
15151
originAddress.length === 1 ? originAddress[0] : originAddress.join(', ');
15252

153-
const destinationAddress = newRoute.destination.address.split(', ');
53+
const destinationAddress = item.destination.address.split(', ');
15454
const destinationName = destinationAddress[0];
15555
destinationAddress.shift();
15656
const destinationLocalAdmin =
@@ -166,31 +66,28 @@ export function addFutureRoute(newRoute, routeCollection, pathOpts) {
16666
name: originName,
16767
localadmin: originLocalAdmin,
16868
coordinates: {
169-
lat: newRoute.origin.coordinates.lat,
170-
lon: newRoute.origin.coordinates.lon,
69+
lat: item.origin.coordinates.lat,
70+
lon: item.origin.coordinates.lon,
17171
},
17272
},
17373
destination: {
17474
name: destinationName,
17575
localadmin: destinationLocalAdmin,
17676
coordinates: {
177-
lat: newRoute.destination.coordinates.lat,
178-
lon: newRoute.destination.coordinates.lon,
77+
lat: item.destination.coordinates.lat,
78+
lon: item.destination.coordinates.lon,
17979
},
18080
},
181-
arriveBy: newRoute.arriveBy,
182-
time: newRoute.time,
183-
url: createUrl(newRoute, pathOpts),
81+
arriveBy: item.arriveBy,
82+
time: item.time,
18483
},
18584
};
18685

187-
const newRouteOriginAndDestination = `${routeToAdd.properties.origin.name}, ${routeToAdd.properties.origin.localadmin} - ${routeToAdd.properties.destination.name}, ${routeToAdd.properties.destination.localadmin}`;
188-
const futureRoutes = routeCollection
189-
? routeCollection.filter(
190-
r =>
191-
r.properties.time >= new Date().getTime() / 1000 &&
192-
`${r.properties.origin.name}, ${r.properties.origin.localadmin} - ${r.properties.destination.name}, ${r.properties.destination.localadmin}` !==
193-
newRouteOriginAndDestination,
86+
const newId = searchId(routeToAdd.properties);
87+
88+
const futureRoutes = collection
89+
? collection.filter(
90+
r => r.properties.time >= now && searchId(r.properties) !== newId,
19491
)
19592
: [];
19693
const sortedItems = sortBy(
@@ -203,5 +100,5 @@ export function addFutureRoute(newRoute, routeCollection, pathOpts) {
203100
);
204101
return sortedItems;
205102
}
206-
return routeCollection || JSON.parse('[]');
103+
return collection || [];
207104
}

yarn.lock

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2326,6 +2326,9 @@ __metadata:
23262326
"@digitransit-store/digitransit-store-future-route@workspace:digitransit-store/packages/digitransit-store-future-route":
23272327
version: 0.0.0-use.local
23282328
resolution: "@digitransit-store/digitransit-store-future-route@workspace:digitransit-store/packages/digitransit-store-future-route"
2329+
peerDependencies:
2330+
lodash: 4.17.21
2331+
lodash-es: 4.17.21
23292332
languageName: unknown
23302333
linkType: soft
23312334

0 commit comments

Comments
 (0)