-
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.
refactor: reuse contact box in case pages (#1035)
- Loading branch information
Showing
8 changed files
with
152 additions
and
256 deletions.
There are no files selected for viewing
84 changes: 17 additions & 67 deletions
84
src/components/customerCases/customerCase/contactInformation/ContactInformation.tsx
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 |
---|---|---|
@@ -1,83 +1,33 @@ | ||
import { getTranslations } from "next-intl/server"; | ||
import { Suspense } from "react"; | ||
|
||
import { EmployeeCardSkeleton } from "src/components/employeeCard/EmployeeCard"; | ||
import Text from "src/components/text/Text"; | ||
import { fetchEmployeesByEmails } from "src/utils/employees"; | ||
import { CompanyLocation } from "studio/lib/interfaces/companyDetails"; | ||
import { COMPANY_LOCATIONS_QUERY } from "studio/lib/queries/admin"; | ||
import { EMPLOYEE_PAGE_SLUG_QUERY } from "studio/lib/queries/siteSettings"; | ||
import { loadStudioQuery } from "studio/lib/store"; | ||
import ContactBoxContent from "src/components/sections/contact-box/ContactBoxContent"; | ||
|
||
import styles from "./contactInformation.module.css"; | ||
import ContactSelector, { ContactByLocationMap } from "./ContactSelector"; | ||
import { fetchLocationsWithContact, getEmployeePageSlug } from "./api"; | ||
|
||
interface ContactInformationProps { | ||
language: string; | ||
} | ||
|
||
async function fetchContactByLocationMap( | ||
companyLocations: CompanyLocation[], | ||
): Promise<ContactByLocationMap> { | ||
const contactByLocation: ContactByLocationMap = {}; | ||
for (const location of companyLocations) { | ||
if ( | ||
location.contactPerson === undefined || | ||
location.contactPerson.length === 0 | ||
) { | ||
continue; | ||
} | ||
const contactRes = await fetchEmployeesByEmails([location.contactPerson]); | ||
if (!contactRes.ok || contactRes.value.length === 0) { | ||
continue; | ||
} | ||
contactByLocation[location._id] = contactRes.value[0]; | ||
} | ||
return contactByLocation; | ||
} | ||
|
||
export default async function ContactInformation({ | ||
language, | ||
}: ContactInformationProps) { | ||
const employeePageSlug = ( | ||
await loadStudioQuery<{ slug: string } | null>(EMPLOYEE_PAGE_SLUG_QUERY, { | ||
language, | ||
}) | ||
).data?.slug; | ||
const companyLocations = ( | ||
await loadStudioQuery<CompanyLocation[]>(COMPANY_LOCATIONS_QUERY, {}) | ||
).data; | ||
const contactByLocation = await fetchContactByLocationMap(companyLocations); | ||
const locationIdsWithContact = Object.keys(contactByLocation); | ||
const locationsWithContact = companyLocations.filter((location) => | ||
locationIdsWithContact.includes(location._id), | ||
); | ||
const employeePageSlug = await getEmployeePageSlug(language); | ||
const contactPoints = fetchLocationsWithContact(); | ||
|
||
const t = await getTranslations("contact_information"); | ||
return ( | ||
<> | ||
<section className={styles.contactBox}> | ||
<div | ||
className={`${styles.contactBox__inner} ${styles["contactBox__inner--light"]}`} | ||
> | ||
<div className={styles.textContent}> | ||
<Text type="h3" as="h2"> | ||
{t("help")} | ||
</Text> | ||
<Text type="bodyBig">{t("contact_sale")} </Text> | ||
</div> | ||
<div className={styles.contactSelectorWrapper}> | ||
<Suspense fallback={<EmployeeCardSkeleton background={"light"} />}> | ||
<ContactSelector | ||
language={language} | ||
locations={locationsWithContact} | ||
contactByLocation={contactByLocation} | ||
employeePageSlug={employeePageSlug} | ||
background={"light"} | ||
/> | ||
</Suspense> | ||
</div> | ||
</div> | ||
</section> | ||
</> | ||
<Suspense fallback={<div>Loading...</div>}> | ||
<ContactBoxContent | ||
data={{ | ||
basicTitle: t("help"), | ||
optionalSubtitle: t("contact_sale"), | ||
background: "light", | ||
}} | ||
language={language} | ||
employeesPageSlug={employeePageSlug} | ||
contactPoints={contactPoints} | ||
/> | ||
</Suspense> | ||
); | ||
} |
74 changes: 0 additions & 74 deletions
74
src/components/customerCases/customerCase/contactInformation/ContactSelector.tsx
This file was deleted.
Oops, something went wrong.
65 changes: 65 additions & 0 deletions
65
src/components/customerCases/customerCase/contactInformation/api.ts
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,65 @@ | ||
import { EmployeeAndMetadata } from "src/components/sections/contact-box/types"; | ||
import { fetchEmployeesByEmails } from "src/utils/employees"; | ||
import { CompanyLocation } from "studio/lib/interfaces/companyDetails"; | ||
import { COMPANY_LOCATIONS_QUERY } from "studio/lib/queries/admin"; | ||
import { EMPLOYEE_PAGE_SLUG_QUERY } from "studio/lib/queries/siteSettings"; | ||
import { loadStudioQuery } from "studio/lib/store"; | ||
|
||
export async function getEmployeePageSlug(language: string) { | ||
const employeePageSlug = | ||
( | ||
await loadStudioQuery<{ slug: string } | null>(EMPLOYEE_PAGE_SLUG_QUERY, { | ||
language, | ||
}) | ||
).data?.slug ?? ""; | ||
|
||
return employeePageSlug; | ||
} | ||
|
||
export async function fetchLocationsWithContact(): Promise< | ||
EmployeeAndMetadata[] | ||
> { | ||
const companyLocations = ( | ||
await loadStudioQuery<CompanyLocation[]>(COMPANY_LOCATIONS_QUERY, {}) | ||
).data; | ||
const contactByLocation = await fetchContactByLocationMap(companyLocations); | ||
|
||
return contactByLocation; | ||
} | ||
|
||
async function fetchContactByLocationMap( | ||
companyLocations: CompanyLocation[], | ||
): Promise<EmployeeAndMetadata[]> { | ||
const uniqueEmails = Array.from( | ||
new Set( | ||
companyLocations | ||
.map((location) => location.contactPerson) | ||
.filter((contactPerson) => contactPerson !== undefined), | ||
), | ||
); | ||
|
||
const contactRes = await fetchEmployeesByEmails(uniqueEmails); | ||
|
||
if (!contactRes.ok) { | ||
return []; | ||
} | ||
|
||
const contactByLocation: EmployeeAndMetadata[] = []; | ||
|
||
for (const location of companyLocations) { | ||
const employee = contactRes.value.find( | ||
(employee) => employee.email === location.contactPerson, | ||
); | ||
|
||
if (!employee || !location?.companyLocationName) { | ||
continue; | ||
} | ||
|
||
contactByLocation.push({ | ||
employee, | ||
tag: location.companyLocationName, | ||
tagSlug: location._id, | ||
}); | ||
} | ||
return contactByLocation; | ||
} |
74 changes: 0 additions & 74 deletions
74
src/components/customerCases/customerCase/contactInformation/contactInformation.module.css
This file was deleted.
Oops, something went wrong.
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.