Skip to content

Commit eb82a23

Browse files
committed
feat: Add address handling with v2 API
1 parent 87ee1ba commit eb82a23

File tree

12 files changed

+135
-246
lines changed

12 files changed

+135
-246
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222
"dependencies": {
2323
"@headlessui/react": "^1.6.1",
2424
"@hookform/error-message": "^2.0.0",
25-
"@medusajs/js-sdk": "0.0.2-snapshot-20240524111804",
26-
"@medusajs/types": "1.12.0-snapshot-20240524111804",
25+
"@medusajs/js-sdk": "file:../medusa/packages/core/js-sdk",
26+
"@medusajs/types": "file:../medusa/packages/core/types",
2727
"@medusajs/ui": "^2.2.0",
2828
"@meilisearch/instant-meilisearch": "^0.7.1",
2929
"@paypal/paypal-js": "^5.0.6",

src/app/[countryCode]/(main)/account/@dashboard/addresses/page.tsx

+6-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,12 @@ export const metadata: Metadata = {
1212
description: "View your addresses",
1313
}
1414

15-
export default async function Addresses() {
16-
const nextHeaders = headers()
17-
const countryCode = nextHeaders.get("next-url")?.split("/")[1] || ""
15+
export default async function Addresses({
16+
params,
17+
}: {
18+
params: { countryCode: string }
19+
}) {
20+
const { countryCode } = params
1821
const customer = await getCustomer()
1922
const region = await getRegion(countryCode)
2023

src/lib/data/customer.ts

+72
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,75 @@ export async function signout(countryCode: string) {
111111
revalidateTag("customer")
112112
redirect(`/${countryCode}/account`)
113113
}
114+
115+
export const addCustomerAddress = async (
116+
_currentState: unknown,
117+
formData: FormData
118+
): Promise<any> => {
119+
const address = {
120+
first_name: formData.get("first_name") as string,
121+
last_name: formData.get("last_name") as string,
122+
company: formData.get("company") as string,
123+
address_1: formData.get("address_1") as string,
124+
address_2: formData.get("address_2") as string,
125+
city: formData.get("city") as string,
126+
postal_code: formData.get("postal_code") as string,
127+
province: formData.get("province") as string,
128+
country_code: formData.get("country_code") as string,
129+
phone: formData.get("phone") as string,
130+
}
131+
132+
return sdk.store.customer
133+
.createAddress(address, {}, await getAuthHeaders())
134+
.then(({ customer }) => {
135+
revalidateTag("customer")
136+
return { success: true, error: null }
137+
})
138+
.catch((err) => {
139+
return { success: false, error: err.toString() }
140+
})
141+
}
142+
143+
export const deleteCustomerAddress = async (
144+
addressId: string
145+
): Promise<void> => {
146+
await sdk.store.customer
147+
.deleteAddress(addressId, await getAuthHeaders())
148+
.then(() => {
149+
revalidateTag("customer")
150+
return { success: true, error: null }
151+
})
152+
.catch((err) => {
153+
return { success: false, error: err.toString() }
154+
})
155+
}
156+
157+
export const updateCustomerAddress = async (
158+
currentState: Record<string, unknown>,
159+
formData: FormData
160+
): Promise<any> => {
161+
const addressId = currentState.addressId as string
162+
163+
const address = {
164+
first_name: formData.get("first_name") as string,
165+
last_name: formData.get("last_name") as string,
166+
company: formData.get("company") as string,
167+
address_1: formData.get("address_1") as string,
168+
address_2: formData.get("address_2") as string,
169+
city: formData.get("city") as string,
170+
postal_code: formData.get("postal_code") as string,
171+
province: formData.get("province") as string,
172+
country_code: formData.get("country_code") as string,
173+
phone: formData.get("phone") as string,
174+
}
175+
176+
return sdk.store.customer
177+
.updateAddress(addressId, address, {}, await getAuthHeaders())
178+
.then(() => {
179+
revalidateTag("customer")
180+
return { success: true, error: null }
181+
})
182+
.catch((err) => {
183+
return { success: false, error: err.toString() }
184+
})
185+
}

src/lib/data/index.ts

-26
This file was deleted.

src/modules/account/actions.ts

-144
This file was deleted.

src/modules/account/components/address-card/add-address.tsx

+2-2
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ import CountrySelect from "@modules/checkout/components/country-select"
1010
import Input from "@modules/common/components/input"
1111
import Modal from "@modules/common/components/modal"
1212
import { SubmitButton } from "@modules/checkout/components/submit-button"
13-
import { addCustomerShippingAddress } from "@modules/account/actions"
1413
import { HttpTypes } from "@medusajs/types"
14+
import { addCustomerAddress } from "@lib/data/customer"
1515

1616
const AddAddress = ({ region }: { region: HttpTypes.StoreRegion }) => {
1717
const [successState, setSuccessState] = useState(false)
1818
const { state, open, close: closeModal } = useToggleState(false)
1919

20-
const [formState, formAction] = useFormState(addCustomerShippingAddress, {
20+
const [formState, formAction] = useFormState(addCustomerAddress, {
2121
success: false,
2222
error: null,
2323
})

src/modules/account/components/address-card/edit-address-modal.tsx

+6-6
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,14 @@ import useToggleState from "@lib/hooks/use-toggle-state"
88
import CountrySelect from "@modules/checkout/components/country-select"
99
import Input from "@modules/common/components/input"
1010
import Modal from "@modules/common/components/modal"
11-
import {
12-
deleteCustomerShippingAddress,
13-
updateCustomerShippingAddress,
14-
} from "@modules/account/actions"
1511
import Spinner from "@modules/common/icons/spinner"
1612
import { useFormState } from "react-dom"
1713
import { SubmitButton } from "@modules/checkout/components/submit-button"
1814
import { HttpTypes } from "@medusajs/types"
15+
import {
16+
deleteCustomerAddress,
17+
updateCustomerAddress,
18+
} from "@lib/data/customer"
1919

2020
type EditAddressProps = {
2121
region: HttpTypes.StoreRegion
@@ -32,7 +32,7 @@ const EditAddress: React.FC<EditAddressProps> = ({
3232
const [successState, setSuccessState] = useState(false)
3333
const { state, open, close: closeModal } = useToggleState(false)
3434

35-
const [formState, formAction] = useFormState(updateCustomerShippingAddress, {
35+
const [formState, formAction] = useFormState(updateCustomerAddress, {
3636
success: false,
3737
error: null,
3838
addressId: address.id,
@@ -58,7 +58,7 @@ const EditAddress: React.FC<EditAddressProps> = ({
5858

5959
const removeAddress = async () => {
6060
setRemoving(true)
61-
await deleteCustomerShippingAddress(address.id)
61+
await deleteCustomerAddress(address.id)
6262
setRemoving(false)
6363
}
6464

src/modules/account/components/overview/index.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,11 @@ const getProfileCompletion = (customer: HttpTypes.StoreCustomer | null) => {
154154
count++
155155
}
156156

157-
if ((customer as any).billing_address) {
157+
const billingAddress = customer.addresses?.find(
158+
(addr) => addr.is_default_billing
159+
)
160+
161+
if (billingAddress) {
158162
count++
159163
}
160164

0 commit comments

Comments
 (0)