sfc-utils 1.4.74 → 1.4.76
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 -96
- package/package.json +1 -1
package/components/geocoder.mjs
CHANGED
|
@@ -57,7 +57,7 @@ const Geocoder = ({
|
|
|
57
57
|
}
|
|
58
58
|
|
|
59
59
|
// We don't want this CONSTANTLY firing, so we debounce it
|
|
60
|
-
const search = (query) => {
|
|
60
|
+
const search = async (query) => {
|
|
61
61
|
// Save value of query so we can check if it's the last one
|
|
62
62
|
latestFetchRef.current = query;
|
|
63
63
|
// If we're already loading, don't fire another request, the request will be re-requested after the first one finishes
|
|
@@ -72,104 +72,100 @@ const Geocoder = ({
|
|
|
72
72
|
}
|
|
73
73
|
// Remove any existing event listeners
|
|
74
74
|
setSingletonEventListener("keydown");
|
|
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
|
-
if (resultFunc) {
|
|
152
|
-
resultFunc(selectedLocation);
|
|
153
|
-
}
|
|
154
|
-
}
|
|
155
|
-
// Hide the list now
|
|
156
|
-
setLocData(null);
|
|
75
|
+
// Wait for a sec to check if we're still typing
|
|
76
|
+
await new Promise((resolve) => setTimeout(resolve, 500)); // We can increase this timeout to ease up on requests too
|
|
77
|
+
// If query doesn't match, we're still typing -- reset
|
|
78
|
+
if (latestFetchRef.current !== query) {
|
|
79
|
+
search(latestFetchRef.current);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
// We've settled, make the req
|
|
83
|
+
const resp = await fetch(
|
|
84
|
+
"https://projects.sfchronicle.com/feeds/geocode/v2.php",
|
|
85
|
+
{
|
|
86
|
+
method: "POST",
|
|
87
|
+
body: formData,
|
|
88
|
+
}
|
|
89
|
+
);
|
|
90
|
+
if (!resp || !resp.ok) {
|
|
91
|
+
return null;
|
|
92
|
+
}
|
|
93
|
+
const output = await resp.json();
|
|
94
|
+
// If this is not the latest fetch after we finished getting results, bail and fetch latest
|
|
95
|
+
if (latestFetchRef.current !== query) {
|
|
96
|
+
search(latestFetchRef.current);
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
// Unset loading
|
|
100
|
+
setLoading(false);
|
|
101
|
+
// Remove any existing event listeners
|
|
102
|
+
setSingletonEventListener("keydown");
|
|
103
|
+
// Show results
|
|
104
|
+
setLocData(output.data);
|
|
105
|
+
// Bail early if there's no data
|
|
106
|
+
if (!output) {
|
|
107
|
+
return false;
|
|
108
|
+
}
|
|
109
|
+
if (output.data.length === 0) {
|
|
110
|
+
// We could show something saying "No results" ... or we could not
|
|
111
|
+
if (output.fallback) {
|
|
112
|
+
// If we're using the fallback, we need to encourage the user to enter city
|
|
113
|
+
setLocData([{ name: "No results, make sure to include city name" }]);
|
|
114
|
+
}
|
|
115
|
+
} else {
|
|
116
|
+
// Create a keydown event listener
|
|
117
|
+
setSingletonEventListener("keydown", function resultsKeyHandler(e) {
|
|
118
|
+
setActiveKeyboardIndex((prevIndex) => {
|
|
119
|
+
let newIndex = prevIndex;
|
|
120
|
+
switch (e.key) {
|
|
121
|
+
case "ArrowDown":
|
|
122
|
+
// Handle the index
|
|
123
|
+
if (prevIndex === null) {
|
|
124
|
+
newIndex = 0;
|
|
125
|
+
} else if (prevIndex < output.data.length - 1) {
|
|
126
|
+
newIndex = prevIndex + 1;
|
|
127
|
+
} else {
|
|
128
|
+
newIndex = 0;
|
|
129
|
+
}
|
|
130
|
+
break;
|
|
131
|
+
case "ArrowUp":
|
|
132
|
+
// Handle the index
|
|
133
|
+
if (prevIndex === null) {
|
|
134
|
+
newIndex = output.data.length - 1;
|
|
135
|
+
} else if (prevIndex > 0) {
|
|
136
|
+
newIndex = prevIndex - 1;
|
|
137
|
+
} else {
|
|
138
|
+
newIndex = output.data.length - 1;
|
|
139
|
+
}
|
|
140
|
+
break;
|
|
141
|
+
case "Enter":
|
|
142
|
+
// Do something with the data
|
|
143
|
+
const selectedLocation = output.data[prevIndex];
|
|
144
|
+
// Update the input value with the choice
|
|
145
|
+
if (selectedLocation) {
|
|
146
|
+
setInputValue(selectedLocation.name);
|
|
147
|
+
// Call function if it exists
|
|
148
|
+
if (resultFunc) {
|
|
149
|
+
resultFunc(selectedLocation);
|
|
150
|
+
}
|
|
157
151
|
}
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
}
|
|
152
|
+
// Hide the list now
|
|
153
|
+
setLocData(null);
|
|
154
|
+
}
|
|
155
|
+
return newIndex;
|
|
156
|
+
});
|
|
157
|
+
});
|
|
161
158
|
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
});
|
|
171
|
-
}
|
|
159
|
+
// Start listening for a click outside the div
|
|
160
|
+
document.addEventListener("click", function resultsClickHandler(e) {
|
|
161
|
+
// Whether we clicked inside or outside, we hide the list
|
|
162
|
+
setLocData(null);
|
|
163
|
+
// Also cancel the keydown listener
|
|
164
|
+
setSingletonEventListener("keydown");
|
|
165
|
+
// Received click, cancel this listener
|
|
166
|
+
this.removeEventListener("click", resultsClickHandler);
|
|
172
167
|
});
|
|
168
|
+
}
|
|
173
169
|
};
|
|
174
170
|
|
|
175
171
|
// Handle the change event
|