@geophrase/core 1.4.0 → 1.4.1
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/package.json +1 -1
- package/src/index.js +50 -13
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -151,6 +151,14 @@ class Geophrase {
|
|
|
151
151
|
return `${this.styleId}-tc`;
|
|
152
152
|
}
|
|
153
153
|
|
|
154
|
+
_loaderId() {
|
|
155
|
+
return `${this.styleId}-loader`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
_spinKeyframesName() {
|
|
159
|
+
return `${this.styleId}-spin`;
|
|
160
|
+
}
|
|
161
|
+
|
|
154
162
|
/**
|
|
155
163
|
* Hide the iframe until its document has actually painted. Browsers render
|
|
156
164
|
* an unloaded iframe as a white rectangle, which (a) flashes visibly and
|
|
@@ -188,7 +196,22 @@ class Geophrase {
|
|
|
188
196
|
}
|
|
189
197
|
#${this.overlayId}.geophrase-active { opacity: 1; visibility: visible; }
|
|
190
198
|
|
|
191
|
-
#${this.
|
|
199
|
+
#${this._loaderId()} {
|
|
200
|
+
position: absolute;
|
|
201
|
+
top: 50%; left: 50%;
|
|
202
|
+
width: 40px; height: 40px;
|
|
203
|
+
margin: -20px 0 0 -20px;
|
|
204
|
+
border: 3px solid rgba(255, 255, 255, 0.25);
|
|
205
|
+
border-top-color: rgba(255, 255, 255, 0.9);
|
|
206
|
+
border-radius: 50%;
|
|
207
|
+
animation: ${this._spinKeyframesName()} 0.8s linear infinite;
|
|
208
|
+
}
|
|
209
|
+
@keyframes ${this._spinKeyframesName()} { to { transform: rotate(360deg); } }
|
|
210
|
+
|
|
211
|
+
#${this.iframeId} {
|
|
212
|
+
width: 100%; height: 100%; border: none;
|
|
213
|
+
position: relative; z-index: 1;
|
|
214
|
+
}
|
|
192
215
|
${this._getIframeBgCSS()}
|
|
193
216
|
|
|
194
217
|
@media (min-width: 768px) {
|
|
@@ -204,11 +227,17 @@ class Geophrase {
|
|
|
204
227
|
const overlay = document.createElement('div');
|
|
205
228
|
overlay.id = this.overlayId;
|
|
206
229
|
|
|
230
|
+
// Spinner shown while the iframe is loading. The iframe stacks above
|
|
231
|
+
// it (z-index: 1) and covers it once paint completes — no state to toggle.
|
|
232
|
+
const loader = document.createElement('div');
|
|
233
|
+
loader.id = this._loaderId();
|
|
234
|
+
overlay.appendChild(loader);
|
|
235
|
+
|
|
207
236
|
const iframe = document.createElement('iframe');
|
|
208
237
|
iframe.id = this.iframeId;
|
|
209
238
|
iframe.allow = 'geolocation';
|
|
210
|
-
|
|
211
239
|
overlay.appendChild(iframe);
|
|
240
|
+
|
|
212
241
|
document.body.appendChild(overlay);
|
|
213
242
|
|
|
214
243
|
window.removeEventListener('message', this._boundHandleMessage);
|
|
@@ -276,18 +305,26 @@ class Geophrase {
|
|
|
276
305
|
}
|
|
277
306
|
|
|
278
307
|
if (data?.type === 'GEOPHRASE_RESOLUTION_TOKEN') {
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
// Server mode: hand the raw token to the merchant for backend exchange
|
|
308
|
+
// Server mode: nothing to wait for — close immediately and hand
|
|
309
|
+
// the raw token to the merchant for backend exchange.
|
|
282
310
|
if (this.mode === 'server') {
|
|
311
|
+
this.close();
|
|
283
312
|
this._safeCall(this.onSuccess, { token: data.token });
|
|
284
313
|
return;
|
|
285
314
|
}
|
|
286
315
|
|
|
287
|
-
// Client mode:
|
|
316
|
+
// Client mode: keep the overlay + spinner visible while we exchange
|
|
317
|
+
// the token for the resolved address. Closing now would leave the
|
|
318
|
+
// merchant's page blank for the duration of the API round-trip.
|
|
319
|
+
iframe.style.opacity = '0';
|
|
320
|
+
|
|
288
321
|
const controller = new AbortController();
|
|
289
322
|
const timeoutId = setTimeout(() => controller.abort(), 15000);
|
|
290
323
|
|
|
324
|
+
// Build the callback to fire after we close the overlay, so the
|
|
325
|
+
// merchant sees the modal disappear before their handler runs.
|
|
326
|
+
let callback = null;
|
|
327
|
+
|
|
291
328
|
try {
|
|
292
329
|
const response = await fetch(`${this.apiBase}/business/resolve/`, {
|
|
293
330
|
method: 'POST',
|
|
@@ -301,20 +338,18 @@ class Geophrase {
|
|
|
301
338
|
|
|
302
339
|
if (!response.ok) {
|
|
303
340
|
const errorData = await response.json().catch(() => ({}));
|
|
304
|
-
this._safeCall(this.onError, {
|
|
341
|
+
callback = () => this._safeCall(this.onError, {
|
|
305
342
|
type: 'API_ERROR',
|
|
306
343
|
status: response.status,
|
|
307
344
|
message: errorData.message || 'Geophrase API resolution failed'
|
|
308
345
|
});
|
|
309
|
-
|
|
346
|
+
} else {
|
|
347
|
+
const responseData = await response.json();
|
|
348
|
+
callback = () => this._safeCall(this.onSuccess, responseData);
|
|
310
349
|
}
|
|
311
|
-
|
|
312
|
-
const responseData = await response.json();
|
|
313
|
-
this._safeCall(this.onSuccess, responseData);
|
|
314
|
-
|
|
315
350
|
} catch (error) {
|
|
316
351
|
console.error('Geophrase Resolution Error:', error);
|
|
317
|
-
this._safeCall(this.onError, {
|
|
352
|
+
callback = () => this._safeCall(this.onError, {
|
|
318
353
|
type: 'NETWORK_ERROR',
|
|
319
354
|
message: error.name === 'AbortError'
|
|
320
355
|
? 'Geophrase API request timed out'
|
|
@@ -322,6 +357,8 @@ class Geophrase {
|
|
|
322
357
|
});
|
|
323
358
|
} finally {
|
|
324
359
|
clearTimeout(timeoutId);
|
|
360
|
+
this.close();
|
|
361
|
+
callback?.();
|
|
325
362
|
}
|
|
326
363
|
}
|
|
327
364
|
}
|