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