@autobusal/routes-search 1.0.0 → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/RoutesSearch.tsx +14 -7
- package/package.json +1 -1
- package/services.ts +1 -1
- package/types.ts +2 -0
- package/utilities.ts +23 -0
package/RoutesSearch.tsx
CHANGED
|
@@ -5,10 +5,10 @@ import { FaArrowUp, FaHistory } from 'react-icons/fa';
|
|
|
5
5
|
import { AiOutlineSearch } from 'react-icons/ai';
|
|
6
6
|
import { Button, Autocomplete, Calendar, Passengers } from '@autobusal/common';
|
|
7
7
|
import { Display, createDate } from '@autobusal/utilities';
|
|
8
|
+
import { getCities } from './utilities';
|
|
8
9
|
import { Options, Columns, Column, Item, Title, Action } from './styles';
|
|
9
|
-
import { AutocompleteData } from '@autobusal/common/Autocomplete/types';
|
|
10
10
|
import { RoutesSearchForm } from './types';
|
|
11
|
-
import {
|
|
11
|
+
import { useGetSuggestions } from './services';
|
|
12
12
|
|
|
13
13
|
interface Props {
|
|
14
14
|
type: 'departure' | 'return'
|
|
@@ -28,7 +28,7 @@ const RoutesSearch = ({ type, from, to, departure, _return, adultsNo, childrenNo
|
|
|
28
28
|
|
|
29
29
|
const { register, handleSubmit, formState: { errors }, setValue } = useForm<RoutesSearchForm>();
|
|
30
30
|
|
|
31
|
-
const { data } =
|
|
31
|
+
const { data: CitiesData } = useGetSuggestions();
|
|
32
32
|
|
|
33
33
|
const onClick = (type: ('departure' | 'return')): void => {
|
|
34
34
|
setSelected(type);
|
|
@@ -45,6 +45,16 @@ const RoutesSearch = ({ type, from, to, departure, _return, adultsNo, childrenNo
|
|
|
45
45
|
data.babies = Number(data.babies);
|
|
46
46
|
data.children = Number(data.children);
|
|
47
47
|
|
|
48
|
+
// add the locations
|
|
49
|
+
const from = data.from.split(':');
|
|
50
|
+
data.from = from[0];
|
|
51
|
+
data.stop_from = from[1];
|
|
52
|
+
|
|
53
|
+
// add the to locations
|
|
54
|
+
const to = data.to.split(':');
|
|
55
|
+
data.to = to[0];
|
|
56
|
+
data.stop_to = to[1];
|
|
57
|
+
|
|
48
58
|
if (data.adults == 0) {
|
|
49
59
|
alert(t('routes_search.errors.adults'));
|
|
50
60
|
} else if (data.adults + data.children > 5) {
|
|
@@ -58,10 +68,7 @@ const RoutesSearch = ({ type, from, to, departure, _return, adultsNo, childrenNo
|
|
|
58
68
|
}
|
|
59
69
|
};
|
|
60
70
|
|
|
61
|
-
const cities
|
|
62
|
-
name: `${ city.name }, ${ city.country?.name ?? '' }`,
|
|
63
|
-
value: city.slug
|
|
64
|
-
}));
|
|
71
|
+
const cities = getCities(CitiesData, t);
|
|
65
72
|
|
|
66
73
|
return (
|
|
67
74
|
<form onSubmit={ handleSubmit(onSubmit) }>
|
package/package.json
CHANGED
package/services.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { useSuspenseQuery, UseSuspenseQueryResult } from '@tanstack/react-query'
|
|
|
2
2
|
import { apiClient } from '@autobusal/providers';
|
|
3
3
|
import { CityData } from '@autobusal/providers/types/locations';
|
|
4
4
|
|
|
5
|
-
export const
|
|
5
|
+
export const useGetSuggestions = (): UseSuspenseQueryResult<CityData[]> => (
|
|
6
6
|
useSuspenseQuery({
|
|
7
7
|
queryKey: ['search-suggestions'],
|
|
8
8
|
queryFn: async () => (
|
package/types.ts
CHANGED
package/utilities.ts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { TFunction } from 'i18next';
|
|
2
|
+
import { AutocompleteData } from '@autobusal/common/Autocomplete/types';
|
|
3
|
+
import { CityData } from '@autobusal/providers/types/locations';
|
|
4
|
+
|
|
5
|
+
export const getCities = (cities: CityData[], t: TFunction<'common'>): AutocompleteData[] => {
|
|
6
|
+
const data: AutocompleteData[] = [];
|
|
7
|
+
|
|
8
|
+
cities.forEach(city => {
|
|
9
|
+
data.push({
|
|
10
|
+
name: `${ city.name }, ${ city.country?.name ?? '' } (${ t('all_stations', { ns: 'common' }) })`,
|
|
11
|
+
value: `${ city.slug }`
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
city.stops?.forEach(stop => {
|
|
15
|
+
data.push({
|
|
16
|
+
name: `${ city.name }, ${ city.country?.name ?? '' } (${ stop.name })`,
|
|
17
|
+
value: `${ city.slug }:${ stop.id }`
|
|
18
|
+
});
|
|
19
|
+
});
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return data;
|
|
23
|
+
};
|