@pradip1995/segment-jewelry-account-pages 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-account-pages",
3
- "version": "0.1.6",
3
+ "version": "0.1.7",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -23,8 +23,7 @@ import {
23
23
  EMAIL_OTP_ERRORS,
24
24
  PHONE_OTP_ERRORS,
25
25
  isValidEmail,
26
- normalizeEmail,
27
- phoneDigits,
26
+ isContactStatusVerified,
28
27
  resolveEmailOtpFlow,
29
28
  resolvePhoneOtpFlow,
30
29
  verifiedFieldAction,
@@ -150,15 +149,11 @@ export default function ModernAccountProfile({
150
149
  try {
151
150
  const fullName = String(formData.get("full_name") || "").trim()
152
151
  const [first_name, ...rest] = fullName.split(/\s+/)
153
- const e164 = normalizeIndianPhone(phone)
152
+ // Phone/email only change via OTP contact endpoints — never PATCH them here.
154
153
  await updateCustomer({
155
154
  first_name: first_name || "",
156
155
  last_name: rest.join(" ") || ".",
157
- phone: e164 || undefined,
158
156
  })
159
- if (e164 && phoneDigits(e164) !== phoneDigits(customer.phone || "")) {
160
- setPhoneVerified(false)
161
- }
162
157
  return { ok: true, error: null }
163
158
  } catch (err) {
164
159
  if (isNextRedirect(err)) throw err
@@ -189,30 +184,6 @@ export default function ModernAccountProfile({
189
184
  ready: emailReady,
190
185
  })
191
186
 
192
- async function ensurePhoneSavedForVerification(): Promise<boolean> {
193
- setPhoneError(null)
194
- if (!phoneReady) {
195
- setPhoneError(PHONE_OTP_ERRORS.empty)
196
- return false
197
- }
198
-
199
- const e164 = normalizeIndianPhone(phone)
200
- if (phoneDigits(e164) === phoneDigits(customer.phone || "")) {
201
- return true
202
- }
203
-
204
- try {
205
- await updateCustomer({ phone: e164 })
206
- return true
207
- } catch (err) {
208
- if (isNextRedirect(err)) throw err
209
- setPhoneError(
210
- safeErrorMessage(err, "Failed to save mobile number. Please try again.")
211
- )
212
- return false
213
- }
214
- }
215
-
216
187
  async function sendVerifyOtp(type: "email_verification" | "phone_verification") {
217
188
  setOtpType(type)
218
189
  setOtpPending(true)
@@ -230,14 +201,10 @@ export default function ModernAccountProfile({
230
201
  return
231
202
  }
232
203
  setOtpFlow(resolved.flow)
233
- let res
234
- if (resolved.flow === "change_contact") {
235
- res = await requestContactChange({ phone: resolved.phone })
236
- } else {
237
- const saved = await ensurePhoneSavedForVerification()
238
- if (!saved) return
239
- res = await sendRegistrationOtp(customer.id, type)
240
- }
204
+ const res =
205
+ resolved.flow === "change_contact"
206
+ ? await requestContactChange({ phone: resolved.phone })
207
+ : await sendRegistrationOtp(customer.id, type)
241
208
  if (!res.success) throw new Error(res.error || "Failed to send code")
242
209
  setOtpToken(res.token ?? null)
243
210
  setOtp("")
@@ -314,27 +281,38 @@ export default function ModernAccountProfile({
314
281
  function handlePhoneChange(value: string) {
315
282
  setPhone(value)
316
283
  setPhoneError(null)
317
- const next = normalizeIndianPhone(value)
318
- if (next && phoneDigits(next) !== phoneDigits(customer.phone || "")) {
319
- setPhoneVerified(false)
320
- } else {
321
- setPhoneVerified(readVerified(customer, "phone_verified"))
322
- }
284
+ // Any edit clears verified UI until OTP succeeds — never restore from server mid-edit.
285
+ setPhoneVerified(false)
323
286
  }
324
287
 
325
288
  function handleEmailChange(value: string) {
326
289
  setEmail(value)
327
290
  setEmailError(null)
328
- if (normalizeEmail(value) !== normalizeEmail(customer.email)) {
329
- setEmailVerified(false)
330
- } else {
331
- setEmailVerified(readVerified(customer, "email_verified"))
332
- }
291
+ setEmailVerified(false)
292
+ }
293
+
294
+ function startEditingPhone() {
295
+ setEditingPhone(true)
296
+ setPhoneVerified(false)
297
+ setPhoneError(null)
298
+ }
299
+
300
+ function startEditingEmail() {
301
+ setEditingEmail(true)
302
+ setEmailVerified(false)
303
+ setEmailError(null)
333
304
  }
334
305
 
335
306
  const fullName = [customer.first_name, customer.last_name].filter(Boolean).join(" ")
336
307
  const initials = getInitials(fullName || customer.email)
337
- const mobileVerified = phoneVerified && Boolean(customer.phone || phoneReady)
308
+ const mobileVerified = isContactStatusVerified({
309
+ verified: phoneVerified && Boolean(customer.phone || phoneReady),
310
+ editing: editingPhone,
311
+ })
312
+ const emailStatusVerified = isContactStatusVerified({
313
+ verified: emailVerified,
314
+ editing: editingEmail,
315
+ })
338
316
  const otpTarget =
339
317
  otpType === "email_verification" ? email || "email" : phone || "phone"
340
318
 
@@ -357,9 +335,9 @@ export default function ModernAccountProfile({
357
335
  {mobileVerified ? "Mobile Verified" : "Mobile Unverified"}
358
336
  </span>
359
337
 
360
- <span className={`modern-status-dot-pill ${emailVerified ? "is-verified" : "is-unverified"}`}>
338
+ <span className={`modern-status-dot-pill ${emailStatusVerified ? "is-verified" : "is-unverified"}`}>
361
339
  <span className="modern-status-dot" />
362
- {emailVerified ? "Email Verified" : "Email Unverified"}
340
+ {emailStatusVerified ? "Email Verified" : "Email Unverified"}
363
341
  </span>
364
342
  </div>
365
343
  </div>
@@ -408,7 +386,7 @@ export default function ModernAccountProfile({
408
386
  verifyAriaLabel="Verify mobile number"
409
387
  editAriaLabel="Edit mobile number"
410
388
  onVerify={() => sendVerifyOtp("phone_verification")}
411
- onEdit={() => setEditingPhone(true)}
389
+ onEdit={startEditingPhone}
412
390
  />
413
391
  </div>
414
392
  {phoneError ? (
@@ -448,9 +426,9 @@ export default function ModernAccountProfile({
448
426
  verifyLabel="Verify Email"
449
427
  verifyAriaLabel="Verify email"
450
428
  editAriaLabel="Edit email"
451
- onVerify={() => sendVerifyOtp("email_verification")}
452
- onEdit={() => setEditingEmail(true)}
453
- />
429
+ onVerify={() => sendVerifyOtp("email_verification")}
430
+ onEdit={startEditingEmail}
431
+ />
454
432
  </div>
455
433
  {emailError ? (
456
434
  <p role="alert" className="modern-field-hint modern-field-hint--error">
@@ -5,6 +5,7 @@ import {
5
5
  PHONE_OTP_ERRORS,
6
6
  isValidEmail,
7
7
  isVerifiedFieldLocked,
8
+ isContactStatusVerified,
8
9
  normalizeEmail,
9
10
  phoneDigits,
10
11
  resolveEmailOtpFlow,
@@ -67,11 +68,18 @@ describe("verifiedFieldAction", () => {
67
68
  want: "edit",
68
69
  },
69
70
  {
70
- name: "editing a verified contact keeps verified until the value changes",
71
+ name: "editing a previously verified contact requires verify again",
71
72
  verified: true,
72
73
  editing: true,
73
74
  ready: true,
74
- want: "verified",
75
+ want: "verify",
76
+ },
77
+ {
78
+ name: "editing with incomplete draft shows no action yet",
79
+ verified: true,
80
+ editing: true,
81
+ ready: false,
82
+ want: "none",
75
83
  },
76
84
  {
77
85
  name: "typed valid unverified contact shows verify",
@@ -102,6 +110,27 @@ describe("verifiedFieldAction", () => {
102
110
  }
103
111
  })
104
112
 
113
+ describe("isContactStatusVerified", () => {
114
+ const cases: Array<{
115
+ name: string
116
+ verified: boolean
117
+ editing: boolean
118
+ want: boolean
119
+ }> = [
120
+ { name: "verified and not editing is verified", verified: true, editing: false, want: true },
121
+ { name: "verified while editing is not verified", verified: true, editing: true, want: false },
122
+ { name: "unverified is never verified", verified: false, editing: false, want: false },
123
+ ]
124
+ for (const tc of cases) {
125
+ it(tc.name, () => {
126
+ assert.equal(
127
+ isContactStatusVerified({ verified: tc.verified, editing: tc.editing }),
128
+ tc.want
129
+ )
130
+ })
131
+ }
132
+ })
133
+
105
134
  describe("normalizeEmail", () => {
106
135
  const cases: Array<{ name: string; value: string | null | undefined; want: string }> = [
107
136
  { name: "trims and lowercases", value: " Jay@Empire.COM ", want: "jay@empire.com" },
@@ -29,12 +29,23 @@ export function verifiedFieldAction(input: {
29
29
  editing: boolean
30
30
  ready: boolean
31
31
  }): VerifiedFieldAction {
32
+ // Locked verified contact: show Edit. Once editing starts, never show
33
+ // Verified again until OTP succeeds — draft changes must re-verify.
32
34
  if (isVerifiedFieldLocked(input)) return "edit"
35
+ if (input.editing) return input.ready ? "verify" : "none"
33
36
  if (input.verified) return "verified"
34
37
  if (input.ready) return "verify"
35
38
  return "none"
36
39
  }
37
40
 
41
+ /** True only when server says verified and the user is not mid-edit. */
42
+ export function isContactStatusVerified(input: {
43
+ verified: boolean
44
+ editing: boolean
45
+ }): boolean {
46
+ return input.verified && !input.editing
47
+ }
48
+
38
49
  export function normalizeEmail(value: string | null | undefined): string {
39
50
  return (value || "").trim().toLowerCase()
40
51
  }