-
-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathindex.tsx
74 lines (62 loc) · 1.75 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
"use client"
import React, { useEffect, useActionState } from "react"
import Input from "@modules/common/components/input"
import AccountInfo from "../account-info"
import { HttpTypes } from "@medusajs/types"
import { updateCustomer } from "@lib/data/customer"
type MyInformationProps = {
customer: HttpTypes.StoreCustomer
}
const ProfileEmail: React.FC<MyInformationProps> = ({ customer }) => {
const [successState, setSuccessState] = React.useState(false)
const updateCustomerPhone = async (
_currentState: Record<string, unknown>,
formData: FormData
) => {
const customer = {
phone: formData.get("phone") as string,
}
try {
await updateCustomer(customer)
return { success: true, error: null }
} catch (error: any) {
return { success: false, error: error.toString() }
}
}
const [state, formAction] = useActionState(updateCustomerPhone, {
error: false,
success: false,
})
const clearState = () => {
setSuccessState(false)
}
useEffect(() => {
setSuccessState(state.success)
}, [state])
return (
<form action={formAction} className="w-full">
<AccountInfo
label="Phone"
currentInfo={`${customer.phone}`}
isSuccess={successState}
isError={!!state.error}
errorMessage={state.error}
clearState={clearState}
data-testid="account-phone-editor"
>
<div className="grid grid-cols-1 gap-y-2">
<Input
label="Phone"
name="phone"
type="phone"
autoComplete="phone"
required
defaultValue={customer.phone ?? ""}
data-testid="phone-input"
/>
</div>
</AccountInfo>
</form>
)
}
export default ProfileEmail