@pradip1995/segment-animation-order 0.2.2 → 0.2.3

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-animation-order",
3
- "version": "0.2.2",
3
+ "version": "0.2.3",
4
4
  "description": "Motion/Lumière order confirmed, failed, and details pages",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -16,10 +16,6 @@
16
16
  ".": "./src/index.ts",
17
17
  "./manifest": "./src/manifest.ts"
18
18
  },
19
- "scripts": {
20
- "typecheck": "tsc --noEmit",
21
- "lint": "tsc --noEmit"
22
- },
23
19
  "peerDependencies": {
24
20
  "@medusajs/types": ">=2",
25
21
  "@pradip1995/commerce-core": "^4.0.0",
@@ -29,7 +25,7 @@
29
25
  "react-dom": ">=19"
30
26
  },
31
27
  "dependencies": {
32
- "@pradip1995/segment-primitives": "^0.4.11",
28
+ "@pradip1995/segment-primitives": "^0.4.0",
33
29
  "framer-motion": "^11.0.0"
34
30
  },
35
31
  "devDependencies": {
@@ -40,5 +36,9 @@
40
36
  "next": ">=15",
41
37
  "react": "19.0.3",
42
38
  "typescript": "^5.7.2"
39
+ },
40
+ "scripts": {
41
+ "typecheck": "tsc --noEmit",
42
+ "lint": "tsc --noEmit"
43
43
  }
44
- }
44
+ }
@@ -1,6 +1,6 @@
1
1
  "use server"
2
2
 
3
- import { cancelOrder, downloadInvoice } from "@pradip1995/commerce-core/data/orders"
3
+ import { cancelOrder } from "@pradip1995/commerce-core/data/orders"
4
4
  import { cookies } from "next/headers"
5
5
 
6
6
  type Result = { success: boolean; error: string | null }
@@ -21,19 +21,84 @@ export async function cancelOrderAction(orderId: string): Promise<Result> {
21
21
  }
22
22
  }
23
23
 
