@pradip1995/segment-jewelry-checkout-form 0.1.6 → 0.1.7
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/address-fields.tsx +63 -24
package/package.json
CHANGED
package/src/address-fields.tsx
CHANGED
|
@@ -108,8 +108,7 @@ function pickPreferredFromList(
|
|
|
108
108
|
addresses.find((a) => a.is_default_billing) ||
|
|
109
109
|
addresses.find(
|
|
110
110
|
(a) => a.metadata?.is_default === true || a.metadata?.is_default === "true"
|
|
111
|
-
)
|
|
112
|
-
addresses[0]
|
|
111
|
+
)
|
|
113
112
|
)
|
|
114
113
|
}
|
|
115
114
|
|
|
@@ -156,34 +155,69 @@ function buildInitialValues(
|
|
|
156
155
|
const matched = findMatchingSavedAddress(cart, addresses)
|
|
157
156
|
const preferred = pickPreferredAddress(customer)
|
|
158
157
|
const shipping = cart?.shipping_address
|
|
158
|
+
const fallbackCountry =
|
|
159
|
+
preferred?.country_code ||
|
|
160
|
+
matched?.country_code ||
|
|
161
|
+
shipping?.country_code ||
|
|
162
|
+
cart?.region?.countries?.[0]?.iso_2 ||
|
|
163
|
+
"in"
|
|
164
|
+
|
|
165
|
+
if (preferred) {
|
|
166
|
+
return addressToFormValues(preferred, {
|
|
167
|
+
email,
|
|
168
|
+
phone: customer?.phone,
|
|
169
|
+
countryCode: fallbackCountry,
|
|
170
|
+
})
|
|
171
|
+
}
|
|
159
172
|
|
|
160
|
-
if (
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
173
|
+
if (matched) {
|
|
174
|
+
return addressToFormValues(matched, {
|
|
175
|
+
email,
|
|
176
|
+
phone: customer?.phone,
|
|
177
|
+
countryCode: fallbackCountry,
|
|
178
|
+
})
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (shipping?.address_1 || shipping?.postal_code) {
|
|
182
|
+
const fullName = shipping?.first_name
|
|
183
|
+
? `${shipping.first_name} ${shipping.last_name === "." ? "" : shipping.last_name || ""}`.trim()
|
|
184
|
+
: ""
|
|
185
|
+
|
|
186
|
+
return {
|
|
187
|
+
fullName,
|
|
188
|
+
firstName: shipping?.first_name || "",
|
|
189
|
+
lastName: shipping?.last_name || "",
|
|
190
|
+
email,
|
|
191
|
+
phone: toFormPhoneValue(shipping?.phone || customer?.phone),
|
|
192
|
+
address1: shipping?.address_1 || "",
|
|
193
|
+
address2: shipping?.address_2 || "",
|
|
194
|
+
postalCode: shipping?.postal_code || "",
|
|
195
|
+
city: shipping?.city || "",
|
|
196
|
+
province: shipping?.province || "",
|
|
197
|
+
countryCode: fallbackCountry,
|
|
168
198
|
}
|
|
169
199
|
}
|
|
170
200
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
201
|
+
if (addresses[0]) {
|
|
202
|
+
return addressToFormValues(addresses[0], {
|
|
203
|
+
email,
|
|
204
|
+
phone: customer?.phone,
|
|
205
|
+
countryCode: fallbackCountry,
|
|
206
|
+
})
|
|
207
|
+
}
|
|
174
208
|
|
|
175
209
|
return {
|
|
176
|
-
fullName,
|
|
177
|
-
firstName:
|
|
178
|
-
lastName:
|
|
210
|
+
fullName: "",
|
|
211
|
+
firstName: "",
|
|
212
|
+
lastName: "",
|
|
179
213
|
email,
|
|
180
|
-
phone: toFormPhoneValue(
|
|
181
|
-
address1:
|
|
182
|
-
address2:
|
|
183
|
-
postalCode:
|
|
184
|
-
city:
|
|
185
|
-
province:
|
|
186
|
-
countryCode:
|
|
214
|
+
phone: toFormPhoneValue(customer?.phone),
|
|
215
|
+
address1: "",
|
|
216
|
+
address2: "",
|
|
217
|
+
postalCode: "",
|
|
218
|
+
city: "",
|
|
219
|
+
province: "",
|
|
220
|
+
countryCode: fallbackCountry,
|
|
187
221
|
}
|
|
188
222
|
}
|
|
189
223
|
|
|
@@ -200,7 +234,12 @@ function resolveInitialSelectedId(
|
|
|
200
234
|
const matched = findMatchingSavedAddress(cart, addresses)
|
|
201
235
|
if (matched?.id) return matched.id
|
|
202
236
|
|
|
203
|
-
|
|
237
|
+
// Cart already has an address (e.g. reorder from a past order) — don't force another saved one.
|
|
238
|
+
if (cart?.shipping_address?.address_1 || cart?.shipping_address?.postal_code) {
|
|
239
|
+
return NEW_ADDRESS_ID
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
return addresses[0]?.id || NEW_ADDRESS_ID
|
|
204
243
|
}
|
|
205
244
|
|
|
206
245
|
function formatAddressParts(address: {
|