sfc-utils 1.4.73 → 1.4.75

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.
@@ -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
- // Make req
76
- fetch("https://projects.sfchronicle.com/feeds/geocode/v2.php", {
77
- method: "POST",
78
- body: formData,
79
- })
80
- .then((resp) => {
81
- // Sometimes, there's a junk response, so let's handle that gracefully
82
- if (!resp || !resp.ok) {
83
- return null;
84
- }
85
- return resp.json();
86
- })
87
- .then((output) => {
88
- // If this is not the latest fetch, bail and fetch latest
89
- if (latestFetchRef.current !== query) {
90
- setTimeout(() => {
91
- // Delay a bit because it seems like we're still typing
92
- search(latestFetchRef.current);
93
- }, 1000);
94
- return;
95
- } else {
96
- // Uncomment this to see what request was actually honored
97
- //console.log('OK VALID RESULTS FOR', latestFetchRef.current)
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
- // Handle result
110
- if (output.data.length === 0) {
111
- // We could show something saying "No results" ... or we could not
112
- if (output.fallback) {
113
- // If we're using the fallback, we need to encourage the user to enter city
114
- setLocData([
115
- { name: "No results, make sure to include city name" },
116
- ]);
117
- }
118
- } else {
119
- // Create a keydown event listener
120
- setSingletonEventListener("keydown", function resultsKeyHandler(e) {
121
- setActiveKeyboardIndex((prevIndex) => {
122
- let newIndex = prevIndex;
123
- switch (e.key) {
124
- case "ArrowDown":
125
- // Handle the index
126
- if (prevIndex === null) {
127
- newIndex = 0;
128
- } else if (prevIndex < output.data.length - 1) {
129
- newIndex = prevIndex + 1;
130
- } else {
131
- newIndex = 0;
132
- }
133
- break;
134
- case "ArrowUp":
135
- // Handle the index
136
- if (prevIndex === null) {
137
- newIndex = output.data.length - 1;
138
- } else if (prevIndex > 0) {
139
- newIndex = prevIndex - 1;
140
- } else {
141
- newIndex = output.data.length - 1;
142
- }
143
- break;
144
- case "Enter":
145
- // Do something with the data
146
- const selectedLocation = output.data[prevIndex];
147
- // Update the input value with the choice
148
- if (selectedLocation) {
149
- setInputValue(selectedLocation.name);
150
- // Call function if it exists
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-test.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
- return newIndex;
159
- });
160
- });
152
+ // Hide the list now
153
+ setLocData(null);
154
+ }
155
+ return newIndex;
156
+ });
157
+ });
161
158
 
162
- // Start listening for a click outside the div
163
- document.addEventListener("click", function resultsClickHandler(e) {
164
- // Whether we clicked inside or outside, we hide the list
165
- setLocData(null);
166
- // Also cancel the keydown listener
167
- setSingletonEventListener("keydown");
168
- // Received click, cancel this listener
169
- this.removeEventListener("click", resultsClickHandler);
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
package/nav2.js CHANGED
@@ -1,13 +1,7 @@
1
1
  let { getBrands } = require("./brands");
2
2
 
3
3
  // Handle nav for various markets and include nav options for other links
4
- let getNav2 = function (
5
- meta,
6
- urlAdd,
7
- forceColor,
8
- navLink,
9
- navArray
10
- ) {
4
+ let getNav2 = function (meta, urlAdd, forceColor, navLink, navArray) {
11
5
  // If we aren't passing meta in, we have to call getSettings here
12
6
  if (!meta) {
13
7
  let { getSettings } = require("./settings");
@@ -141,7 +135,7 @@ let getNav2 = function (
141
135
  >
142
136
  ${navLink.text}${dropdownIcon}
143
137
  </a>
144
- `
138
+ `;
145
139
  }
146
140
 
147
141
  let rightBlock = `
@@ -149,7 +143,6 @@ let getNav2 = function (
149
143
  <div>Subscribe</div>
150
144
  </a>
151
145
  `;
152
-
153
146
 
154
147
  let navHTML = `<nav class="nav2-container ${invertClass}">
155
148
  <div class="nav2-left">
@@ -166,12 +159,12 @@ let getNav2 = function (
166
159
  class="nav2-desk-logo"
167
160
  alt="Logo"
168
161
  src="https://files.sfchronicle.com/static-assets/logos/${marketPrefix}-${color}.png"
169
- ></img>
162
+ />
170
163
  <img
171
164
  class="nav2-mobile-logo"
172
165
  alt="Logo"
173
166
  src="https://files.sfchronicle.com/static-assets/logos/${marketPrefix}-square-${color}.png"
174
- ></img>
167
+ />
175
168
  </div>
176
169
  </a>
177
170
  </div>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "sfc-utils",
3
- "version": "1.4.73",
3
+ "version": "1.4.75",
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
+ }