24
+ async function readInvoiceError(response: Response): Promise<string> {
25
+ const fallback = `Failed to download invoice: ${response.status} ${response.statusText}`
26
+ try {
27
+ const contentType = response.headers.get("content-type") || ""
28
+ if (contentType.includes("application/json")) {
29
+ const body = (await response.json()) as {
30
+ message?: string
31
+ error?: string
32
+ }
33
+ if (body.message) return body.message
34
+ if (body.error) return body.error
35
+ } else {
36
+ const text = (await response.text()).trim()
37
+ if (text) return text.slice(0, 240)
38
+ }
39
+ } catch {
40
+ /* ignore parse errors */
41
+ }
42
+ return fallback
43
+ }
44
+
45
+ /**
46
+ * Download invoice PDF. Calls the invoice plugin directly so we can surface
47
+ * the real API error (e.g. order not eligible) instead of a bare 400.
48
+ */
24
49
  export async function downloadInvoiceAction(
25
50
  orderId: string
26
51
  ): Promise<{ success: boolean; data?: string; error?: string }> {
27
52
  if (!orderId) return { success: false, error: "Order ID is required" }
53
+
28
54
  try {
29
- const result = await downloadInvoice(orderId)
30
- if (!result?.success || !result.data) {
31
- return {
32
- success: false,
33
- error: result?.error || "Failed to download invoice",
34
- }
55
+ const cookieStore = await cookies()
56
+ const token = cookieStore.get("_medusa_jwt")?.value
57
+ const guestToken = !token
58
+ ? cookieStore.get("_medusa_guest_jwt")?.value ||
59
+ cookieStore.get("_medusa_guest_token")?.value
60
+ : null
61
+
62
+ const headers: HeadersInit = {
63
+ "x-publishable-api-key": process.env.NEXT_PUBLIC_MEDUSA_PUBLISHABLE_KEY || "",
64
+ }
65
+
66
+ let url = `/store/invoice/download/${orderId}`
67
+ if (token) {
68
+ headers.Cookie = `_medusa_jwt=${token}`
69
+ headers.Authorization = `Bearer ${token}`
70
+ } else if (guestToken) {
71
+ url = `/store/guest/invoice/download/${orderId}`
72
+ headers.Cookie = `_medusa_guest_jwt=${guestToken}`
73
+ headers.Authorization = `Bearer ${guestToken}`
74
+ }
75
+
76
+ const backendUrl =
77
+ process.env.NEXT_PUBLIC_MEDUSA_BACKEND_URL || "http://localhost:9000"
78
+ const response = await fetch(`${backendUrl}${url}`, {
79
+ method: "GET",
80
+ headers,
81
+ cache: "no-store",
82
+ })
83
+
84
+ if (!response.ok) {
85
+ return { success: false, error: await readInvoiceError(response) }
86
+ }
87
+
88
+ const contentType = response.headers.get("content-type") || ""
89
+ if (!contentType.includes("application/pdf")) {
90
+ return { success: false, error: await readInvoiceError(response) }
91
+ }
92
+
93
+ const arrayBuffer = await response.arrayBuffer()
94
+ if (arrayBuffer.byteLength === 0) {
95
+ return { success: false, error: "Generated PDF is empty" }
96
+ }
97
+
98
+ return {
99
+ success: true,
100
+ data: Buffer.from(arrayBuffer).toString("base64"),
35
101
  }
36
- return { success: true, data: result.data }
37
102
  } catch (err) {
38
103
  return {
39
104
  success: false,
@@ -6,7 +6,6 @@ import { usePathname, useRouter } from "next/navigation"
6
6
  import type { HttpTypes } from "@medusajs/types"
7
7
  import { addToCart } from "@pradip1995/commerce-core/client/actions/cart"
8
8
  import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
9
- import { presentOrderLines } from "@pradip1995/segment-primitives/order-lines"
10
9
  import {
11
10
  cancelOrderAction,
12
11
  downloadInvoiceAction,
@@ -70,7 +69,13 @@ function canReorder(order: HttpTypes.StoreOrder): boolean {
70
69
 
71
70
  function canDownloadInvoice(order: HttpTypes.StoreOrder): boolean {
72
71
  const status = (order.status || "").toLowerCase()
73
- return status !== "canceled" && status !== "cancelled"
72
+ if (status === "canceled" || status === "cancelled") return false
73
+ // Invoice only after the order is delivered or marked completed.
74
+ if (status === "completed" || status === "complete") return true
75
+ const fulfillment = (order.fulfillment_status || "").toLowerCase()
76
+ return ["delivered", "partially_delivered", "completed", "complete"].includes(
77
+ fulfillment
78
+ )
74
79
  }
75
80
 
76
81
  function triggerPdfDownload(base64: string, filename: string) {
@@ -100,7 +105,7 @@ export default function OrderActions({ order }: { order: HttpTypes.StoreOrder })
100
105
  const router = useRouter()
101
106
  const pathname = usePathname() || ""
102
107
  const countryCode = pathname.split("/").filter(Boolean)[0] || "in"
103
- const items = presentOrderLines(order).purchasedItems as LineItem[]
108
+ const items = (order.items || []) as LineItem[]
104
109
  const activeReturns = getActiveReturns(order)
105
110
  const hasOpenReturn = activeReturns.length > 0
106
111
  const showCancel = canCancelOrder(order)
@@ -4,7 +4,6 @@ import Image from "next/image"
4
4
  import { motion } from "framer-motion"
5
5
  import LocalizedLink from "@pradip1995/segment-primitives/localized-link"
6
6
  import { formatPrice } from "@pradip1995/segment-primitives/format-price"
7
- import { presentOrderLines } from "@pradip1995/segment-primitives/order-lines"
8
7
  import type { HttpTypes } from "@medusajs/types"
9
8
  import OrderActions from "./order-actions"
10
9
  import OrderTracking from "./order-tracking"
@@ -33,8 +32,7 @@ export default function OrderDetailsView({
33
32
  isLoggedIn: boolean
34
33
  }) {
35
34
  const currency = order.currency_code || "inr"
36
- const presentation = presentOrderLines(order)
37
- const items = presentation.purchasedItems
35
+ const items = order.items || []
38
36
  const shipping = order.shipping_address
39
37
  const status = pretty(order.status)
40
38
  const fulfillment = pretty(order.fulfillment_status)
@@ -218,15 +216,15 @@ export default function OrderDetailsView({
218
216
  <dl className="anim-order__summary">
219
217
  <div>
220
218
  <dt>Subtotal</dt>
221
- <dd>{formatPrice(presentation.paidSubtotal, currency)}</dd>
219
+ <dd>{formatPrice(order.subtotal, currency)}</dd>
222
220
  </div>
223
221
  <div>
224
222
  <dt>Shipping</dt>
225
223
  <dd>{formatPrice(order.shipping_total, currency)}</dd>
226
224
  </div>
227
225
  <div className="anim-order__summary-total">
228
- <dt>Paid total</dt>
229
- <dd>{formatPrice(presentation.paidTotal, currency)}</dd>
226
+ <dt>Total</dt>
227
+ <dd>{formatPrice(order.total, currency)}</dd>
230
228
  </div>
231
229
  </dl>
232
230
  </div>