Skip to content

Commit

Permalink
Merge pull request #503 from varianter/refactor/v3-inline-benefit-com…
Browse files Browse the repository at this point in the history
…ponent

v3 - inline benefit component
  • Loading branch information
mathiazom authored Aug 23, 2024
2 parents 6e7ee1a + 1e53edd commit 67eb27a
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 50 deletions.
35 changes: 24 additions & 11 deletions src/app/(main)/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Metadata } from "next";
import { redirect } from "next/navigation";
import { Blog } from "src/blog/Blog";
import BlogPreview from "src/blog/BlogPreview";
import SalaryAndBenefits from 'src/salaryAndBenefits/SalaryAndBenefits';
import SalaryAndBenefits from "src/salaryAndBenefits/SalaryAndBenefits";
import { getDraftModeInfo } from "src/utils/draftmode";
import SectionRenderer from "src/utils/renderSection";
import { fetchSeoData, generateMetadataFromSeo } from "src/utils/seo";
import { BlogPage, PageBuilder, Post } from "studio/lib/payloads/pages";
import { SalaryAndBenefits as SalaryAndBenefitsPayload } from 'studio/lib/payloads/salaryAndBenefits';
import { SalaryAndBenefitsPage } from "studio/lib/payloads/salaryAndBenefits";
import {
BLOG_PAGE_QUERY,
POSTS_QUERY,
Expand Down Expand Up @@ -36,13 +36,22 @@ async function Page({ params }: Props) {
const { slug } = params;
const { perspective, isDraftMode } = getDraftModeInfo();

const [initialPage, initialBlogPage, initialSalaryAndBenefitsPage] = await Promise.all([
loadQuery<PageBuilder>(SLUG_QUERY, { slug }, { perspective }),
loadQuery<BlogPage>(BLOG_PAGE_QUERY, { slug }, { perspective }),
loadQuery<SalaryAndBenefitsPayload>(SALARY_AND_BENEFITS_PAGE_QUERY, { slug }, { perspective }),
]);
const [initialPage, initialBlogPage, initialSalaryAndBenefitsPage] =
await Promise.all([
loadQuery<PageBuilder>(SLUG_QUERY, { slug }, { perspective }),
loadQuery<BlogPage>(BLOG_PAGE_QUERY, { slug }, { perspective }),
loadQuery<SalaryAndBenefitsPage>(
SALARY_AND_BENEFITS_PAGE_QUERY,
{ slug },
{ perspective },
),
]);

if (!initialPage.data && !initialBlogPage.data && !initialSalaryAndBenefitsPage.data) {
if (
!initialPage.data &&
!initialBlogPage.data &&
!initialSalaryAndBenefitsPage.data
) {
console.log(`Page ${slug} not found`);
// TODO: add error snackbar
redirect("/");
Expand All @@ -53,7 +62,7 @@ async function Page({ params }: Props) {
const initialPosts = await loadQuery<Post[]>(
POSTS_QUERY,
{ slug },
{ perspective }
{ perspective },
);

if (!initialPosts) {
Expand Down Expand Up @@ -95,9 +104,13 @@ async function Page({ params }: Props) {

if (initialSalaryAndBenefitsPage.data) {
return isDraftMode ? (
<SalaryAndBenefitsPreview initialSalaryAndBenefits={initialSalaryAndBenefitsPage} />
<SalaryAndBenefitsPreview
initialSalaryAndBenefits={initialSalaryAndBenefitsPage}
/>
) : (
<SalaryAndBenefits salaryAndBenefits={initialSalaryAndBenefitsPage.data} />
<SalaryAndBenefits
salaryAndBenefits={initialSalaryAndBenefitsPage.data}
/>
);
}

Expand Down
19 changes: 11 additions & 8 deletions src/salaryAndBenefits/SalaryAndBenefits.tsx
Original file line number Diff line number Diff line change
@@ -1,23 +1,26 @@
import styles from "./salaryAndBenefits.module.css";
import Text from "src/components/text/Text";
import {SalaryAndBenefits as SalaryAndBenefitsPayload} from "studio/lib/payloads/salaryAndBenefits";
import Benefit from './components/benefit/Benefit';
import { SalaryAndBenefitsPage } from "studio/lib/payloads/salaryAndBenefits";
import { RichText } from "src/components/richText/RichText";

interface SalaryAndBenefitsProps {
salaryAndBenefits: SalaryAndBenefitsPayload
salaryAndBenefits: SalaryAndBenefitsPage;
}

const SalaryAndBenefits = ({salaryAndBenefits}: SalaryAndBenefitsProps) => {
const SalaryAndBenefits = ({ salaryAndBenefits }: SalaryAndBenefitsProps) => {
return (
<div className={styles.wrapper}>
<Text type="h1">{salaryAndBenefits.basicTitle}</Text>
<div className={styles.benefits}>
{salaryAndBenefits.benefits.map((benefit) => (
<Benefit key={benefit._key} benefit={benefit} />
<div key={benefit._key} className={styles.benefitWrapper}>
<Text type="h2">{benefit.basicTitle}</Text>
<RichText value={benefit.richText} />
</div>
))}
</div>
</div>
)
}
);
};

export default SalaryAndBenefits;
export default SalaryAndBenefits;
6 changes: 3 additions & 3 deletions src/salaryAndBenefits/SalaryAndBenefitsPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
import { Suspense } from "react";
import SalaryAndBenefits from "./SalaryAndBenefits";
import { QueryResponseInitial, useQuery } from "@sanity/react-loader";
import { SalaryAndBenefits as SalaryAndBenefitsPayload } from "studio/lib/payloads/salaryAndBenefits";
import { SalaryAndBenefitsPage } from "studio/lib/payloads/salaryAndBenefits";
import { SALARY_AND_BENEFITS_PAGE_QUERY } from "studio/lib/queries/pages";

interface SalaryAndBenefitsPreviewProps {
initialSalaryAndBenefits: QueryResponseInitial<SalaryAndBenefitsPayload>;
initialSalaryAndBenefits: QueryResponseInitial<SalaryAndBenefitsPage>;
}

const SalaryAndBenefitsPreview = ({
initialSalaryAndBenefits,
}: SalaryAndBenefitsPreviewProps) => {
const { data: newData } = useQuery<SalaryAndBenefitsPayload>(
const { data: newData } = useQuery<SalaryAndBenefitsPage>(
SALARY_AND_BENEFITS_PAGE_QUERY,
{ slug: initialSalaryAndBenefits.data.slug.current },
{ initial: initialSalaryAndBenefits },
Expand Down
18 changes: 0 additions & 18 deletions src/salaryAndBenefits/components/benefit/Benefit.tsx

This file was deleted.

5 changes: 0 additions & 5 deletions src/salaryAndBenefits/components/benefit/benefit.module.css

This file was deleted.

6 changes: 6 additions & 0 deletions src/salaryAndBenefits/salaryAndBenefits.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,10 @@
flex-direction: column;
gap: 5rem;
max-width: var(--max-content-width-medium);
}

.benefitWrapper {
display: flex;
flex-direction: column;
gap: 1rem;
}
9 changes: 4 additions & 5 deletions studio/lib/payloads/salaryAndBenefits.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { PortableTextBlock } from 'src/components/richText/RichText';
import { Slug } from './global';
import { PortableTextBlock } from "src/components/richText/RichText";
import { Slug } from "./global";

export interface Benefit {
_type: string;
Expand All @@ -8,8 +8,7 @@ export interface Benefit {
richText: PortableTextBlock[];
}


export interface SalaryAndBenefits {
export interface SalaryAndBenefitsPage {
_createdAt: string;
_id: string;
_rev: string;
Expand All @@ -19,4 +18,4 @@ export interface SalaryAndBenefits {
page: string;
slug: Slug;
benefits: Benefit[];
}
}

0 comments on commit 67eb27a

Please sign in to comment.