|
1
|
import { ArrowPath, CurrencyDollar, Envelope, FlyingBox } from "@medusajs/icons" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { Avatar, Container, Copy, Heading, Text } from "@medusajs/ui" |
|
4
|
import { Link } from "react-router-dom" |
|
5
|
import { |
|
6
|
getFormattedAddress, |
|
7
|
isSameAddress, |
|
8
|
} from "../../lib/utils/address-utils" |
|
9
|
import { getOrderCustomer } from "../../lib/utils/order-utils" |
|
10
|
import { ActionMenu } from "../common/action-menu" |
|
11
|
|
|
12
|
interface CustomerSectionProps { |
|
13
|
order: HttpTypes.AdminOrder |
|
14
|
} |
|
15
|
|
|
16
|
export const CustomerSection = ({ order }: CustomerSectionProps) => { |
|
17
|
return ( |
|
18
|
<Container className="p-0 divide-y"> |
|
19
|
<Header /> |
|
20
|
<ID order={order} /> |
|
21
|
<Contact order={order} /> |
|
22
|
<Addresses order={order} /> |
|
23
|
</Container> |
|
24
|
) |
|
25
|
} |
|
26
|
|
|
27
|
const Header = () => { |
|
28
|
return ( |
|
29
|
<div className="flex items-center justify-between px-6 py-4 gap-2"> |
|
30
|
<Heading level="h2">Customer</Heading> |
|
31
|
<ActionMenu |
|
32
|
groups={[ |
|
33
|
{ |
|
34
|
actions: [ |
|
35
|
{ |
|
36
|
label: "Transfer ownership", |
|
37
|
to: "transfer-ownership", |
|
38
|
icon: <ArrowPath />, |
|
39
|
}, |
|
40
|
], |
|
41
|
}, |
|
42
|
{ |
|
43
|
actions: [ |
|
44
|
{ |
|
45
|
label: "Edit shipping address", |
|
46
|
to: "shipping-address", |
|
47
|
icon: <FlyingBox />, |
|
48
|
}, |
|
49
|
{ |
|
50
|
label: "Edit billing address", |
|
51
|
to: "billing-address", |
|
52
|
icon: <CurrencyDollar />, |
|
53
|
}, |
|
54
|
], |
|
55
|
}, |
|
56
|
{ |
|
57
|
actions: [ |
|
58
|
{ |
|
59
|
label: "Edit email", |
|
60
|
to: `email`, |
|
61
|
icon: <Envelope />, |
|
62
|
}, |
|
63
|
], |
|
64
|
}, |
|
65
|
]} |
|
66
|
/> |
|
67
|
</div> |
|
68
|
) |
|
69
|
} |
|
70
|
|
|
71
|
const ID = ({ order }: CustomerSectionProps) => { |
|
72
|
const id = order.customer_id |
|
73
|
const name = getOrderCustomer(order) |
|
74
|
const email = order.email |
|
75
|
const fallback = (name || email || "").charAt(0).toUpperCase() |
|
76
|
|
|
77
|
return ( |
|
78
|
<div className="text-ui-fg-subtle grid grid-cols-2 items-center px-6 py-4"> |
|
79
|
<Text size="small" leading="compact" weight="plus"> |
|
80
|
ID |
|
81
|
</Text> |
|
82
|
<Link |
|
83
|
to={`/customers/${id}`} |
|
84
|
className="focus:shadow-borders-focus rounded-[4px] outline-none transition-shadow" |
|
85
|
> |
|
86
|
<div className="flex items-center gap-x-2"> |
|
87
|
<Avatar size="2xsmall" fallback={fallback} /> |
|
88
|
<div className="flex flex-1 overflow-hidden"> |
|
89
|
<Text |
|
90
|
size="small" |
|
91
|
leading="compact" |
|
92
|
className="text-ui-fg-subtle hover:text-ui-fg-base transition-fg truncate" |
|
93
|
> |
|
94
|
{name || email} |
|
95
|
</Text> |
|
96
|
</div> |
|
97
|
</div> |
|
98
|
</Link> |
|
99
|
</div> |
|
100
|
) |
|
101
|
} |
|
102
|
|
|
103
|
const Contact = ({ order }: CustomerSectionProps) => { |
|
104
|
const phone = order.shipping_address?.phone || order.billing_address?.phone |
|
105
|
const email = order.email || "" |
|
106
|
|
|
107
|
return ( |
|
108
|
<div className="text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4"> |
|
109
|
<Text size="small" leading="compact" weight="plus"> |
|
110
|
Contact |
|
111
|
</Text> |
|
112
|
<div className="flex flex-col gap-y-2"> |
|
113
|
<div className="grid grid-cols-[1fr_20px] items-start gap-x-2"> |
|
114
|
<Text |
|
115
|
size="small" |
|
116
|
leading="compact" |
|
117
|
className="text-pretty break-all" |
|
118
|
> |
|
119
|
{email} |
|
120
|
</Text> |
|
121
|
|
|
122
|
<div className="flex justify-end"> |
|
123
|
<Copy content={email} className="text-ui-fg-muted" /> |
|
124
|
</div> |
|
125
|
</div> |
|
126
|
{phone && ( |
|
127
|
<div className="grid grid-cols-[1fr_20px] items-start gap-x-2"> |
|
128
|
<Text |
|
129
|
size="small" |
|
130
|
leading="compact" |
|
131
|
className="text-pretty break-all" |
|
132
|
> |
|
133
|
{phone} |
|
134
|
</Text> |
|
135
|
|
|
136
|
<div className="flex justify-end"> |
|
137
|
<Copy content={phone} className="text-ui-fg-muted" /> |
|
138
|
</div> |
|
139
|
</div> |
|
140
|
)} |
|
141
|
</div> |
|
142
|
</div> |
|
143
|
) |
|
144
|
} |
|
145
|
|
|
146
|
const AddressPrint = ({ |
|
147
|
address, |
|
148
|
type, |
|
149
|
}: { |
|
150
|
address: |
|
151
|
| HttpTypes.AdminOrder["shipping_address"] |
|
152
|
| HttpTypes.AdminOrder["billing_address"] |
|
153
|
type: "shipping" | "billing" |
|
154
|
}) => { |
|
155
|
return ( |
|
156
|
<div className="text-ui-fg-subtle grid grid-cols-2 items-start px-6 py-4"> |
|
157
|
<Text size="small" leading="compact" weight="plus"> |
|
158
|
{type === "shipping" ? "Shipping address" : "Billing address"} |
|
159
|
</Text> |
|
160
|
{address ? ( |
|
161
|
<div className="grid grid-cols-[1fr_20px] items-start gap-x-2"> |
|
162
|
<Text size="small" leading="compact"> |
|
163
|
{getFormattedAddress(address).map((line, i) => { |
|
164
|
return ( |
|
165
|
<span key={i} className="break-words"> |
|
166
|
{line} |
|
167
|
<br /> |
|
168
|
</span> |
|
169
|
) |
|
170
|
})} |
|
171
|
</Text> |
|
172
|
<div className="flex justify-end"> |
|
173
|
<Copy |
|
174
|
content={getFormattedAddress(address).join("\n")} |
|
175
|
className="text-ui-fg-muted" |
|
176
|
/> |
|
177
|
</div> |
|
178
|
</div> |
|
179
|
) : ( |
|
180
|
<Text size="small" leading="compact"> |
|
181
|
- |
|
182
|
</Text> |
|
183
|
)} |
|
184
|
</div> |
|
185
|
) |
|
186
|
} |
|
187
|
|
|
188
|
const Addresses = ({ order }: CustomerSectionProps) => { |
|
189
|
return ( |
|
190
|
<div className="divide-y"> |
|
191
|
<AddressPrint address={order.shipping_address} type="shipping" /> |
|
192
|
{!isSameAddress(order.shipping_address, order.billing_address) ? ( |
|
193
|
<AddressPrint address={order.billing_address} type="billing" /> |
|
194
|
) : ( |
|
195
|
<div className="grid grid-cols-2 items-center px-6 py-4"> |
|
196
|
<Text |
|
197
|
size="small" |
|
198
|
leading="compact" |
|
199
|
weight="plus" |
|
200
|
className="text-ui-fg-subtle" |
|
201
|
> |
|
202
|
Billing address |
|
203
|
</Text> |
|
204
|
<Text size="small" leading="compact" className="text-ui-fg-muted"> |
|
205
|
Same as shipping address |
|
206
|
</Text> |
|
207
|
</div> |
|
208
|
)} |
|
209
|
</div> |
|
210
|
) |
|
211
|
} |