|
1
|
import { XMark } from "@medusajs/icons" |
|
2
|
import { HttpTypes } from "@medusajs/types" |
|
3
|
import { Badge, IconButton, Skeleton, Text } from "@medusajs/ui" |
|
4
|
import { useCustomerAddress } from "../../hooks/api/customers" |
|
5
|
import { getFormattedAddress } from "../../lib/utils/address-utils" |
|
6
|
|
|
7
|
interface AddressCardProps { |
|
8
|
customerId: string |
|
9
|
addressId: string |
|
10
|
tag?: "shipping" | "billing" |
|
11
|
onRemove?: () => void | Promise<void> |
|
12
|
} |
|
13
|
|
|
14
|
export const AddressCard = ({ |
|
15
|
customerId, |
|
16
|
addressId, |
|
17
|
tag = "shipping", |
|
18
|
onRemove, |
|
19
|
}: AddressCardProps) => { |
|
20
|
const { address, isPending, isError, error } = useCustomerAddress( |
|
21
|
customerId, |
|
22
|
addressId |
|
23
|
) |
|
24
|
|
|
25
|
if (isError) { |
|
26
|
throw error |
|
27
|
} |
|
28
|
|
|
29
|
const isReady = !isPending && !!address |
|
30
|
|
|
31
|
return ( |
|
32
|
<div className="px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center gap-4"> |
|
33
|
{!isReady ? <LoadingState /> : <AddressInfo address={address} />} |
|
34
|
<Badge size="2xsmall"> |
|
35
|
{tag === "shipping" ? "Shipping" : "Billing"} |
|
36
|
</Badge> |
|
37
|
{onRemove && ( |
|
38
|
<IconButton |
|
39
|
className="shrink-0" |
|
40
|
variant="transparent" |
|
41
|
size="small" |
|
42
|
onClick={onRemove} |
|
43
|
type="button" |
|
44
|
> |
|
45
|
<XMark /> |
|
46
|
</IconButton> |
|
47
|
)} |
|
48
|
</div> |
|
49
|
) |
|
50
|
} |
|
51
|
|
|
52
|
const LoadingState = () => { |
|
53
|
return ( |
|
54
|
<div className="flex flex-col gap-0.5"> |
|
55
|
<Skeleton className="w-20 h-5" /> |
|
56
|
<Skeleton className="w-16 h-5" /> |
|
57
|
<Skeleton className="w-16 h-5" /> |
|
58
|
</div> |
|
59
|
) |
|
60
|
} |
|
61
|
|
|
62
|
interface AddressInfoProps { |
|
63
|
address: HttpTypes.AdminCustomerAddress |
|
64
|
} |
|
65
|
|
|
66
|
const AddressInfo = ({ address }: AddressInfoProps) => { |
|
67
|
const addressSegments = getFormattedAddress(address) |
|
68
|
|
|
69
|
return ( |
|
70
|
<div className="flex flex-col flex-1"> |
|
71
|
{address.address_name && ( |
|
72
|
<Text size="small" weight="plus" leading="compact"> |
|
73
|
{address.address_name} |
|
74
|
</Text> |
|
75
|
)} |
|
76
|
{addressSegments.map((segment, idx) => ( |
|
77
|
<Text size="small" leading="compact" className="text-ui-fg-subtle"> |
|
78
|
{segment} |
|
79
|
{idx < addressSegments.length - 1 && ", "} |
|
80
|
</Text> |
|
81
|
))} |
|
82
|
</div> |
|
83
|
) |
|
84
|
} |