@pradip1995/segment-jewelry-checkout-form 0.1.5 → 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.5",
3
+ "version": "0.1.7",
4
4
  "license": "MIT",
5
5
  "publishConfig": {
6
6
  "access": "public"
@@ -14,7 +14,8 @@
14
14
  "exports": {
15
15
  ".": "./src/index.ts",
16
16
  "./manifest": "./src/manifest.ts",
17
- "./payment-block": "./src/payment-block.tsx"
17
+ "./payment-block": "./src/payment-block.tsx",
18
+ "./checkout-line": "./src/checkout-line.ts"
18
19
  },
19
20
  "peerDependencies": {
20
21
  "@pradip1995/commerce-core": "^4.0.0",
@@ -37,6 +38,7 @@
37
38
  },
38
39
  "scripts": {
39
40
  "typecheck": "tsc --noEmit",
40
- "lint": "tsc --noEmit"
41
+ "lint": "tsc --noEmit",
42
+ "test": "node --experimental-strip-types --test src/*.test.ts"
41
43
  }
42
44
  }
@@ -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: {
@@ -0,0 +1,69 @@
1
+ "use client"
2
+
3
+ import Image from "next/image"
4
+ import type { HttpTypes } from "@medusajs/types"
5
+ import { formatPrice } from "@pradip1995/segment-primitives/format-price"
6
+ import { resolveCartLineItemImage } from "@pradip1995/segment-primitives/resolve-media-url"
7
+ import { checkoutItemCount, checkoutLineView } from "./checkout-line"
8
+
9
+ export default function CheckoutBagItems({
10
+ cart,
11
+ title = "Your items",
12
+ }: {
13
+ cart: HttpTypes.StoreCart
14
+ title?: string
15
+ }) {
16
+ const items = cart.items || []
17
+ const currency = cart.currency_code || "inr"
18
+ const itemCount = checkoutItemCount(items)
19
+
20
+ if (items.length === 0) return null
21
+
22
+ return (
23
+ <section className="jewelry-checkout-bag" aria-label={title}>
24
+ <header className="jewelry-checkout-bag__head">
25
+ <h2 className="jewelry-checkout-bag__title">{title}</h2>
26
+ <p className="jewelry-checkout-bag__count">
27
+ {itemCount} {itemCount === 1 ? "item" : "items"}
28
+ </p>
29
+ </header>
30
+
31
+ <ul className="jewelry-checkout-bag__list">
32
+ {items.map((item) => {
33
+ const line = checkoutLineView(item)
34
+ const thumbnail = resolveCartLineItemImage(item)
35
+ return (
36
+ <li key={item.id} className="jewelry-checkout-bag__item">
37
+ <div className="jewelry-checkout-bag__thumb">
38
+ {thumbnail ? (
39
+ <Image
40
+ src={thumbnail}
41
+ alt={line.title}
42
+ fill
43
+ className="object-cover"
44
+ sizes="88px"
45
+ unoptimized
46
+ />
47
+ ) : (
48
+ <div className="jewelry-checkout-bag__thumb-empty" aria-hidden />
49
+ )}
50
+ </div>
51
+ <div className="jewelry-checkout-bag__meta">
52
+ <p className="jewelry-checkout-bag__name">{line.title}</p>
53
+ {line.variantLabel ? (
54
+ <p className="jewelry-checkout-bag__variant">{line.variantLabel}</p>
55
+ ) : null}
56
+ <p className="jewelry-checkout-bag__qty">
57
+ {line.quantityLabel}
58
+ <span aria-hidden> · </span>
59
+ {formatPrice(line.unitPrice, currency)} each
60
+ </p>
61
+ </div>
62
+ <p className="jewelry-checkout-bag__total">{formatPrice(line.lineTotal, currency)}</p>
63
+ </li>
64
+ )
65
+ })}
66
+ </ul>
67
+ </section>
68
+ )
69
+ }
@@ -0,0 +1,147 @@
1
+ import assert from "node:assert/strict"
2
+ import { describe, it } from "node:test"
3
+ import {
4
+ checkoutItemCount,
5
+ checkoutLineView,
6
+ isPlaceholderVariant,
7
+ type CheckoutLineItemInput,
8
+ type CheckoutLineView,
9
+ } from "./checkout-line.ts"
10
+
11
+ const LINE_CASES: Array<{
12
+ name: string
13
+ item: CheckoutLineItemInput
14
+ want: CheckoutLineView
15
+ }> = [
16
+ {
17
+ name: "shows title, qty, and line total for a single piece",
18
+ item: {
19
+ id: "li_1",
20
+ title: "Pendants demo 3",
21
+ quantity: 1,
22
+ unit_price: 869,
23
+ total: 869,
24
+ },
25
+ want: {
26
+ id: "li_1",
27
+ title: "Pendants demo 3",
28
+ variantLabel: null,
29
+ quantity: 1,
30
+ quantityLabel: "Qty 1",
31
+ unitPrice: 869,
32
+ lineTotal: 869,
33
+ },
34
+ },
35
+ {
36
+ name: "multiplies unit price when line total is missing",
37
+ item: {
38
+ id: "li_2",
39
+ title: "Gold hoop",
40
+ quantity: 3,
41
+ unit_price: 500,
42
+ },
43
+ want: {
44
+ id: "li_2",
45
+ title: "Gold hoop",
46
+ variantLabel: null,
47
+ quantity: 3,
48
+ quantityLabel: "Qty 3",
49
+ unitPrice: 500,
50
+ lineTotal: 1500,
51
+ },
52
+ },
53
+ {
54
+ name: "prefers product title and keeps a real variant label",
55
+ item: {
56
+ id: "li_3",
57
+ title: "Variant name",
58
+ product_title: "Emerald ring",
59
+ quantity: 2,
60
+ unit_price: 1200,
61
+ total: 2400,
62
+ variant: { title: "18K Gold / 6" },
63
+ },
64
+ want: {
65
+ id: "li_3",
66
+ title: "Emerald ring",
67
+ variantLabel: "18K Gold / 6",
68
+ quantity: 2,
69
+ quantityLabel: "Qty 2",
70
+ unitPrice: 1200,
71
+ lineTotal: 2400,
72
+ },
73
+ },
74
+ {
75
+ name: "hides a default variant label",
76
+ item: {
77
+ id: "li_4",
78
+ title: "Pendants demo 2",
79
+ quantity: 1,
80
+ unit_price: 846,
81
+ total: 846,
82
+ variant: { title: "Default" },
83
+ },
84
+ want: {
85
+ id: "li_4",
86
+ title: "Pendants demo 2",
87
+ variantLabel: null,
88
+ quantity: 1,
89
+ quantityLabel: "Qty 1",
90
+ unitPrice: 846,
91
+ lineTotal: 846,
92
+ },
93
+ },
94
+ {
95
+ name: "treats missing quantity as one and falls back to Product",
96
+ item: { id: "", title: " " },
97
+ want: {
98
+ id: "",
99
+ title: "Product",
100
+ variantLabel: null,
101
+ quantity: 1,
102
+ quantityLabel: "Qty 1",
103
+ unitPrice: 0,
104
+ lineTotal: 0,
105
+ },
106
+ },
107
+ ]
108
+
109
+ const COUNT_CASES: Array<{
110
+ name: string
111
+ items: Array<{ quantity?: number | null }> | null | undefined
112
+ want: number
113
+ }> = [
114
+ { name: "counts mixed quantities", items: [{ quantity: 1 }, { quantity: 2 }], want: 3 },
115
+ { name: "returns zero for an empty list", items: [], want: 0 },
116
+ { name: "returns zero when items are missing", items: null, want: 0 },
117
+ ]
118
+
119
+ const VARIANT_CASES: Array<{ name: string; title: string | null; want: boolean }> = [
120
+ { name: "treats null as a placeholder variant", title: null, want: true },
121
+ { name: "treats Default as a placeholder variant", title: "Default", want: true },
122
+ { name: "keeps a real size variant", title: "Size 6", want: false },
123
+ ]
124
+
125
+ describe("checkoutLineView", () => {
126
+ for (const row of LINE_CASES) {
127
+ it(row.name, () => {
128
+ assert.deepEqual(checkoutLineView(row.item), row.want)
129
+ })
130
+ }
131
+ })
132
+
133
+ describe("checkoutItemCount", () => {
134
+ for (const row of COUNT_CASES) {
135
+ it(row.name, () => {
136
+ assert.equal(checkoutItemCount(row.items), row.want)
137
+ })
138
+ }
139
+ })
140
+
141
+ describe("isPlaceholderVariant", () => {
142
+ for (const row of VARIANT_CASES) {
143
+ it(row.name, () => {
144
+ assert.equal(isPlaceholderVariant(row.title), row.want)
145
+ })
146
+ }
147
+ })
@@ -0,0 +1,58 @@
1
+ export type CheckoutLineItemInput = {
2
+ id?: string | null
3
+ title?: string | null
4
+ product_title?: string | null
5
+ quantity?: number | null
6
+ unit_price?: number | null
7
+ total?: number | null
8
+ variant?: { title?: string | null } | null
9
+ }
10
+
11
+ export type CheckoutLineView = {
12
+ id: string
13
+ title: string
14
+ variantLabel: string | null
15
+ quantity: number
16
+ quantityLabel: string
17
+ unitPrice: number
18
+ lineTotal: number
19
+ }
20
+
21
+ export function isPlaceholderVariant(title?: string | null): boolean {
22
+ if (!title) return true
23
+ const normalized = title.toLowerCase()
24
+ return (
25
+ normalized === "default" ||
26
+ normalized.includes("default variant") ||
27
+ normalized.includes("default option")
28
+ )
29
+ }
30
+
31
+ export function checkoutLineView(item: CheckoutLineItemInput): CheckoutLineView {
32
+ const quantity = Math.max(1, Number(item.quantity) || 1)
33
+ const unitPrice = Number(item.unit_price) || 0
34
+ const lineTotal =
35
+ typeof item.total === "number" ? item.total : unitPrice * quantity
36
+ const variantTitle = item.variant?.title
37
+ const variantLabel =
38
+ variantTitle && !isPlaceholderVariant(variantTitle) ? variantTitle : null
39
+
40
+ return {
41
+ id: item.id || "",
42
+ title: (item.product_title || item.title || "Product").trim() || "Product",
43
+ variantLabel,
44
+ quantity,
45
+ quantityLabel: `Qty ${quantity}`,
46
+ unitPrice,
47
+ lineTotal,
48
+ }
49
+ }
50
+
51
+ export function checkoutItemCount(
52
+ items: Array<{ quantity?: number | null }> | null | undefined
53
+ ): number {
54
+ return (items || []).reduce(
55
+ (sum, item) => sum + Math.max(0, Number(item.quantity) || 0),
56
+ 0
57
+ )
58
+ }
@@ -861,3 +861,136 @@
861
861
  grid-template-columns: 1fr;
862
862
  }
