|
1
|
--- |
|
2
|
tags: |
|
3
|
- customer |
|
4
|
- storefront |
|
5
|
products: |
|
6
|
- customer |
|
7
|
--- |
|
8
|
|
|
9
|
import { CodeTabs, CodeTab } from "docs-ui" |
|
10
|
|
|
11
|
export const metadata = { |
|
12
|
title: `Edit Customer Profile in Storefront`, |
|
13
|
} |
|
14
|
|
|
15
|
# {metadata.title} |
|
16
|
|
|
17
|
In this guide, you'll learn how to edit the customer's profile in the storefront, which is useful if you're adding a profile page to your storefront that allows the customer to view and edit their details. |
|
18
|
|
|
19
|
To edit the customer's profile in the storefront, send a request to the [Update Customer API route](!api!/store/customers/update-customer). |
|
20
|
|
|
21
|
For example: |
|
22
|
|
|
23
|
<Note title="Tip"> |
|
24
|
|
|
25
|
- Learn how to install and configure the JS SDK in the [JS SDK documentation](../../../js-sdk/page.mdx). |
|
26
|
- This example uses the `useCustomer` hook defined in the [Customer Context guide](../context/page.mdx). |
|
27
|
- Since only authenticated customers can edit their profile, this example assumes that the JS SDK is already configured for authentication and the customer's authentication token was set as explained in the [Login in Storefront](../login/page.mdx) and [Third-Party Login](../third-party-login/page.mdx) guides. |
|
28
|
|
|
29
|
</Note> |
|
30
|
|
|
31
|
<CodeTabs group="store-request"> |
|
32
|
<CodeTab label="React" value="react"> |
|
33
|
|
|
34
|
export const highlights = [ |
|
35
|
["4", "useCustomer", "Use the hook defined in the Customer Context guide."], |
|
36
|
] |
|
37
|
|
|
38
|
```tsx highlights={highlights} collapsibleLines="47-85" expandButtonLabel="Show form" |
|
39
|
"use client" // include with Next.js 13+ |
|
40
|
|
|
41
|
import { useState } from "react" |
|
42
|
import { useCustomer } from "@/providers/customer" |
|
43
|
import { sdk } from "@/lib/sdk" |
|
44
|
|
|
45
|
export default function EditProfile() { |
|
46
|
const { customer, setCustomer } = useCustomer() |
|
47
|
console.log(customer) |
|
48
|
const [firstName, setFirstName] = useState( |
|
49
|
customer?.first_name || "" |
|
50
|
) |
|
51
|
const [lastName, setLastName] = useState( |
|
52
|
customer?.last_name || "" |
|
53
|
) |
|
54
|
const [company, setCompany] = useState( |
|
55
|
customer?.company_name || "" |
|
56
|
) |
|
57
|
const [phone, setPhone] = useState( |
|
58
|
customer?.phone || "" |
|
59
|
) |
|
60
|
const [loading, setLoading] = useState(false) |
|
61
|
|
|
62
|
const handleEdit = ( |
|
63
|
e: React.MouseEvent<HTMLButtonElement, MouseEvent> |
|
64
|
) => { |
|
65
|
e.preventDefault() |
|
66
|
|
|
67
|
if (!customer) { |
|
68
|
return |
|
69
|
} |
|
70
|
|
|
71
|
setLoading(true) |
|
72
|
|
|
73
|
sdk.store.customer.update({ |
|
74
|
first_name: firstName, |
|
75
|
last_name: lastName, |
|
76
|
company_name: company, |
|
77
|
phone, |
|
78
|
}) |
|
79
|
.then(({ customer: updatedCustomer }) => { |
|
80
|
setCustomer(updatedCustomer) |
|
81
|
}) |
|
82
|
.finally(() => setLoading(false)) |
|
83
|
} |
|
84
|
|
|
85
|
return ( |
|
86
|
<form> |
|
87
|
<input |
|
88
|
type="text" |
|
89
|
name="first_name" |
|
90
|
value={firstName} |
|
91
|
placeholder="First Name" |
|
92
|
onChange={(e) => setFirstName(e.target.value)} |
|
93
|
/> |
|
94
|
<input |
|
95
|
type="text" |
|
96
|
name="last_name" |
|
97
|
value={lastName} |
|
98
|
placeholder="Last Name" |
|
99
|
onChange={(e) => setLastName(e.target.value)} |
|
100
|
/> |
|
101
|
<input |
|
102
|
type="text" |
|
103
|
name="company" |
|
104
|
value={company} |
|
105
|
placeholder="Company" |
|
106
|
onChange={(e) => setCompany(e.target.value)} |
|
107
|
/> |
|
108
|
<input |
|
109
|
type="text" |
|
110
|
name="phone" |
|
111
|
value={phone} |
|
112
|
placeholder="Phone Number" |
|
113
|
onChange={(e) => setPhone(e.target.value)} |
|
114
|
/> |
|
115
|
<button |
|
116
|
disabled={loading} |
|
117
|
onClick={handleEdit} |
|
118
|
> |
|
119
|
Edit |
|
120
|
</button> |
|
121
|
</form> |
|
122
|
) |
|
123
|
} |
|
124
|
``` |
|
125
|
|
|
126
|
</CodeTab> |
|
127
|
<CodeTab label="JS SDK" value="js-sdk"> |
|
128
|
|
|
129
|
```ts |
|
130
|
sdk.store.customer.update({ |
|
131
|
first_name: firstName, |
|
132
|
last_name: lastName, |
|
133
|
company_name: company, |
|
134
|
phone, |
|
135
|
}) |
|
136
|
.then(({ customer }) => { |
|
137
|
// use customer... |
|
138
|
console.log(customer) |
|
139
|
}) |
|
140
|
``` |
|
141
|
|
|
142
|
</CodeTab> |
|
143
|
</CodeTabs> |
|
144
|
|
|
145
|
In the example above, you send a request to the [Update Customer API route](!api!/store/customers/update-customer) to update the customer's details. |
|
146
|
|
|
147
|
The response of the request has a `customer` field which is a [customer object](!api!/store/customers/schema). |