sfc-utils 1.4.55 → 1.4.56
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/components/geocoder.mjs +92 -93
- package/package.json +1 -1
package/components/geocoder.mjs
CHANGED
|
@@ -1,179 +1,179 @@
|
|
|
1
|
-
import React, { useRef, useState } from
|
|
2
|
-
import * as geocoderStyles from
|
|
1
|
+
import React, { useRef, useState } from "react";
|
|
2
|
+
import * as geocoderStyles from "../styles/modules/geocoder.module.less";
|
|
3
3
|
|
|
4
4
|
// This is a singleton event listener that we can use to add/remove event listeners
|
|
5
5
|
// Don't call it during SSR
|
|
6
6
|
var setSingletonEventListener = (function (element) {
|
|
7
|
-
var handlers = {}
|
|
7
|
+
var handlers = {};
|
|
8
8
|
return function (evtName, func) {
|
|
9
9
|
handlers.hasOwnProperty(evtName) &&
|
|
10
|
-
element.removeEventListener(evtName, handlers[evtName])
|
|
10
|
+
element.removeEventListener(evtName, handlers[evtName]);
|
|
11
11
|
if (func) {
|
|
12
|
-
handlers[evtName] = func
|
|
13
|
-
element.addEventListener(evtName, func)
|
|
12
|
+
handlers[evtName] = func;
|
|
13
|
+
element.addEventListener(evtName, func);
|
|
14
14
|
} else {
|
|
15
|
-
delete handlers[evtName]
|
|
15
|
+
delete handlers[evtName];
|
|
16
16
|
}
|
|
17
|
-
}
|
|
18
|
-
})(typeof document !==
|
|
17
|
+
};
|
|
18
|
+
})(typeof document !== "undefined" && document);
|
|
19
19
|
|
|
20
20
|
// Our new and improved geocoder! Hits PositionStack first and then falls back to classic geocoder
|
|
21
21
|
// Accepts a region to filter results by -- this can be custom but if it's not passed in, it will default to the state where the market resides
|
|
22
22
|
const Geocoder = ({
|
|
23
23
|
filterRegion, // You need to test results, but could also pass in a neighbourhood, district, city, county, state or administrative area
|
|
24
|
-
market,
|
|
24
|
+
market, // Will filter by the market's state if no filterRegion provided
|
|
25
25
|
resultFunc,
|
|
26
26
|
buttonTrackingId,
|
|
27
|
-
placeholder
|
|
27
|
+
placeholder,
|
|
28
28
|
}) => {
|
|
29
29
|
// Show a loader when we're requesting
|
|
30
|
-
const [loading, setLoading] = useState(false)
|
|
31
|
-
const [locData, setLocData] = useState(null)
|
|
32
|
-
const [inputValue, setInputValue] = useState(
|
|
33
|
-
const [activeKeyboardIndex, setActiveKeyboardIndex] = useState(null)
|
|
34
|
-
const resultsRef = useRef(null)
|
|
35
|
-
const geocoderInputRef = useRef(null)
|
|
36
|
-
const latestFetchRef = useRef(null)
|
|
30
|
+
const [loading, setLoading] = useState(false);
|
|
31
|
+
const [locData, setLocData] = useState(null);
|
|
32
|
+
const [inputValue, setInputValue] = useState("");
|
|
33
|
+
const [activeKeyboardIndex, setActiveKeyboardIndex] = useState(null);
|
|
34
|
+
const resultsRef = useRef(null);
|
|
35
|
+
const geocoderInputRef = useRef(null);
|
|
36
|
+
const latestFetchRef = useRef(null);
|
|
37
37
|
|
|
38
38
|
if (!filterRegion) {
|
|
39
39
|
switch (market) {
|
|
40
|
-
case
|
|
41
|
-
filterRegion =
|
|
42
|
-
break
|
|
43
|
-
case
|
|
44
|
-
case
|
|
45
|
-
filterRegion =
|
|
46
|
-
break
|
|
47
|
-
case
|
|
48
|
-
filterRegion =
|
|
49
|
-
break
|
|
50
|
-
case
|
|
51
|
-
filterRegion =
|
|
40
|
+
case "SFC":
|
|
41
|
+
filterRegion = "California";
|
|
42
|
+
break;
|
|
43
|
+
case "Houston":
|
|
44
|
+
case "San Antonio":
|
|
45
|
+
filterRegion = "Texas";
|
|
46
|
+
break;
|
|
47
|
+
case "Albany":
|
|
48
|
+
filterRegion = "New York";
|
|
49
|
+
break;
|
|
50
|
+
case "CT":
|
|
51
|
+
filterRegion = "Connecticut";
|
|
52
52
|
default:
|
|
53
|
-
filterRegion =
|
|
53
|
+
filterRegion = "United States";
|
|
54
54
|
}
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
// We don't want this CONSTANTLY firing, so we debounce it
|
|
58
58
|
const search = (query) => {
|
|
59
59
|
// Save value of query so we can check if it's the last one
|
|
60
|
-
latestFetchRef.current = query
|
|
60
|
+
latestFetchRef.current = query;
|
|
61
61
|
// If we're already loading, don't fire another request, the request will be re-requested after the first one finishes
|
|
62
62
|
if (loading) {
|
|
63
|
-
return false
|
|
63
|
+
return false;
|
|
64
64
|
}
|
|
65
65
|
// POST as form url encoded data
|
|
66
|
-
let formData = new FormData()
|
|
67
|
-
formData.append(
|
|
68
|
-
formData.append(
|
|
66
|
+
let formData = new FormData();
|
|
67
|
+
formData.append("query", query);
|
|
68
|
+
formData.append("region", filterRegion);
|
|
69
69
|
// Remove any existing event listeners
|
|
70
|
-
setSingletonEventListener(
|
|
70
|
+
setSingletonEventListener("keydown");
|
|
71
71
|
// Make req
|
|
72
|
-
fetch(
|
|
73
|
-
method:
|
|
72
|
+
fetch("https://projects.sfchronicle.com/feeds/geocode/v2.php", {
|
|
73
|
+
method: "POST",
|
|
74
74
|
body: formData,
|
|
75
75
|
})
|
|
76
76
|
.then((resp) => {
|
|
77
77
|
// Sometimes, there's a junk response, so let's handle that gracefully
|
|
78
78
|
if (!resp || !resp.ok) {
|
|
79
|
-
return null
|
|
79
|
+
return null;
|
|
80
80
|
}
|
|
81
|
-
return resp.json()
|
|
81
|
+
return resp.json();
|
|
82
82
|
})
|
|
83
83
|
.then((output) => {
|
|
84
84
|
// If this is not the latest fetch, bail and fetch latest
|
|
85
85
|
if (latestFetchRef.current !== query) {
|
|
86
86
|
setTimeout(() => {
|
|
87
87
|
// Delay a bit because it seems like we're still typing
|
|
88
|
-
search(latestFetchRef.current)
|
|
89
|
-
}, 1000)
|
|
90
|
-
return
|
|
88
|
+
search(latestFetchRef.current);
|
|
89
|
+
}, 1000);
|
|
90
|
+
return;
|
|
91
91
|
} else {
|
|
92
92
|
// Uncomment this to see what request was actually honored
|
|
93
93
|
//console.log('OK VALID RESULTS FOR', latestFetchRef.current)
|
|
94
94
|
}
|
|
95
95
|
// Unset loading
|
|
96
|
-
setLoading(false)
|
|
96
|
+
setLoading(false);
|
|
97
97
|
// Remove any existing event listeners
|
|
98
|
-
setSingletonEventListener(
|
|
98
|
+
setSingletonEventListener("keydown");
|
|
99
99
|
// Show results
|
|
100
|
-
setLocData(output.data)
|
|
100
|
+
setLocData(output.data);
|
|
101
101
|
// Bail early if there's no data
|
|
102
102
|
if (!output) {
|
|
103
|
-
return false
|
|
103
|
+
return false;
|
|
104
104
|
}
|
|
105
105
|
// Handle result
|
|
106
106
|
if (output.data.length === 0) {
|
|
107
107
|
// We could show something saying "No results" ... or we could not
|
|
108
108
|
} else {
|
|
109
109
|
// Create a keydown event listener
|
|
110
|
-
setSingletonEventListener(
|
|
110
|
+
setSingletonEventListener("keydown", function resultsKeyHandler(e) {
|
|
111
111
|
setActiveKeyboardIndex((prevIndex) => {
|
|
112
|
-
let newIndex = prevIndex
|
|
112
|
+
let newIndex = prevIndex;
|
|
113
113
|
switch (e.key) {
|
|
114
|
-
case
|
|
114
|
+
case "ArrowDown":
|
|
115
115
|
// Handle the index
|
|
116
116
|
if (prevIndex === null) {
|
|
117
|
-
newIndex = 0
|
|
117
|
+
newIndex = 0;
|
|
118
118
|
} else if (prevIndex < output.data.length - 1) {
|
|
119
|
-
newIndex = prevIndex + 1
|
|
119
|
+
newIndex = prevIndex + 1;
|
|
120
120
|
} else {
|
|
121
|
-
newIndex = 0
|
|
121
|
+
newIndex = 0;
|
|
122
122
|
}
|
|
123
|
-
break
|
|
124
|
-
case
|
|
123
|
+
break;
|
|
124
|
+
case "ArrowUp":
|
|
125
125
|
// Handle the index
|
|
126
126
|
if (prevIndex === null) {
|
|
127
|
-
newIndex = output.data.length - 1
|
|
127
|
+
newIndex = output.data.length - 1;
|
|
128
128
|
} else if (prevIndex > 0) {
|
|
129
|
-
newIndex = prevIndex - 1
|
|
129
|
+
newIndex = prevIndex - 1;
|
|
130
130
|
} else {
|
|
131
|
-
newIndex = output.data.length - 1
|
|
131
|
+
newIndex = output.data.length - 1;
|
|
132
132
|
}
|
|
133
|
-
break
|
|
134
|
-
case
|
|
133
|
+
break;
|
|
134
|
+
case "Enter":
|
|
135
135
|
// Do something with the data
|
|
136
|
-
const selectedLocation = output.data[prevIndex]
|
|
136
|
+
const selectedLocation = output.data[prevIndex];
|
|
137
137
|
// Update the input value with the choice
|
|
138
138
|
if (selectedLocation) {
|
|
139
|
-
setInputValue(selectedLocation.name)
|
|
139
|
+
setInputValue(selectedLocation.name);
|
|
140
140
|
// Call function if it exists
|
|
141
141
|
if (resultFunc) {
|
|
142
|
-
resultFunc(selectedLocation)
|
|
142
|
+
resultFunc(selectedLocation);
|
|
143
143
|
}
|
|
144
144
|
}
|
|
145
145
|
// Hide the list now
|
|
146
|
-
setLocData(null)
|
|
146
|
+
setLocData(null);
|
|
147
147
|
}
|
|
148
|
-
return newIndex
|
|
149
|
-
})
|
|
150
|
-
})
|
|
148
|
+
return newIndex;
|
|
149
|
+
});
|
|
150
|
+
});
|
|
151
151
|
|
|
152
152
|
// Start listening for a click outside the div
|
|
153
|
-
document.addEventListener(
|
|
153
|
+
document.addEventListener("click", function resultsClickHandler(e) {
|
|
154
154
|
// Whether we clicked inside or outside, we hide the list
|
|
155
|
-
setLocData(null)
|
|
155
|
+
setLocData(null);
|
|
156
156
|
// Also cancel the keydown listener
|
|
157
|
-
setSingletonEventListener(
|
|
157
|
+
setSingletonEventListener("keydown");
|
|
158
158
|
// Received click, cancel this listener
|
|
159
|
-
this.removeEventListener(
|
|
160
|
-
})
|
|
159
|
+
this.removeEventListener("click", resultsClickHandler);
|
|
160
|
+
});
|
|
161
161
|
}
|
|
162
|
-
})
|
|
163
|
-
}
|
|
162
|
+
});
|
|
163
|
+
};
|
|
164
164
|
|
|
165
165
|
// Handle the change event
|
|
166
166
|
const handleChange = (event) => {
|
|
167
167
|
// Set the input value
|
|
168
|
-
const inputValue = event.target.value
|
|
169
|
-
setInputValue(inputValue)
|
|
170
|
-
setActiveKeyboardIndex(null)
|
|
168
|
+
const inputValue = event.target.value;
|
|
169
|
+
setInputValue(inputValue);
|
|
170
|
+
setActiveKeyboardIndex(null);
|
|
171
171
|
// Only query if the value is not empty
|
|
172
172
|
if (inputValue) {
|
|
173
|
-
setLoading(true)
|
|
174
|
-
search(inputValue)
|
|
173
|
+
setLoading(true);
|
|
174
|
+
search(inputValue);
|
|
175
175
|
}
|
|
176
|
-
}
|
|
176
|
+
};
|
|
177
177
|
|
|
178
178
|
return (
|
|
179
179
|
<div className={geocoderStyles.wrapper}>
|
|
@@ -192,44 +192,43 @@ const Geocoder = ({
|
|
|
192
192
|
{locData && (
|
|
193
193
|
<ul className={geocoderStyles.resultsWrapper} ref={resultsRef}>
|
|
194
194
|
{locData.map((loc, i) => {
|
|
195
|
-
let thisClass = geocoderStyles.result
|
|
195
|
+
let thisClass = geocoderStyles.result;
|
|
196
196
|
if (activeKeyboardIndex === i) {
|
|
197
|
-
thisClass +=
|
|
197
|
+
thisClass += " " + geocoderStyles.active;
|
|
198
198
|
}
|
|
199
199
|
return (
|
|
200
200
|
<li
|
|
201
201
|
className={thisClass}
|
|
202
202
|
key={i}
|
|
203
203
|
onClick={() => {
|
|
204
|
-
setInputValue(locData[i].name)
|
|
204
|
+
setInputValue(locData[i].name);
|
|
205
205
|
// Call function if it exists
|
|
206
206
|
if (resultFunc) {
|
|
207
|
-
resultFunc(locData[i])
|
|
207
|
+
resultFunc(locData[i]);
|
|
208
208
|
}
|
|
209
209
|
}}
|
|
210
210
|
>
|
|
211
211
|
<button
|
|
212
|
-
|
|
213
|
-
className={buttonTrackingId ? geocoderStyles.button + " devhub-ga-tracking" : geocoderStyles.button}
|
|
212
|
+
className={geocoderStyles.button}
|
|
214
213
|
onFocus={(e) => {
|
|
215
214
|
// Set index
|
|
216
|
-
setActiveKeyboardIndex(i)
|
|
215
|
+
setActiveKeyboardIndex(i);
|
|
217
216
|
}}
|
|
218
217
|
>
|
|
219
218
|
<div className={geocoderStyles.name}>{loc.name}</div>
|
|
220
219
|
<div className={geocoderStyles.details}>
|
|
221
220
|
{loc.locality}
|
|
222
|
-
{loc.locality && loc.region &&
|
|
221
|
+
{loc.locality && loc.region && ", "}
|
|
223
222
|
{loc.region}
|
|
224
223
|
</div>
|
|
225
224
|
</button>
|
|
226
225
|
</li>
|
|
227
|
-
)
|
|
226
|
+
);
|
|
228
227
|
})}
|
|
229
228
|
</ul>
|
|
230
229
|
)}
|
|
231
230
|
</div>
|
|
232
|
-
)
|
|
233
|
-
}
|
|
231
|
+
);
|
|
232
|
+
};
|
|
234
233
|
|
|
235
|
-
export default Geocoder
|
|
234
|
+
export default Geocoder;
|