Skip to content

Commit

Permalink
fix: Minor fixes to store
Browse files Browse the repository at this point in the history
  • Loading branch information
sradevski committed Jul 1, 2024
1 parent 32f76c3 commit 694e11e
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 76 deletions.
4 changes: 3 additions & 1 deletion src/app/[countryCode]/(checkout)/checkout/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -27,11 +28,12 @@ const fetchCart = async () => {

export default async function Checkout() {
const cart = await fetchCart()
const customer = await getCustomer()

return (
<div className="grid grid-cols-1 small:grid-cols-[1fr_416px] content-container gap-x-40 py-12">
<Wrapper cart={cart}>
<CheckoutForm />
<CheckoutForm cart={cart} customer={customer} />
</Wrapper>
<CheckoutSummary cart={cart} />
</div>
Expand Down
3 changes: 1 addition & 2 deletions src/app/[countryCode]/(main)/cart/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -15,7 +14,7 @@ const fetchCart = async () => {
const cart = await retrieveCart()

if (!cart) {
return notFound()
return null
}

if (cart?.items?.length) {
Expand Down
92 changes: 52 additions & 40 deletions src/lib/data/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
18 changes: 12 additions & 6 deletions src/lib/util/get-product-price.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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)
}
Expand Down
32 changes: 17 additions & 15 deletions src/modules/checkout/components/discount-code/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
)

await applyPromotions(
validPromotions
.filter((p) => p.code === undefined)
.map((p) => p.code!)
validPromotions.filter((p) => p.code === undefined).map((p) => p.code!)
)
}

Expand Down Expand Up @@ -77,6 +75,7 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
<>
<div className="flex w-full gap-x-2">
<Input
className="size-full"
id="promotion-input"
name="code"
type="text"
Expand Down Expand Up @@ -122,18 +121,21 @@ const DiscountCode: React.FC<DiscountCodeProps> = ({ cart }) => {
{promotion.code}
</Badge>{" "}
(
{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 && (
<Tooltip content="This promotion is automatically applied">
Expand Down
18 changes: 8 additions & 10 deletions src/modules/checkout/templates/checkout-form/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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<StoreCart | null>(null)
const [customer, setCustomer] = useState<StoreCustomer | null>(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
}
Expand All @@ -33,8 +33,6 @@ export default function CheckoutForm() {
setPaymentMethods(payments)
)

getCustomer().then((customer: any) => setCustomer(customer))

return (
<div>
<div className="w-full grid grid-cols-1 gap-y-8">
Expand Down
2 changes: 1 addition & 1 deletion src/modules/layout/components/cart-button/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const fetchCart = async () => {
const cart = await retrieveCart()

if (!cart) {
return notFound()
return null
}

if (cart?.items?.length) {
Expand Down
2 changes: 1 addition & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es6",
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"allowJs": true,
"skipLibCheck": true,
Expand Down

0 comments on commit 694e11e

Please sign in to comment.