863
863
  }
864
+
865
+ .jewelry-checkout-bag {
866
+ margin-top: 2rem;
867
+ padding-top: 1.5rem;
868
+ border-top: 1px solid rgba(59, 31, 78, 0.12);
869
+ }
870
+
871
+ .jewelry-checkout-bag__head {
872
+ display: flex;
873
+ align-items: center;
874
+ justify-content: space-between;
875
+ gap: 1rem;
876
+ margin-bottom: 1rem;
877
+ }
878
+
879
+ .jewelry-checkout-bag__title {
880
+ margin: 0;
881
+ display: flex;
882
+ align-items: center;
883
+ gap: 0.6rem;
884
+ font-family: var(--font-sans, inherit);
885
+ font-size: 0.8125rem;
886
+ font-weight: 700;
887
+ letter-spacing: 0.14em;
888
+ text-transform: uppercase;
889
+ color: #1a1224;
890
+ }
891
+
892
+ .jewelry-checkout-bag__title::before {
893
+ content: "";
894
+ width: 3px;
895
+ height: 0.95em;
896
+ border-radius: 1px;
897
+ background: #3b1f4e;
898
+ }
899
+
900
+ .jewelry-checkout-bag__count {
901
+ margin: 0;
902
+ font-size: 0.8125rem;
903
+ font-weight: 600;
904
+ color: #6b6573;
905
+ }
906
+
907
+ .jewelry-checkout-bag__list {
908
+ display: flex;
909
+ flex-direction: column;
910
+ gap: 0.85rem;
911
+ margin: 0;
912
+ padding: 0;
913
+ list-style: none;
914
+ }
915
+
916
+ .jewelry-checkout-bag__item {
917
+ display: grid;
918
+ grid-template-columns: 5.5rem minmax(0, 1fr) auto;
919
+ gap: 1rem;
920
+ align-items: center;
921
+ padding: 0.85rem;
922
+ border: 1px solid rgba(59, 31, 78, 0.12);
923
+ border-radius: 0.85rem;
924
+ background: #ffffff;
925
+ }
926
+
927
+ .jewelry-checkout-bag__thumb {
928
+ position: relative;
929
+ width: 5.5rem;
930
+ height: 5.5rem;
931
+ overflow: hidden;
932
+ border-radius: 0.7rem;
933
+ background: #faf6f1;
934
+ border: 1px solid rgba(59, 31, 78, 0.1);
935
+ }
936
+
937
+ .jewelry-checkout-bag__thumb-empty {
938
+ width: 100%;
939
+ height: 100%;
940
+ background: linear-gradient(135deg, #faf6f1 0%, #f4eff8 100%);
941
+ }
942
+
943
+ .jewelry-checkout-bag__meta {
944
+ min-width: 0;
945
+ display: flex;
946
+ flex-direction: column;
947
+ gap: 0.25rem;
948
+ }
949
+
950
+ .jewelry-checkout-bag__name {
951
+ margin: 0;
952
+ font-family: var(--font-heading, Georgia, serif);
953
+ font-size: 1.05rem;
954
+ font-weight: 600;
955
+ line-height: 1.3;
956
+ color: #1a1224;
957
+ }
958
+
959
+ .jewelry-checkout-bag__variant {
960
+ margin: 0;
961
+ font-size: 0.6875rem;
962
+ font-weight: 600;
963
+ letter-spacing: 0.06em;
964
+ text-transform: uppercase;
965
+ color: #6b6573;
966
+ }
967
+
968
+ .jewelry-checkout-bag__qty {
969
+ margin: 0.1rem 0 0;
970
+ font-size: 0.8125rem;
971
+ color: #6b6573;
972
+ }
973
+
974
+ .jewelry-checkout-bag__total {
975
+ margin: 0;
976
+ font-size: 0.9375rem;
977
+ font-weight: 700;
978
+ color: #3b1f4e;
979
+ white-space: nowrap;
980
+ }
981
+
982
+ @media (max-width: 520px) {
983
+ .jewelry-checkout-bag__item {
984
+ grid-template-columns: 4.25rem minmax(0, 1fr);
985
+ }
986
+
987
+ .jewelry-checkout-bag__thumb {
988
+ width: 4.25rem;
989
+ height: 4.25rem;
990
+ }
991
+
992
+ .jewelry-checkout-bag__total {
993
+ grid-column: 2;
994
+ justify-self: start;
995
+ }
996
+ }
package/src/segment.tsx CHANGED
@@ -2,6 +2,7 @@
2
2
 
3
3
  import type { HttpTypes } from "@medusajs/types"
4
4
  import AddressFields from "./address-fields"
5
+ import CheckoutBagItems from "./checkout-bag-items"
5
6
  import LeaveCheckoutGuard from "./leave-checkout-guard"
6
7
  import "./jewelry-checkout-form.css"
7
8
 
@@ -9,10 +10,12 @@ export default function JewelryCheckoutForm({
9
10
  cart,
10
11
  customer,
11
12
  formTitle = "Contact & shipping",
13
+ itemsTitle = "Your items",
12
14
  }: {
13
15
  cart?: HttpTypes.StoreCart
14
16
  customer?: HttpTypes.StoreCustomer | null
15
17
  formTitle?: string
18
+ itemsTitle?: string
16
19
  shippingMethods?: HttpTypes.StoreCartShippingOption[]
17
20
  paymentProviders?: Array<{ id: string }>
18
21
  shopName?: string
@@ -35,6 +38,7 @@ export default function JewelryCheckoutForm({
35
38
  <form id="checkout-address-form" autoComplete="off">
36
39
  <AddressFields cart={cart} customer={customer} formTitle={formTitle} />
37
40
  </form>
41
+ <CheckoutBagItems cart={cart} title={itemsTitle} />
38
42
  </div>
39
43
  </>
40
44
  )