Skip to content

Commit 94453a4

Browse files
authored
Merge pull request #414 from medusajs/v2
merge v2 branch into main
2 parents 0058c52 + 5bb9311 commit 94453a4

File tree

5 files changed

+9197
-5431
lines changed

5 files changed

+9197
-5431
lines changed

package.json

+4-4
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"dependencies": {
1919
"@headlessui/react": "^1.6.1",
2020
"@hookform/error-message": "^2.0.0",
21-
"@medusajs/js-sdk": "2.0.0",
22-
"@medusajs/ui": "2.0.0",
21+
"@medusajs/js-sdk": "latest",
22+
"@medusajs/ui": "latest",
2323
"@meilisearch/instant-meilisearch": "^0.7.1",
2424
"@paypal/paypal-js": "^5.0.6",
2525
"@paypal/react-paypal-js": "^7.8.1",
@@ -42,8 +42,8 @@
4242
},
4343
"devDependencies": {
4444
"@babel/core": "^7.17.5",
45-
"@medusajs/types": "2.0.0",
46-
"@medusajs/ui-preset": "2.0.0",
45+
"@medusajs/types": "latest",
46+
"@medusajs/ui-preset": "latest",
4747
"@playwright/test": "^1.41.1",
4848
"@types/lodash": "^4.14.195",
4949
"@types/node": "17.0.21",

src/app/[countryCode]/(main)/layout.tsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,26 @@
11
import { Metadata } from "next"
22

3+
import { retrieveCart } from "@lib/data/cart"
4+
import { getCustomer } from "@lib/data/customer"
5+
import { getBaseURL } from "@lib/util/env"
6+
import CartMismatchBanner from "@modules/layout/components/cart-mismatch-banner"
37
import Footer from "@modules/layout/templates/footer"
48
import Nav from "@modules/layout/templates/nav"
5-
import { getBaseURL } from "@lib/util/env"
69

710
export const metadata: Metadata = {
811
metadataBase: new URL(getBaseURL()),
912
}
1013

1114
export default async function PageLayout(props: { children: React.ReactNode }) {
15+
const customer = await getCustomer()
16+
const cart = await retrieveCart()
17+
1218
return (
1319
<>
1420
<Nav />
21+
{customer && cart && (
22+
<CartMismatchBanner customer={customer} cart={cart} />
23+
)}
1524
{props.children}
1625
<Footer />
1726
</>

src/lib/data/customer.ts

+31-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { HttpTypes } from "@medusajs/types"
66
import { revalidateTag } from "next/cache"
77
import { redirect } from "next/navigation"
88
import { cache } from "react"
9-
import { getAuthHeaders, removeAuthToken, setAuthToken } from "./cookies"
9+
import {
10+
getAuthHeaders,
11+
getCartId,
12+
removeAuthToken,
13+
setAuthToken,
14+
} from "./cookies"
1015

1116
export const getCustomer = cache(async function () {
1217
return await sdk.store.customer
@@ -58,6 +63,9 @@ export async function signup(_currentState: unknown, formData: FormData) {
5863
setAuthToken(loginToken as string)
5964

6065
revalidateTag("customer")
66+
67+
await transferCart()
68+
6169
return createdCustomer
6270
} catch (error: any) {
6371
return error.toString()
@@ -78,6 +86,12 @@ export async function login(_currentState: unknown, formData: FormData) {
7886
} catch (error: any) {
7987
return error.toString()
8088
}
89+
90+
try {
91+
await transferCart()
92+
} catch (error: any) {
93+
return error.toString()
94+
}
8195
}
8296

8397
export async function signout(countryCode: string) {
@@ -88,6 +102,22 @@ export async function signout(countryCode: string) {
88102
redirect(`/${countryCode}/account`)
89103
}
90104

105+
export async function transferCart() {
106+
const cartId = getCartId()
107+
108+
if (!cartId) {
109+
return
110+
}
111+
112+
const headers = {
113+
...getAuthHeaders(),
114+
}
115+
116+
await sdk.store.cart.transferCart(cartId, {}, headers)
117+
118+
revalidateTag("cart")
119+
}
120+
91121
export const addCustomerAddress = async (
92122
_currentState: unknown,
93123
formData: FormData
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"use client"
2+
3+
import { transferCart } from "@lib/data/customer"
4+
import { ExclamationCircleSolid } from "@medusajs/icons"
5+
import { StoreCart, StoreCustomer } from "@medusajs/types"
6+
import { Button } from "@medusajs/ui"
7+
import { useState } from "react"
8+
9+
function CartMismatchBanner(props: {
10+
customer: StoreCustomer
11+
cart: StoreCart
12+
}) {
13+
const { customer, cart } = props
14+
const [isPending, setIsPending] = useState(false)
15+
const [actionText, setActionText] = useState("Run transfer again")
16+
17+
if (!customer || !!cart.customer_id) {
18+
return
19+
}
20+
21+
const handleSubmit = async () => {
22+
try {
23+
setIsPending(true)
24+
setActionText("Transferring..")
25+
26+
await transferCart()
27+
} catch {
28+
setActionText("Run transfer again")
29+
setIsPending(false)
30+
}
31+
}
32+
33+
return (
34+
<div className="flex items-center justify-center small:p-4 p-2 text-center bg-orange-300 small:gap-2 gap-1 text-sm mt-2 text-orange-800">
35+
<div className="flex flex-col small:flex-row small:gap-2 gap-1 items-center">
36+
<span className="flex items-center gap-1">
37+
<ExclamationCircleSolid className="inline" />
38+
Something went wrong when we tried to transfer your cart
39+
</span>
40+
41+
<span>·</span>
42+
43+
<Button
44+
variant="transparent"
45+
className="hover:bg-transparent active:bg-transparent focus:bg-transparent disabled:text-orange-500 text-orange-950 p-0 bg-transparent"
46+
size="base"
47+
disabled={isPending}
48+
onClick={handleSubmit}
49+
>
50+
{actionText}
51+
</Button>
52+
</div>
53+
</div>
54+
)
55+
}
56+
57+
export default CartMismatchBanner

0 commit comments

Comments
 (0)