-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.tsx
More file actions
146 lines (130 loc) · 3.19 KB
/
utils.tsx
File metadata and controls
146 lines (130 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import { Path, UseFormSetError } from "react-hook-form";
import { EditMyCityDialog, GeolocationDialog, MyCityDialog } from './dialogs';
import { CENTER, DIALOGS } from './constants';
import { USER_DATA_FORM } from "./Signup/UserDataForm/Constants";
import {
DirectionsCallback,
GetDialog,
GetMyLocation,
RenderRoute,
} from './types';
import { LocalStorageValue } from '~/hooks/use-local-storage/types';
export const checkIsPasswordConfirmed = <F>(
password: string,
confirmation: string,
setError: UseFormSetError<F>
) => {
const fieldName = USER_DATA_FORM.TAGS.CONFIRMATION as Path<F>;
if (password !== confirmation) {
setError(
fieldName,
{ message: USER_DATA_FORM.VALIDATION.CONFIRMATION.MATCH },
{ shouldFocus: true }
);
return false;
}
return true;
};
export const getRange: GetRange = (start, stop, step = 1) =>
Array.from({ length: (stop - start) / step + 1 }, (_, i) => start + i * step);
export const getDialog: GetDialog = (
dialog,
setDialog,
myCity,
onSelectCity,
onLoadAutocomplete,
onPlaceChanged
) => {
const closeDialog = () => setDialog(null);
const openEditMyCityDialog = () => setDialog(DIALOGS.EDIT_MY_CITY);
switch (dialog) {
case DIALOGS.GEOLOCATION:
return (
<GeolocationDialog
open={dialog === DIALOGS.GEOLOCATION}
onClose={closeDialog}
/>
);
case DIALOGS.MY_CITY:
return (
<MyCityDialog
myCity={myCity}
open={dialog === DIALOGS.MY_CITY}
onClose={closeDialog}
onDecline={openEditMyCityDialog}
/>
);
case DIALOGS.EDIT_MY_CITY:
return (
<EditMyCityDialog
myCity={myCity}
onLoadAutocomplete={onLoadAutocomplete}
onPlaceChanged={onPlaceChanged}
open={dialog === DIALOGS.EDIT_MY_CITY}
onClose={closeDialog}
onSelectCity={onSelectCity}
/>
);
default:
return null;
}
};
export const getMapCenter = (
storedCenter: LocalStorageValue | null
): google.maps.LatLngLiteral => {
if (
storedCenter &&
typeof storedCenter === 'object' &&
'lat' in storedCenter &&
'lng' in storedCenter
)
return {
lat: Number(storedCenter.lat),
lng: Number(storedCenter.lng),
};
return CENTER;
};
export const renderRoute: RenderRoute = (
destination,
myLocation,
map,
directionsRenderer,
directionsService,
setDialog
) => {
const directionsCallback: DirectionsCallback = (result, status) => {
if (result && status === 'OK') {
directionsRenderer?.setMap(map);
directionsRenderer?.setDirections(result);
}
};
if (destination) {
if (!myLocation) {
setDialog(DIALOGS.GEOLOCATION);
return;
}
const request = {
destination,
origin: myLocation,
travelMode: google.maps.TravelMode.DRIVING,
};
directionsService?.route(request, directionsCallback);
} else {
directionsRenderer?.setMap(null);
}
};
export const getMyLocation: GetMyLocation = (setDialog, setMyLocation) => {
const openGeolocationDialog = () => setDialog(DIALOGS.GEOLOCATION);
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position: GeolocationPosition) =>
setMyLocation({
lat: position.coords.latitude,
lng: position.coords.longitude,
}),
openGeolocationDialog
);
return;
}
openGeolocationDialog();
};