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