-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds component and basic functionality for contact box
- Loading branch information
Showing
11 changed files
with
254 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import { Suspense } from "react"; | ||
|
||
import { EmployeeCardSkeleton } from "src/components/employeeCard/EmployeeCard"; | ||
import Text from "src/components/text/Text"; | ||
import { ChewbaccaEmployee } from "src/types/employees"; | ||
import { fetchEmployeesByEmails } from "src/utils/employees"; | ||
import { ContactBoxSection } from "studio/lib/interfaces/pages"; | ||
import { EMPLOYEE_PAGE_SLUG_QUERY } from "studio/lib/queries/siteSettings"; | ||
import { loadStudioQuery } from "studio/lib/store"; | ||
|
||
import styles from "./contact-box.module.css"; | ||
import ContactSelector, { EmployeeAndTag } from "./ContactSelector"; | ||
|
||
export interface ContactBoxProps { | ||
section: ContactBoxSection; | ||
language: string; | ||
} | ||
|
||
export default async function ContactBox({ | ||
section, | ||
language, | ||
}: ContactBoxProps) { | ||
const employeesPageRes = await loadStudioQuery<{ slug: string }>( | ||
EMPLOYEE_PAGE_SLUG_QUERY, | ||
{ | ||
language, | ||
}, | ||
); | ||
const employeesPageSlug = employeesPageRes.data.slug; | ||
|
||
const contactPoints = fetchEmployeesByEmails( | ||
section.contactPoints.map((contactPoint) => contactPoint.email), | ||
).then((result) => | ||
result.ok | ||
? result.value.map((e) => employeeAndTag(e, section.contactPoints)) | ||
: [], | ||
); | ||
return ( | ||
<section className={styles.contactBox}> | ||
<div> | ||
<Text type="h3" as="h2"> | ||
{section.basicTitle} | ||
</Text> | ||
|
||
{section.optionalSubtitle && ( | ||
<Text type="bodyBig">{section.optionalSubtitle}</Text> | ||
)} | ||
</div> | ||
|
||
<div> | ||
<Suspense fallback={<EmployeeCardSkeleton />}> | ||
<ContactSelector | ||
employeesPageSlug={employeesPageSlug} | ||
contactPoints={contactPoints} | ||
language={language} | ||
/> | ||
</Suspense> | ||
</div> | ||
</section> | ||
); | ||
} | ||
|
||
function employeeAndTag( | ||
employee: ChewbaccaEmployee, | ||
contactPoints: ContactBoxSection["contactPoints"], | ||
): EmployeeAndTag { | ||
const tag = | ||
contactPoints.find((contactPoint) => contactPoint.email === employee.email) | ||
?.tag ?? ""; | ||
return { | ||
employee, | ||
tag, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
"use client"; | ||
|
||
import { useQuery } from "@sanity/react-loader"; | ||
import { Suspense } from "react"; | ||
|
||
import { PreviewProps } from "src/types/preview"; | ||
import { ContactBoxSection, PageBuilder } from "studio/lib/interfaces/pages"; | ||
import { PAGE_QUERY } from "studio/lib/queries/pages"; | ||
|
||
import ContactBox from "./ContactBox"; | ||
|
||
export default function ContactBoxPreview({ | ||
initialData, | ||
sectionIndex, | ||
}: PreviewProps) { | ||
const { data: newData } = useQuery<PageBuilder | null>( | ||
PAGE_QUERY, | ||
{ id: initialData.data._id, language: initialData.data.language }, | ||
{ initial: initialData }, | ||
); | ||
|
||
const section = newData | ||
? (newData.sections.find( | ||
(section, index) => | ||
section._type === "contactBox" && index === sectionIndex, | ||
) as ContactBoxSection) | ||
: (initialData.data.sections.find( | ||
(section, index) => | ||
section._type === "contactBox" && index === sectionIndex, | ||
) as ContactBoxSection); | ||
|
||
return ( | ||
<Suspense> | ||
<ContactBox section={section} language={initialData.data.language} /> | ||
</Suspense> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
"use client"; | ||
|
||
import { use, useState } from "react"; | ||
|
||
import EmployeeCard from "src/components/employeeCard/EmployeeCard"; | ||
import { Tag } from "src/components/tag"; | ||
import { ChewbaccaEmployee } from "src/types/employees"; | ||
|
||
export type EmployeeAndTag = { | ||
employee: ChewbaccaEmployee; | ||
tag: string; | ||
}; | ||
|
||
export type ContactSelectorProps = { | ||
contactPoints: Promise<EmployeeAndTag[]>; | ||
employeesPageSlug: string; | ||
language: string; | ||
}; | ||
|
||
export default function ContactSelector({ | ||
contactPoints: contactPointsPromise, | ||
employeesPageSlug, | ||
language, | ||
}: ContactSelectorProps) { | ||
const contactPoints = use(contactPointsPromise); | ||
|
||
const [selectedTag, setSelectedTag] = useState<string | null>( | ||
contactPoints[0].tag, | ||
); | ||
const selectedContactPoint = contactPoints.find( | ||
(contactPoint) => selectedTag == contactPoint.tag, | ||
); | ||
|
||
return ( | ||
<div> | ||
<div> | ||
{contactPoints.map((contactPoint) => ( | ||
<Tag | ||
key={contactPoint.tag} | ||
type="button" | ||
active={selectedTag === contactPoint.tag} | ||
onClick={() => setSelectedTag(contactPoint.tag)} | ||
text={contactPoint.tag} | ||
/> | ||
))} | ||
</div> | ||
<div> | ||
{selectedContactPoint?.employee && ( | ||
<EmployeeCard | ||
employee={selectedContactPoint.employee} | ||
employeePageSlug={employeesPageSlug} | ||
language={language} | ||
/> | ||
)} | ||
</div> | ||
</div> | ||
); | ||
} |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.