@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@pradip1995/segment-jewelry-checkout-form",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -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 (addresses.length) {
161
- const source = preferred || matched
162
- if (source) {
163
- return addressToFormValues(source, {
164
- email,
165
- phone: customer?.phone,
166
- countryCode: source.country_code || cart?.region?.countries?.[0]?.iso_2 || "in",
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
- const fullName = shipping?.first_name
172
- ? `${shipping.first_name} ${shipping.last_name === "." ? "" : shipping.last_name || ""}`.trim()
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: shipping?.first_name || "",
178
- lastName: shipping?.last_name || "",
210
+ fullName: "",
211
+ firstName: "",
212
+ lastName: "",
179
213
  email,
180
- phone: toFormPhoneValue(shipping?.phone || customer?.phone),
181
- address1: shipping?.address_1 || "",
182
- address2: shipping?.address_2 || "",
183
- postalCode: shipping?.postal_code || "",
184
- city: shipping?.city || "",
185
- province: shipping?.province || "",
186
- countryCode: shipping?.country_code || cart?.region?.countries?.[0]?.iso_2 || "in",
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
- return NEW_ADDRESS_ID
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: {