diff --git a/src/app/[countryCode]/(checkout)/checkout/page.tsx b/src/app/[countryCode]/(checkout)/checkout/page.tsx index b5d73a9d8..a4704f129 100644 --- a/src/app/[countryCode]/(checkout)/checkout/page.tsx +++ b/src/app/[countryCode]/(checkout)/checkout/page.tsx @@ -6,6 +6,7 @@ import CheckoutForm from "@modules/checkout/templates/checkout-form" import CheckoutSummary from "@modules/checkout/templates/checkout-summary" import { enrichLineItems, retrieveCart } from "@lib/data/cart" import { HttpTypes } from "@medusajs/types" +import { getCustomer } from "@lib/data/customer" export const metadata: Metadata = { title: "Checkout", @@ -27,11 +28,12 @@ const fetchCart = async () => { export default async function Checkout() { const cart = await fetchCart() + const customer = await getCustomer() return (
- +
diff --git a/src/app/[countryCode]/(main)/cart/page.tsx b/src/app/[countryCode]/(main)/cart/page.tsx index 65ace65cd..1dce39983 100644 --- a/src/app/[countryCode]/(main)/cart/page.tsx +++ b/src/app/[countryCode]/(main)/cart/page.tsx @@ -4,7 +4,6 @@ import CartTemplate from "@modules/cart/templates" import { enrichLineItems, retrieveCart } from "@lib/data/cart" import { HttpTypes } from "@medusajs/types" import { getCustomer } from "@lib/data/customer" -import { notFound } from "next/navigation" export const metadata: Metadata = { title: "Cart", @@ -15,7 +14,7 @@ const fetchCart = async () => { const cart = await retrieveCart() if (!cart) { - return notFound() + return null } if (cart?.items?.length) { diff --git a/src/lib/data/cart.ts b/src/lib/data/cart.ts index 80cff4844..91d6e961d 100644 --- a/src/lib/data/cart.ts +++ b/src/lib/data/cart.ts @@ -287,51 +287,63 @@ export async function submitPromotionForm( formData: FormData ) { const code = formData.get("code") as string - await applyPromotions([code]) + try { + await applyPromotions([code]) + } catch (e: any) { + return e.message + } } // TODO: Pass a POJO instead of a form entity here export async function setAddresses(currentState: unknown, formData: FormData) { - if (!formData) return "No form data received" - const cartId = getCartId() - if (!cartId) return { message: "No cartId cookie found" } - - const data = { - shipping_address: { - first_name: formData.get("shipping_address.first_name"), - last_name: formData.get("shipping_address.last_name"), - address_1: formData.get("shipping_address.address_1"), - address_2: "", - company: formData.get("shipping_address.company"), - postal_code: formData.get("shipping_address.postal_code"), - city: formData.get("shipping_address.city"), - country_code: formData.get("shipping_address.country_code"), - province: formData.get("shipping_address.province"), - phone: formData.get("shipping_address.phone"), - }, - email: formData.get("email"), - } as any - - const sameAsBilling = formData.get("same_as_billing") - if (sameAsBilling === "on") data.billing_address = data.shipping_address - - if (sameAsBilling !== "on") - data.billing_address = { - first_name: formData.get("billing_address.first_name"), - last_name: formData.get("billing_address.last_name"), - address_1: formData.get("billing_address.address_1"), - address_2: "", - company: formData.get("billing_address.company"), - postal_code: formData.get("billing_address.postal_code"), - city: formData.get("billing_address.city"), - country_code: formData.get("billing_address.country_code"), - province: formData.get("billing_address.province"), - phone: formData.get("billing_address.phone"), + try { + if (!formData) { + throw new Error("No form data found when setting addresses") } - await updateCart(data) - redirect( - `/${formData.get("shipping_address.country_code")}/checkout?step=delivery` - ) + const cartId = getCartId() + if (!cartId) { + throw new Error("No existing cart found when setting addresses") + } + + const data = { + shipping_address: { + first_name: formData.get("shipping_address.first_name"), + last_name: formData.get("shipping_address.last_name"), + address_1: formData.get("shipping_address.address_1"), + address_2: "", + company: formData.get("shipping_address.company"), + postal_code: formData.get("shipping_address.postal_code"), + city: formData.get("shipping_address.city"), + country_code: formData.get("shipping_address.country_code"), + province: formData.get("shipping_address.province"), + phone: formData.get("shipping_address.phone"), + }, + email: formData.get("email"), + } as any + + const sameAsBilling = formData.get("same_as_billing") + if (sameAsBilling === "on") data.billing_address = data.shipping_address + + if (sameAsBilling !== "on") + data.billing_address = { + first_name: formData.get("billing_address.first_name"), + last_name: formData.get("billing_address.last_name"), + address_1: formData.get("billing_address.address_1"), + address_2: "", + company: formData.get("billing_address.company"), + postal_code: formData.get("billing_address.postal_code"), + city: formData.get("billing_address.city"), + country_code: formData.get("billing_address.country_code"), + province: formData.get("billing_address.province"), + phone: formData.get("billing_address.phone"), + } + await updateCart(data) + redirect( + `/${formData.get("shipping_address.country_code")}/checkout?step=delivery` + ) + } catch (e: any) { + return e.message + } } export async function placeOrder() { diff --git a/src/lib/util/get-product-price.ts b/src/lib/util/get-product-price.ts index 8537346d0..b7d776203 100644 --- a/src/lib/util/get-product-price.ts +++ b/src/lib/util/get-product-price.ts @@ -3,6 +3,10 @@ import { getPercentageDiff } from "./get-precentage-diff" import { convertToLocale } from "./money" export const getPricesForVariant = (variant: any) => { + if (!variant.calculated_price?.calculated_amount) { + return {} + } + return { calculated_price_number: variant.calculated_price.calculated_amount, calculated_price: convertToLocale({ @@ -39,12 +43,14 @@ export function getProductPrice({ return null } - const cheapestVariant: any = product.variants.sort((a: any, b: any) => { - return ( - a.calculated_price.calculated_amount - - b.calculated_price.calculated_amount - ) - })[0] + const cheapestVariant: any = product.variants + .filter((v: any) => !!v.calculated_price) + .sort((a: any, b: any) => { + return ( + a.calculated_price.calculated_amount - + b.calculated_price.calculated_amount + ) + })[0] return getPricesForVariant(cheapestVariant) } diff --git a/src/modules/checkout/components/discount-code/index.tsx b/src/modules/checkout/components/discount-code/index.tsx index cae400fa2..7c7160fa9 100644 --- a/src/modules/checkout/components/discount-code/index.tsx +++ b/src/modules/checkout/components/discount-code/index.tsx @@ -28,9 +28,7 @@ const DiscountCode: React.FC = ({ cart }) => { ) await applyPromotions( - validPromotions - .filter((p) => p.code === undefined) - .map((p) => p.code!) + validPromotions.filter((p) => p.code === undefined).map((p) => p.code!) ) } @@ -77,6 +75,7 @@ const DiscountCode: React.FC = ({ cart }) => { <>
= ({ cart }) => { {promotion.code} {" "} ( - {promotion.application_method?.value !== undefined && - promotion.application_method.currency_code !== undefined && ( - <> - {promotion.application_method.type === "percentage" - ? `${promotion.application_method.value}%` - : convertToLocale({ - amount: promotion.application_method.value, - currency_code: - promotion.application_method.currency_code, - })} - - )} + {promotion.application_method?.value !== undefined && + promotion.application_method.currency_code !== + undefined && ( + <> + {promotion.application_method.type === + "percentage" + ? `${promotion.application_method.value}%` + : convertToLocale({ + amount: promotion.application_method.value, + currency_code: + promotion.application_method + .currency_code, + })} + + )} ) {promotion.is_automatic && ( diff --git a/src/modules/checkout/templates/checkout-form/index.tsx b/src/modules/checkout/templates/checkout-form/index.tsx index efe775c2f..73dadf166 100644 --- a/src/modules/checkout/templates/checkout-form/index.tsx +++ b/src/modules/checkout/templates/checkout-form/index.tsx @@ -4,23 +4,23 @@ import { retrieveCart } from "@lib/data/cart" import { getCustomer } from "@lib/data/customer" import { listCartShippingMethods } from "@lib/data/fulfillment" import { listCartPaymentMethods } from "@lib/data/payment" -import { StoreCart, StoreCustomer } from "@medusajs/types" +import { HttpTypes, StoreCart, StoreCustomer } from "@medusajs/types" import Addresses from "@modules/checkout/components/addresses" import Payment from "@modules/checkout/components/payment" import Review from "@modules/checkout/components/review" import Shipping from "@modules/checkout/components/shipping" import { useState } from "react" -export default function CheckoutForm() { - const [cart, setCart] = useState(null) - const [customer, setCustomer] = useState(null) +export default function CheckoutForm({ + cart, + customer, +}: { + cart: HttpTypes.StoreCart | null + customer: HttpTypes.StoreCustomer | null +}) { const [shippingMethods, setAvailableShippingMethods] = useState([]) const [paymentMethods, setPaymentMethods] = useState([]) - if (!cart) { - retrieveCart().then((cart) => setCart(cart)) - } - if (!cart) { return null } @@ -33,8 +33,6 @@ export default function CheckoutForm() { setPaymentMethods(payments) ) - getCustomer().then((customer: any) => setCustomer(customer)) - return (
diff --git a/src/modules/layout/components/cart-button/index.tsx b/src/modules/layout/components/cart-button/index.tsx index f55bebd80..9f9758dfc 100644 --- a/src/modules/layout/components/cart-button/index.tsx +++ b/src/modules/layout/components/cart-button/index.tsx @@ -6,7 +6,7 @@ const fetchCart = async () => { const cart = await retrieveCart() if (!cart) { - return notFound() + return null } if (cart?.items?.length) { diff --git a/tsconfig.json b/tsconfig.json index 0bc7c5ef6..efb930f45 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -1,6 +1,6 @@ { "compilerOptions": { - "target": "es6", + "target": "es5", "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, "skipLibCheck": true,