Skip to content

Commit 37fcb42

Browse files
author
apav-dev
committed
Refactor address handling across multiple components to use SafeAddress for improved content validation and consistency.
1 parent ecac34e commit 37fcb42

8 files changed

Lines changed: 70 additions & 32 deletions

File tree

starter/src/registry/hs1-albany-new-patients/components/Hs1AlbanyNewPatientsLocationSection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ComponentConfig, Fields, PuckComponent } from "@puckeditor/core";
2-
import { Address } from "@yext/pages-components";
2+
import { Address } from "../../shared/SafeAddress";
33
import {
44
resolveComponentData,
55
TranslatableString,

starter/src/registry/hs1-albany-office/components/Hs1AlbanyOfficeContentSection.tsx

Lines changed: 21 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ import {
99
YextEntityFieldSelector,
1010
} from "@yext/visual-editor";
1111
import {
12-
Address,
1312
ComplexImageType,
1413
HoursTable,
1514
HoursType,
1615
ImageType,
17-
Link,
1816
} from "@yext/pages-components";
17+
import { Address } from "../../shared/SafeAddress";
18+
import { Link } from "../../shared/SafeLink";
1919

2020
type StyledTextProps = {
2121
text: YextEntityField<TranslatableString>;
@@ -325,6 +325,7 @@ export const Hs1AlbanyOfficeContentSectionComponent: PuckComponent<
325325
> = (props) => {
326326
const streamDocument = useDocument() as Record<string, unknown>;
327327
const locale = (streamDocument.locale as string) ?? "en";
328+
const address = streamDocument.address as Record<string, unknown> | undefined;
328329
const officeImage = resolveComponentData(
329330
props.officeImage,
330331
locale,
@@ -449,20 +450,24 @@ export const Hs1AlbanyOfficeContentSectionComponent: PuckComponent<
449450

450451
{renderHeading(props.officeName, officeName)}
451452
<div className="space-y-2 font-['Arial','Helvetica',sans-serif] text-[15px] leading-[1.65] text-[#4a4a4a]">
452-
<Address
453-
address={streamDocument.address as never}
454-
lines={[["line1"], ["city", ",", "region", "postalCode"]]}
455-
className="not-italic"
456-
/>
457-
<Link
458-
cta={{
459-
link: mainPhone,
460-
linkType: "PHONE",
461-
}}
462-
className="inline-block"
463-
>
464-
{mainPhone}
465-
</Link>
453+
{address ? (
454+
<Address
455+
address={address as never}
456+
lines={[["line1"], ["city", ",", "region", "postalCode"]]}
457+
className="not-italic"
458+
/>
459+
) : null}
460+
{mainPhone ? (
461+
<Link
462+
cta={{
463+
link: mainPhone,
464+
linkType: "PHONE",
465+
}}
466+
className="inline-block"
467+
>
468+
{mainPhone}
469+
</Link>
470+
) : null}
466471
</div>
467472

468473
{renderHeading(props.officeHoursTitle, officeHoursTitle)}

starter/src/registry/hs1-albany-office/components/Hs1AlbanyOfficeLocationSection.tsx

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
YextEntityField,
77
YextEntityFieldSelector,
88
} from "@yext/visual-editor";
9-
import { Address } from "@yext/pages-components";
9+
import { Address } from "../../shared/SafeAddress";
1010
import { Link } from "../../shared/SafeLink";
1111

1212
type StyledTextProps = {
@@ -219,7 +219,7 @@ export const Hs1AlbanyOfficeLocationSectionComponent: PuckComponent<
219219
{addressLabel}
220220
</p>
221221
<Address
222-
address={streamDocument.address as never}
222+
address={address as never}
223223
lines={[
224224
["line1"],
225225
["city", ",", "region", "postalCode", ",", "countryCode"],
@@ -239,15 +239,17 @@ export const Hs1AlbanyOfficeLocationSectionComponent: PuckComponent<
239239
>
240240
{contactLabel}
241241
</p>
242-
<Link
243-
cta={{
244-
link: mainPhone,
245-
linkType: "PHONE",
246-
}}
247-
className="font-['Arial','Helvetica',sans-serif] text-[14px] text-[#4a4a4a]"
248-
>
249-
{mainPhone}
250-
</Link>
242+
{mainPhone ? (
243+
<Link
244+
cta={{
245+
link: mainPhone,
246+
linkType: "PHONE",
247+
}}
248+
className="font-['Arial','Helvetica',sans-serif] text-[14px] text-[#4a4a4a]"
249+
>
250+
{mainPhone}
251+
</Link>
252+
) : null}
251253
</div>
252254
<div className="min-h-[320px] border border-[#dddddd] bg-white">
253255
<iframe

starter/src/registry/hs1-albany-services/components/Hs1AlbanyServicesLocationSection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
type YextEntityField,
1111
YextEntityFieldSelector,
1212
} from "@yext/visual-editor";
13-
import { Address, type AddressLine } from "@yext/pages-components";
13+
import { Address, type AddressLine } from "../../shared/SafeAddress";
1414
import { Link } from "../../shared/SafeLink";
1515

1616
type StyledTextProps = {

starter/src/registry/hs1-albany-staff/components/Hs1AlbanyStaffLocationSection.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import {
66
YextEntityField,
77
YextEntityFieldSelector,
88
} from "@yext/visual-editor";
9-
import { Address } from "@yext/pages-components";
9+
import { Address } from "../../shared/SafeAddress";
1010
import { Link } from "../../shared/SafeLink";
1111

1212
type StyledTextProps = {

starter/src/registry/hs1-carmel/components/Hs1CarmelLocationSection.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
YextEntityFieldSelector,
77
resolveComponentData,
88
} from "@yext/visual-editor";
9-
import { Address, HoursStatus, HoursTable } from "@yext/pages-components";
9+
import { HoursStatus, HoursTable } from "@yext/pages-components";
10+
import { Address } from "../../shared/SafeAddress";
1011
import { Link } from "../../shared/SafeLink";
1112

1213
type StyledTextProps = {

starter/src/registry/hs1-chicago/components/Hs1ChicagoLocationHoursSection.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {
66
YextEntityField,
77
YextEntityFieldSelector,
88
} from "@yext/visual-editor";
9-
import { Address, HoursTable } from "@yext/pages-components";
9+
import { HoursTable } from "@yext/pages-components";
10+
import { Address } from "../../shared/SafeAddress";
1011

1112
type StyledTextProps = {
1213
text: YextEntityField<TranslatableString>;
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import type { ComponentProps } from "react";
2+
import { Address as PagesAddress } from "@yext/pages-components";
3+
4+
type PagesAddressProps = ComponentProps<typeof PagesAddress>;
5+
6+
const hasAddressContent = (address: PagesAddressProps["address"]): boolean => {
7+
if (!address) {
8+
return false;
9+
}
10+
11+
return Boolean(
12+
address.line1 ||
13+
address.line2 ||
14+
address.city ||
15+
address.region ||
16+
address.postalCode ||
17+
address.countryCode,
18+
);
19+
};
20+
21+
export const Address = (props: PagesAddressProps) => {
22+
if (!hasAddressContent(props.address)) {
23+
return null;
24+
}
25+
26+
return <PagesAddress {...props} />;
27+
};
28+
29+
export type { AddressLine } from "@yext/pages-components";

0 commit comments

Comments
 (0)