-
Notifications
You must be signed in to change notification settings - Fork 0
/
endpoint.js
189 lines (176 loc) · 5.46 KB
/
endpoint.js
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import { divIcon } from "leaflet";
import {
constructLocation,
matchLatLon
} from "../core-utils/map";
import { locationType } from "../core-utils/types";
import PropTypes from "prop-types";
import React, { Component } from "react";
import { Marker, Popup } from "react-leaflet";
import ReactDOMServer from "react-dom/server";
import {
Briefcase,
Home,
MapMarkerAlt,
Sync,
Times
} from "@styled-icons/fa-solid";
import { withNamespaces } from "react-i18next"
import { ListGroup, ListGroupItem } from 'react-bootstrap'
/**
* These icons are used to render common icons for user locations. These will
* only show up in applications that allow saving user locations.
*/
function UserLocationIcon({ type }) {
switch (type) {
case "briefcase":
return <Briefcase size={16} />;
case "home":
return <Home size={16} />;
case "map-marker":
return <MapMarkerAlt size={16} />;
case "refresh":
return <Sync size={16} />;
case "times":
return <Times size={16} />;
default:
return null;
}
}
UserLocationIcon.propTypes = {
type: PropTypes.string.isRequired
};
class Endpoint extends Component {
rememberAsHome = () => {
const { location: propsLocation, rememberPlace } = this.props;
const location = { ...propsLocation };
location.id = "home";
location.icon = "home";
location.type = "home";
rememberPlace({ type: "home", location });
};
rememberAsWork = () => {
const { location: propsLocation, rememberPlace } = this.props;
const location = { ...propsLocation };
location.id = "work";
location.icon = "briefcase";
location.type = "work";
rememberPlace({ type: "work", location });
};
forgetHome = () => {
const { forgetPlace } = this.props;
forgetPlace("home");
};
forgetWork = () => {
const { forgetPlace } = this.props;
forgetPlace("work");
};
clearLocation = () => {
const { clearLocation, type } = this.props;
clearLocation({ locationType:type });
};
swapLocation = () => {
this.props.switchLocations();
};
onDragEnd = e => {
const { setLocation, type } = this.props;
const location = constructLocation(e.target.getLatLng());
setLocation({ locationType: type, location, reverseGeocode: true });
};
render() {
const {
location,
locations,
MapMarkerIcon,
showUserSettings,
type,
t
} = this.props;
const position =
location && location.lat && location.lon
? [location.lat, location.lon]
: null;
if (!position) return null;
const match = locations.find(l => matchLatLon(l, location));
const isWork = match && match.type === "work";
const isHome = match && match.type === "home";
const iconHtml = ReactDOMServer.renderToStaticMarkup(
<MapMarkerIcon location={location} type={type} />
);
const otherType = t( type === 'from' ? 'destination' : 'departure2')
const icon = isWork ? "briefcase" : isHome ? "home" : "map-marker";
return (
<Marker
draggable
icon={divIcon({ html: iconHtml, className: "" })}
position={position}
onDragEnd={this.onDragEnd}
>
{showUserSettings && (
<Popup className="otp-ui-endpointMarkerPopup">
<ListGroup>
<ListGroupItem>
<strong><UserLocationIcon type={icon} /> {location.name}</strong>
</ListGroupItem>
<ListGroupItem
disabled={isWork}
onClick={isHome ? this.forgetHome : this.rememberAsHome}
>
{isHome ? (
<>
<UserLocationIcon type="times" /> {t('forget_home')}
</>
) : (
<>
<UserLocationIcon type="home" /> {t('save_as_home')}
</>
)}
</ListGroupItem>
<ListGroupItem
disabled={isHome}
onClick={isWork ? this.forgetWork : this.rememberAsWork}
>
{isWork ? (
<>
<UserLocationIcon type="times" /> {t('forget_work')}
</>
) : (
<>
<UserLocationIcon type="briefcase" /> {t('save_as_work')}
</>
)}
</ListGroupItem>
<ListGroupItem
onClick={this.clearLocation}
>
<UserLocationIcon type="times" /> {t('remove')} {t(type === 'from' ? 'departure2' : 'destination')}
</ListGroupItem>
<ListGroupItem
// bsStyle="link"
onClick={this.swapLocation}
>
<UserLocationIcon type="refresh" /> {t('change_in_location', { label: otherType })}
</ListGroupItem>
</ListGroup>
</Popup>
)}
</Marker>
);
}
}
// See documenation in main index file for documenation on these props.
Endpoint.propTypes = {
clearLocation: PropTypes.func.isRequired,
forgetPlace: PropTypes.func.isRequired,
location: locationType,
locations: PropTypes.arrayOf(locationType).isRequired,
MapMarkerIcon: PropTypes.elementType.isRequired,
rememberPlace: PropTypes.func.isRequired,
setLocation: PropTypes.func.isRequired,
showUserSettings: PropTypes.bool.isRequired,
type: PropTypes.string.isRequired
};
Endpoint.defaultProps = {
location: undefined
};
export default withNamespaces()(Endpoint)