-
-
Notifications
You must be signed in to change notification settings - Fork 667
/
Copy pathindex.tsx
185 lines (172 loc) · 6.59 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"use client"
import { setAddresses } from "@lib/data/cart"
import compareAddresses from "@lib/util/compare-addresses"
import { CheckCircleSolid } from "@medusajs/icons"
import { HttpTypes } from "@medusajs/types"
import { Heading, Text, useToggleState } from "@medusajs/ui"
import Divider from "@modules/common/components/divider"
import Spinner from "@modules/common/icons/spinner"
import { usePathname, useRouter, useSearchParams } from "next/navigation"
import { useActionState } from "react"
import BillingAddress from "../billing_address"
import ErrorMessage from "../error-message"
import ShippingAddress from "../shipping-address"
import { SubmitButton } from "../submit-button"
const Addresses = ({
cart,
customer,
}: {
cart: HttpTypes.StoreCart | null
customer: HttpTypes.StoreCustomer | null
}) => {
const searchParams = useSearchParams()
const router = useRouter()
const pathname = usePathname()
const isOpen = searchParams.get("step") === "address"
const { state: sameAsBilling, toggle: toggleSameAsBilling } = useToggleState(
cart?.shipping_address && cart?.billing_address
? compareAddresses(cart?.shipping_address, cart?.billing_address)
: true
)
const handleEdit = () => {
router.push(pathname + "?step=address")
}
const [message, formAction] = useActionState(setAddresses, null)
return (
<div className="bg-white">
<div className="flex flex-row items-center justify-between mb-6">
<Heading
level="h2"
className="flex flex-row text-3xl-regular gap-x-2 items-baseline"
>
Shipping Address
{!isOpen && <CheckCircleSolid />}
</Heading>
{!isOpen && cart?.shipping_address && (
<Text>
<button
onClick={handleEdit}
className="text-ui-fg-interactive hover:text-ui-fg-interactive-hover"
data-testid="edit-address-button"
>
Edit
</button>
</Text>
)}
</div>
{isOpen ? (
<form action={formAction}>
<div className="pb-8">
<ShippingAddress
customer={customer}
checked={sameAsBilling}
onChange={toggleSameAsBilling}
cart={cart}
/>
{!sameAsBilling && (
<div>
<Heading
level="h2"
className="text-3xl-regular gap-x-4 pb-6 pt-8"
>
Billing address
</Heading>
<BillingAddress cart={cart} />
</div>
)}
<SubmitButton className="mt-6" data-testid="submit-address-button">
Continue to delivery
</SubmitButton>
<ErrorMessage error={message} data-testid="address-error-message" />
</div>
</form>
) : (
<div>
<div className="text-small-regular">
{cart && cart.shipping_address ? (
<div className="flex items-start gap-x-8">
<div className="flex items-start gap-x-1 w-full">
<div
className="flex flex-col w-1/3"
data-testid="shipping-address-summary"
>
<Text className="txt-medium-plus text-ui-fg-base mb-1">
Shipping Address
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.shipping_address.first_name || ""}{" "}
{cart.shipping_address.last_name || ""}
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.shipping_address.address_1 || ""}{" "}
{cart.shipping_address.address_2 || ""}
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.shipping_address.postal_code || ""},{" "}
{cart.shipping_address.city || ""}
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.shipping_address.country_code?.toUpperCase() || ""}
</Text>
</div>
<div
className="flex flex-col w-1/3 "
data-testid="shipping-contact-summary"
>
<Text className="txt-medium-plus text-ui-fg-base mb-1">
Contact
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.shipping_address.phone || ""}
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.email || ""}
</Text>
</div>
<div
className="flex flex-col w-1/3"
data-testid="billing-address-summary"
>
<Text className="txt-medium-plus text-ui-fg-base mb-1">
Billing Address
</Text>
{sameAsBilling ? (
<Text className="txt-medium text-ui-fg-subtle">
Billing- and delivery address are the same.
</Text>
) : (
<>
<Text className="txt-medium text-ui-fg-subtle">
{cart.billing_address?.first_name || ""}{" "}
{cart.billing_address?.last_name || ""}
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.billing_address?.address_1 || ""}{" "}
{cart.billing_address?.address_2 || ""}
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.billing_address?.postal_code || ""},{" "}
{cart.billing_address?.city || ""}
</Text>
<Text className="txt-medium text-ui-fg-subtle">
{cart.billing_address?.country_code?.toUpperCase() ||
""}
</Text>
</>
)}
</div>
</div>
</div>
) : (
<div>
<Spinner />
</div>
)}
</div>
</div>
)}
<Divider className="mt-8" />
</div>
)
}
export default Addresses