@usequeek/theme-kit 0.1.43 → 0.2.0
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/README.md +0 -1
- package/apps/apps.css +8 -0
- package/apps/follow-card.tsx +17 -5
- package/apps/subscribe-form.tsx +13 -2
- package/client.ts +0 -2
- package/hooks/use-auth-flow.tsx +14 -1
- package/locales/en.default.json +5 -0
- package/package.json +1 -1
- package/provider.tsx +5 -0
- package/shared-blocks/product-qa.tsx +7 -1
- package/hooks/use-address-autocomplete.ts +0 -71
- package/hooks/use-resolve-coordinates.ts +0 -57
package/README.md
CHANGED
|
@@ -135,7 +135,6 @@ they keep the data already on screen. They return `isLoading`, except
|
|
|
135
135
|
| `useAuth()` | `hooks/use-auth` | The signed-in customer and sign-in modal controls |
|
|
136
136
|
| `useVariantSelection(product, preferredVariantPId?)` | `hooks/use-variant-selection` | Option selection state and the add-to-cart gate; opens on a linked `?variant=` |
|
|
137
137
|
| `useSearch(query, products, collections)` | `hooks/use-search` | Instant search over data you hold |
|
|
138
|
-
| `useAddressAutocomplete(text)` / `useResolveCoordinates()` | `hooks/use-address-autocomplete`, `hooks/use-resolve-coordinates` | Delivery address lookup |
|
|
139
138
|
| `useHref()` | `hooks/use-href` | Builds a store-relative link |
|
|
140
139
|
| `useLocales()` | `hooks/use-locales` | The store's languages and the active one |
|
|
141
140
|
| `useStorefront()` / `useStorefrontLocale()` / `useThemeStrings()` | `provider` | The store, the request locale, the bound `t` |
|
package/apps/apps.css
CHANGED
|
@@ -148,6 +148,14 @@
|
|
|
148
148
|
line-height: 1.4;
|
|
149
149
|
}
|
|
150
150
|
|
|
151
|
+
/* Shown in a theme preview instead of sending the follow (nothing to follow). */
|
|
152
|
+
.core-follow-card__notice {
|
|
153
|
+
font-size: 0.75rem;
|
|
154
|
+
color: var(--brand-text-muted, #666);
|
|
155
|
+
margin: 0 0 0.5rem;
|
|
156
|
+
line-height: 1.4;
|
|
157
|
+
}
|
|
158
|
+
|
|
151
159
|
.core-follow-card__count {
|
|
152
160
|
font-size: 0.7rem;
|
|
153
161
|
color: var(--brand-text-muted, #999);
|
package/apps/follow-card.tsx
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import type { JSX } from 'react';
|
|
4
4
|
import { useState, useCallback, useEffect, useSyncExternalStore } from 'react';
|
|
5
|
-
import { useStorefront, useStorefrontLocale, useThemeStrings } from '../provider';
|
|
5
|
+
import { useIsThemePreview, useStorefront, useStorefrontLocale, useThemeStrings } from '../provider';
|
|
6
6
|
import { useUserStore } from '../stores/user-store';
|
|
7
7
|
import { useAuthModalStore } from '../stores/auth-modal-store';
|
|
8
8
|
import { getQueekClient } from '../sdk/queek-client';
|
|
@@ -84,6 +84,8 @@ function registerInlineMount(): () => void {
|
|
|
84
84
|
export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}): JSX.Element | null {
|
|
85
85
|
const { config, vendor } = useStorefront();
|
|
86
86
|
const t = useThemeStrings();
|
|
87
|
+
const isThemePreview = useIsThemePreview();
|
|
88
|
+
const [previewMessage, setPreviewMessage] = useState(false);
|
|
87
89
|
const locale = useStorefrontLocale();
|
|
88
90
|
const followConfig = config.apps?.follow_card;
|
|
89
91
|
const mount = display === 'inline' ? 'inline' : 'floating';
|
|
@@ -107,7 +109,7 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
|
|
|
107
109
|
// Check follow status when user is logged in
|
|
108
110
|
useEffect(() => {
|
|
109
111
|
if (!shows) return;
|
|
110
|
-
if (!user || !vendor.slug) {
|
|
112
|
+
if (isThemePreview || !user || !vendor.slug) {
|
|
111
113
|
setChecked(true);
|
|
112
114
|
return;
|
|
113
115
|
}
|
|
@@ -119,7 +121,7 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
|
|
|
119
121
|
})
|
|
120
122
|
.catch(() => {})
|
|
121
123
|
.finally(() => setChecked(true));
|
|
122
|
-
}, [user, vendor.slug, shows]);
|
|
124
|
+
}, [user, vendor.slug, shows, isThemePreview]);
|
|
123
125
|
|
|
124
126
|
// Check persistent dismiss
|
|
125
127
|
useEffect(() => {
|
|
@@ -129,6 +131,10 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
|
|
|
129
131
|
}, [vendor.id]);
|
|
130
132
|
|
|
131
133
|
const handleFollow = useCallback(async () => {
|
|
134
|
+
if (isThemePreview) {
|
|
135
|
+
setPreviewMessage(true);
|
|
136
|
+
return;
|
|
137
|
+
}
|
|
132
138
|
if (!user) {
|
|
133
139
|
openAuth('login');
|
|
134
140
|
return;
|
|
@@ -148,9 +154,13 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
|
|
|
148
154
|
} finally {
|
|
149
155
|
setLoading(false);
|
|
150
156
|
}
|
|
151
|
-
}, [user, vendor.slug, openAuth, shareContact]);
|
|
157
|
+
}, [user, vendor.slug, openAuth, shareContact, isThemePreview]);
|
|
152
158
|
|
|
153
159
|
const handleUnfollow = useCallback(async () => {
|
|
160
|
+
if (isThemePreview) {
|
|
161
|
+
setPreviewMessage(true);
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
154
164
|
if (!user) return;
|
|
155
165
|
|
|
156
166
|
setLoading(true);
|
|
@@ -164,7 +174,7 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
|
|
|
164
174
|
} finally {
|
|
165
175
|
setLoading(false);
|
|
166
176
|
}
|
|
167
|
-
}, [user, vendor.slug]);
|
|
177
|
+
}, [user, vendor.slug, isThemePreview]);
|
|
168
178
|
|
|
169
179
|
const handleDismiss = useCallback(() => {
|
|
170
180
|
setDismissed(true);
|
|
@@ -184,6 +194,7 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
|
|
|
184
194
|
if (isFollowing) {
|
|
185
195
|
return (
|
|
186
196
|
<div className={`core-follow-card core-follow-card--compact ${placement}`} role="complementary" aria-label={t('follow.following')}>
|
|
197
|
+
{previewMessage && <p className="core-follow-card__notice" role="status">{t('preview.action')}</p>}
|
|
187
198
|
<span className="core-follow-card__state">{t('follow.following')}</span>
|
|
188
199
|
<button type="button" className="core-follow-card__unfollow" onClick={handleUnfollow} disabled={loading}>
|
|
189
200
|
{loading ? t('follow.leaving') : t('follow.unfollow')}
|
|
@@ -214,6 +225,7 @@ export function FollowCard({ display }: { display?: 'floating' | 'inline' } = {}
|
|
|
214
225
|
</button>
|
|
215
226
|
)}
|
|
216
227
|
|
|
228
|
+
{previewMessage && <p className="core-follow-card__notice" role="status">{t('preview.action')}</p>}
|
|
217
229
|
<div className="core-follow-card__body">
|
|
218
230
|
{vendor.logo && (
|
|
219
231
|
<img
|
package/apps/subscribe-form.tsx
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
import type { JSX } from 'react';
|
|
4
4
|
import { useCallback, useId, useState } from 'react';
|
|
5
|
-
import { useStorefront } from '../provider';
|
|
5
|
+
import { useIsThemePreview, useStorefront, useThemeStrings } from '../provider';
|
|
6
6
|
import { getQueekClient } from '../sdk/queek-client';
|
|
7
7
|
import type { SubscribeFormProps } from '../types/theme';
|
|
8
8
|
|
|
@@ -33,6 +33,8 @@ export function SubscribeForm({
|
|
|
33
33
|
onSuccess,
|
|
34
34
|
}: SubscribeFormProps & { layout?: 'stacked' | 'inline'; onSuccess?: () => void } = {}): JSX.Element {
|
|
35
35
|
const { vendor } = useStorefront();
|
|
36
|
+
const isThemePreview = useIsThemePreview();
|
|
37
|
+
const t = useThemeStrings();
|
|
36
38
|
const emailId = useId();
|
|
37
39
|
const nameId = useId();
|
|
38
40
|
|
|
@@ -46,6 +48,15 @@ export function SubscribeForm({
|
|
|
46
48
|
event.preventDefault();
|
|
47
49
|
if (status === 'saving' || email.trim() === '') return;
|
|
48
50
|
|
|
51
|
+
if (isThemePreview) {
|
|
52
|
+
setStatus('done');
|
|
53
|
+
setMessage(t('preview.action'));
|
|
54
|
+
setName('');
|
|
55
|
+
setEmail('');
|
|
56
|
+
onSuccess?.();
|
|
57
|
+
return;
|
|
58
|
+
}
|
|
59
|
+
|
|
49
60
|
setStatus('saving');
|
|
50
61
|
try {
|
|
51
62
|
const res = await getQueekClient(vendor.slug ?? undefined).post<{ message?: string }>(
|
|
@@ -62,7 +73,7 @@ export function SubscribeForm({
|
|
|
62
73
|
setMessage('That didn’t go through. Please check the address and try again.');
|
|
63
74
|
}
|
|
64
75
|
},
|
|
65
|
-
[name, email, status, vendor.slug, onSuccess],
|
|
76
|
+
[name, email, status, vendor.slug, onSuccess, isThemePreview, t],
|
|
66
77
|
);
|
|
67
78
|
|
|
68
79
|
if (status === 'done') {
|
package/client.ts
CHANGED
|
@@ -52,8 +52,6 @@ export * from './hooks/use-search';
|
|
|
52
52
|
export * from './hooks/use-posts';
|
|
53
53
|
export * from './hooks/use-reviews';
|
|
54
54
|
export * from './hooks/use-variant-selection';
|
|
55
|
-
export * from './hooks/use-address-autocomplete';
|
|
56
|
-
export * from './hooks/use-resolve-coordinates';
|
|
57
55
|
export * from './stores/cart-panel-store';
|
|
58
56
|
export * from './stores/product-modal-store';
|
|
59
57
|
export * from './stores/search-modal-store';
|
package/hooks/use-auth-flow.tsx
CHANGED
|
@@ -3,7 +3,7 @@
|
|
|
3
3
|
import type { JSX } from 'react';
|
|
4
4
|
import { createContext, useContext, useState, type ReactNode } from 'react';
|
|
5
5
|
import { QueekSdkError, type EmailVerifyOtpResponse } from '@usequeek/client-sdk';
|
|
6
|
-
import { useStorefront, useThemeStrings } from '../provider';
|
|
6
|
+
import { useIsThemePreview, useStorefront, useThemeStrings } from '../provider';
|
|
7
7
|
import { getQueekClient, setAuthTokens, QUEEK_AUTH_URL } from '../sdk/queek-client';
|
|
8
8
|
import { useAuthModalStore } from '../stores/auth-modal-store';
|
|
9
9
|
import { useUserStore } from '../stores/user-store';
|
|
@@ -90,6 +90,7 @@ const AuthFlowContext = createContext<AuthFlowValue | null>(null);
|
|
|
90
90
|
export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Element {
|
|
91
91
|
const { vendor } = useStorefront();
|
|
92
92
|
const t = useThemeStrings();
|
|
93
|
+
const isThemePreview = useIsThemePreview();
|
|
93
94
|
const closeModal = useAuthModalStore((s) => s.close);
|
|
94
95
|
const setUser = useUserStore((s) => s.setUser);
|
|
95
96
|
|
|
@@ -103,6 +104,12 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
103
104
|
const [loading, setLoading] = useState(false);
|
|
104
105
|
const [error, setError] = useState<string | null>(null);
|
|
105
106
|
|
|
107
|
+
function blockPreview(): boolean {
|
|
108
|
+
if (!isThemePreview) return false;
|
|
109
|
+
setError(t('auth.error.preview'));
|
|
110
|
+
return true;
|
|
111
|
+
}
|
|
112
|
+
|
|
106
113
|
function applyUser(u: { id: string; name: string; first_name?: string | null; last_name?: string | null; email: string | null; phone?: string | null; avatar: string | null; profile_complete?: boolean }): void {
|
|
107
114
|
setUser({
|
|
108
115
|
id: u.id,
|
|
@@ -122,6 +129,7 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
122
129
|
// ── Email OTP flow (primary) ──────────────────────────────────────────────
|
|
123
130
|
|
|
124
131
|
const submitEmailRequestOtp = async (): Promise<void> => {
|
|
132
|
+
if (blockPreview()) return;
|
|
125
133
|
const trimmedEmail = email.trim();
|
|
126
134
|
if (!trimmedEmail) return;
|
|
127
135
|
setError(null);
|
|
@@ -139,6 +147,7 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
139
147
|
};
|
|
140
148
|
|
|
141
149
|
const submitEmailVerifyOtp = async (): Promise<void> => {
|
|
150
|
+
if (blockPreview()) return;
|
|
142
151
|
if (otp.length < EMAIL_OTP_LENGTH) return;
|
|
143
152
|
setError(null);
|
|
144
153
|
setLoading(true);
|
|
@@ -167,6 +176,7 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
167
176
|
// ── Signup (email OTP flow) ───────────────────────────────────────────────
|
|
168
177
|
|
|
169
178
|
const submitRegister = async (): Promise<void> => {
|
|
179
|
+
if (blockPreview()) return;
|
|
170
180
|
if (!firstName.trim()) {
|
|
171
181
|
setError(t('auth.error.firstname'));
|
|
172
182
|
return;
|
|
@@ -193,6 +203,7 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
193
203
|
// ── Email password flow ───────────────────────────────────────────────────
|
|
194
204
|
|
|
195
205
|
const submitEmailLogin = async (): Promise<void> => {
|
|
206
|
+
if (blockPreview()) return;
|
|
196
207
|
if (!email.trim() || !password) return;
|
|
197
208
|
setError(null);
|
|
198
209
|
setLoading(true);
|
|
@@ -214,6 +225,7 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
214
225
|
};
|
|
215
226
|
|
|
216
227
|
const submitEmailRegister = async (): Promise<void> => {
|
|
228
|
+
if (blockPreview()) return;
|
|
217
229
|
if (!firstName.trim() || !email.trim() || password.length < 8) return;
|
|
218
230
|
setError(null);
|
|
219
231
|
setLoading(true);
|
|
@@ -240,6 +252,7 @@ export function AuthFlowProvider({ children }: { children: ReactNode }): JSX.Ele
|
|
|
240
252
|
// ── Google OAuth ──────────────────────────────────────────────────────────
|
|
241
253
|
|
|
242
254
|
const initiateGoogleAuth = async (): Promise<void> => {
|
|
255
|
+
if (blockPreview()) return;
|
|
243
256
|
setError(null);
|
|
244
257
|
setLoading(true);
|
|
245
258
|
try {
|
package/locales/en.default.json
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
1
|
{
|
|
2
|
+
"preview": {
|
|
3
|
+
"action": "This is a theme preview, so nothing was sent.",
|
|
4
|
+
"address": "Address lookup isn't available in a theme preview."
|
|
5
|
+
},
|
|
2
6
|
"cart": {
|
|
3
7
|
"title": "Your cart",
|
|
4
8
|
"empty": {
|
|
@@ -132,6 +136,7 @@
|
|
|
132
136
|
"different": "← Use a different email"
|
|
133
137
|
},
|
|
134
138
|
"error": {
|
|
139
|
+
"preview": "Signing in isn't available in a theme preview.",
|
|
135
140
|
"credentials": "Wrong email or password. Please try again.",
|
|
136
141
|
"method": "This account uses a different sign-in method (Google or phone).",
|
|
137
142
|
"exists": "Email already registered. Please sign in instead.",
|
package/package.json
CHANGED
package/provider.tsx
CHANGED
|
@@ -218,6 +218,11 @@ export function useStorefront(): StorefrontContextValue {
|
|
|
218
218
|
return context;
|
|
219
219
|
}
|
|
220
220
|
|
|
221
|
+
/** Whether this storefront is a theme demo with no backing store. */
|
|
222
|
+
export function useIsThemePreview(): boolean {
|
|
223
|
+
return useStorefront().previewData !== undefined;
|
|
224
|
+
}
|
|
225
|
+
|
|
221
226
|
/**
|
|
222
227
|
* Validated request locale for translated reads (`null` = primary).
|
|
223
228
|
* Browser hooks send `?locale=` when this is set and key their client
|
|
@@ -6,7 +6,7 @@ import type { ProductQaBlockData, ProductQuestionItem } from '../types/block';
|
|
|
6
6
|
import { Image } from '../components/image';
|
|
7
7
|
import { useProductQuestions } from '../hooks/use-product-questions';
|
|
8
8
|
import { useAuth } from '../hooks/use-auth';
|
|
9
|
-
import { useStorefront } from '../provider';
|
|
9
|
+
import { useIsThemePreview, useStorefront, useThemeStrings } from '../provider';
|
|
10
10
|
import { getQueekClient } from '../sdk/queek-client';
|
|
11
11
|
import { formatDisplayDate } from '../utils/format';
|
|
12
12
|
|
|
@@ -47,6 +47,8 @@ export function CoreProductQaBlock(props: ProductQaBlockData): JSX.Element {
|
|
|
47
47
|
|
|
48
48
|
function AskQuestionPanel({ productSlug }: { productSlug: string }): JSX.Element {
|
|
49
49
|
const { vendor } = useStorefront();
|
|
50
|
+
const isThemePreview = useIsThemePreview();
|
|
51
|
+
const t = useThemeStrings();
|
|
50
52
|
const { user, openAuthModal } = useAuth();
|
|
51
53
|
const [open, setOpen] = useState(false);
|
|
52
54
|
const [question, setQuestion] = useState('');
|
|
@@ -70,6 +72,10 @@ function AskQuestionPanel({ productSlug }: { productSlug: string }): JSX.Element
|
|
|
70
72
|
};
|
|
71
73
|
|
|
72
74
|
const handleSubmit = async (): Promise<void> => {
|
|
75
|
+
if (isThemePreview) {
|
|
76
|
+
setError(t('preview.action'));
|
|
77
|
+
return;
|
|
78
|
+
}
|
|
73
79
|
const trimmed = question.trim();
|
|
74
80
|
if (!trimmed) {
|
|
75
81
|
setError('Please write a question.');
|
|
@@ -1,71 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useEffect, useState } from 'react';
|
|
4
|
-
import { useStorefront } from '../provider';
|
|
5
|
-
import { getQueekClient } from '../sdk/queek-client';
|
|
6
|
-
|
|
7
|
-
/** One address suggestion. Pass `place_id` to `useResolveCoordinates` to get its position. */
|
|
8
|
-
export interface AddressSuggestion {
|
|
9
|
-
description: string;
|
|
10
|
-
place_id: string;
|
|
11
|
-
}
|
|
12
|
-
|
|
13
|
-
const DEBOUNCE_MS = 300;
|
|
14
|
-
|
|
15
|
-
/**
|
|
16
|
-
* Address suggestions for what the shopper has typed, after a 300 ms pause.
|
|
17
|
-
* Nothing is requested for fewer than two characters. A failed request clears
|
|
18
|
-
* the suggestions rather than throwing, so the field stays usable.
|
|
19
|
-
*
|
|
20
|
-
* @example
|
|
21
|
-
* const { suggestions, loading } = useAddressAutocomplete(text);
|
|
22
|
-
*/
|
|
23
|
-
export function useAddressAutocomplete(query: string): {
|
|
24
|
-
suggestions: AddressSuggestion[];
|
|
25
|
-
loading: boolean;
|
|
26
|
-
} {
|
|
27
|
-
const { vendor } = useStorefront();
|
|
28
|
-
const [suggestions, setSuggestions] = useState<AddressSuggestion[]>([]);
|
|
29
|
-
const [loading, setLoading] = useState(false);
|
|
30
|
-
|
|
31
|
-
useEffect(() => {
|
|
32
|
-
if (!query || query.trim().length < 2) {
|
|
33
|
-
setSuggestions([]);
|
|
34
|
-
return;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
let cancelled = false;
|
|
38
|
-
const timer = setTimeout(() => {
|
|
39
|
-
setLoading(true);
|
|
40
|
-
const client = getQueekClient(vendor.slug ?? undefined);
|
|
41
|
-
const path = `/client/address/autocomplete?query=${encodeURIComponent(query.trim())}`;
|
|
42
|
-
|
|
43
|
-
client
|
|
44
|
-
.get<AddressSuggestion[] | { data: AddressSuggestion[] }>(path)
|
|
45
|
-
.then((res) => {
|
|
46
|
-
if (cancelled) return;
|
|
47
|
-
// Backend returns a bare array (not wrapped in envelope). SDK returns
|
|
48
|
-
// whatever the server sent — handle both shapes defensively.
|
|
49
|
-
const list: unknown = Array.isArray(res)
|
|
50
|
-
? res
|
|
51
|
-
: Array.isArray((res as { data?: unknown })?.data)
|
|
52
|
-
? (res as { data: AddressSuggestion[] }).data
|
|
53
|
-
: [];
|
|
54
|
-
setSuggestions(list as AddressSuggestion[]);
|
|
55
|
-
})
|
|
56
|
-
.catch(() => {
|
|
57
|
-
if (!cancelled) setSuggestions([]);
|
|
58
|
-
})
|
|
59
|
-
.finally(() => {
|
|
60
|
-
if (!cancelled) setLoading(false);
|
|
61
|
-
});
|
|
62
|
-
}, DEBOUNCE_MS);
|
|
63
|
-
|
|
64
|
-
return () => {
|
|
65
|
-
cancelled = true;
|
|
66
|
-
clearTimeout(timer);
|
|
67
|
-
};
|
|
68
|
-
}, [query, vendor.slug]);
|
|
69
|
-
|
|
70
|
-
return { suggestions, loading };
|
|
71
|
-
}
|
|
@@ -1,57 +0,0 @@
|
|
|
1
|
-
'use client';
|
|
2
|
-
|
|
3
|
-
import { useState } from 'react';
|
|
4
|
-
import { useStorefront } from '../provider';
|
|
5
|
-
import { getQueekClient } from '../sdk/queek-client';
|
|
6
|
-
import { useLocationStore } from '../stores/location-store';
|
|
7
|
-
|
|
8
|
-
interface CoordinatesResponse {
|
|
9
|
-
lat?: number;
|
|
10
|
-
lng?: number;
|
|
11
|
-
latitude?: number;
|
|
12
|
-
longitude?: number;
|
|
13
|
-
formatted_address?: string;
|
|
14
|
-
state_id?: string | null;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
/**
|
|
18
|
-
* Turns a chosen address suggestion into coordinates and saves it as the
|
|
19
|
-
* shopper's delivery location. `resolve` returns true when a location was
|
|
20
|
-
* saved and false when the API gave no usable position or the request failed.
|
|
21
|
-
*
|
|
22
|
-
* @example
|
|
23
|
-
* const { resolve, resolving } = useResolveCoordinates();
|
|
24
|
-
* const ok = await resolve(suggestion.place_id, suggestion.description);
|
|
25
|
-
*/
|
|
26
|
-
export function useResolveCoordinates(): {
|
|
27
|
-
resolve: (placeId: string, description: string) => Promise<boolean>;
|
|
28
|
-
resolving: boolean;
|
|
29
|
-
} {
|
|
30
|
-
const { vendor } = useStorefront();
|
|
31
|
-
const setLocation = useLocationStore((s) => s.setLocation);
|
|
32
|
-
const [resolving, setResolving] = useState(false);
|
|
33
|
-
|
|
34
|
-
const resolve = async (placeId: string, description: string): Promise<boolean> => {
|
|
35
|
-
setResolving(true);
|
|
36
|
-
try {
|
|
37
|
-
const client = getQueekClient(vendor.slug ?? undefined);
|
|
38
|
-
const res = await client.post<CoordinatesResponse>('/client/geo/resolve', {
|
|
39
|
-
place_id: placeId,
|
|
40
|
-
});
|
|
41
|
-
const data = (res?.data ?? {}) as CoordinatesResponse;
|
|
42
|
-
const lat = data.lat ?? data.latitude;
|
|
43
|
-
const lng = data.lng ?? data.longitude;
|
|
44
|
-
if (typeof lat === 'number' && typeof lng === 'number') {
|
|
45
|
-
setLocation(lat, lng, data.formatted_address ?? description, data.state_id ?? null);
|
|
46
|
-
return true;
|
|
47
|
-
}
|
|
48
|
-
return false;
|
|
49
|
-
} catch {
|
|
50
|
-
return false;
|
|
51
|
-
} finally {
|
|
52
|
-
setResolving(false);
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
|
|
56
|
-
return { resolve, resolving };
|
|
57
|
-
}
|