Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 - improve type narrowing for query response data #785

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions src/app/(main)/[lang]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Metadata } from "next";

import InformationSection from "src/components/informationSection/InformationSection";
import { getDraftModeInfo } from "src/utils/draftmode";
import { isNonNullQueryResponse } from "src/utils/queryResponse";
import SectionRenderer from "src/utils/renderSection";
import { generateMetadataFromSeo } from "src/utils/seo";
import { LinkType } from "studio/lib/interfaces/navigation";
Expand Down Expand Up @@ -40,7 +41,7 @@ const Home = async ({ params }: Props) => {
{ perspective },
);

if (initialLandingPage.data === null) {
if (!isNonNullQueryResponse(initialLandingPage)) {
return (
<InformationSection
title="Welcome! Velkommen! Välkommen!"
Expand All @@ -52,17 +53,12 @@ const Home = async ({ params }: Props) => {
);
}

const initialData = {
...initialLandingPage,
data: initialLandingPage.data,
};

return initialLandingPage.data.sections.map((section, index) => (
<SectionRenderer
key={section._key}
section={section}
isDraftMode={isDraftMode}
initialData={initialData}
initialData={initialLandingPage}
isLandingPage={true}
sectionIndex={index}
/>
Expand Down
53 changes: 17 additions & 36 deletions src/utils/pageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import { CUSTOMER_CASE_QUERY } from "studioShared/lib/queries/customerCases";
import { loadSharedQuery } from "studioShared/lib/store";
import { customerCaseID } from "studioShared/schemas/documents/customerCase";

import { isNonNullQueryResponse } from "./queryResponse";

type PageFromParams<D, T> = {
queryResponse: D;
docType: T;
Expand All @@ -43,22 +45,19 @@ async function fetchDynamicPage({
if (path.length === 0) {
return null;
}
const pageResult = await loadStudioQuery<PageBuilder | null>(
const queryResponse = await loadStudioQuery<PageBuilder | null>(
PAGE_BY_SLUG_QUERY,
{
slug: path[0],
language,
},
{ perspective },
);
if (pageResult.data === null) {
if (!isNonNullQueryResponse(queryResponse)) {
return null;
}
return {
queryResponse: {
...pageResult,
data: pageResult.data,
},
queryResponse,
docType: pageBuilderID,
};
}
Expand Down Expand Up @@ -89,39 +88,30 @@ async function fetchCompensationsPage({
perspective,
},
);
if (compensationsPageResult.data === null) {
if (!isNonNullQueryResponse(compensationsPageResult)) {
return null;
}
const companyLocationsResult = await loadStudioQuery<CompanyLocation[]>(
COMPANY_LOCATIONS_QUERY,
{},
{ perspective },
);
if (companyLocationsResult.data === null) {
if (!isNonNullQueryResponse(companyLocationsResult)) {
return null;
}
const localeDocumentResult = await loadStudioQuery<LocaleDocument>(
LOCALE_QUERY,
{},
{ perspective },
);
if (localeDocumentResult.data === null) {
if (!isNonNullQueryResponse(localeDocumentResult)) {
return null;
}
return {
queryResponse: {
compensationsPage: {
...compensationsPageResult,
data: compensationsPageResult.data,
},
companyLocations: {
...companyLocationsResult,
data: companyLocationsResult.data,
},
locale: {
...localeDocumentResult,
data: localeDocumentResult.data,
},
compensationsPage: compensationsPageResult,
companyLocations: companyLocationsResult,
locale: localeDocumentResult,
},
docType: compensationsId,
};
Expand All @@ -148,15 +138,12 @@ async function fetchCustomerCase({
},
{ perspective },
);
if (customerCasesPageResult.data === null) {
if (!isNonNullQueryResponse(customerCasesPageResult)) {
return null;
}
if (path.length === 1) {
return {
queryResponse: {
...customerCasesPageResult,
data: customerCasesPageResult.data,
},
queryResponse: customerCasesPageResult,
docType: customerCasesPageID,
};
}
Expand All @@ -170,14 +157,11 @@ async function fetchCustomerCase({
perspective,
},
);
if (customerCaseResult.data === null) {
if (!isNonNullQueryResponse(customerCaseResult)) {
return null;
}
return {
queryResponse: {
...customerCaseResult,
data: customerCaseResult.data,
},
queryResponse: customerCaseResult,
docType: customerCaseID,
};
}
Expand All @@ -203,14 +187,11 @@ async function fetchLegalDocument({
perspective,
},
);
if (queryResponse.data === null) {
if (!isNonNullQueryResponse(queryResponse)) {
return null;
}
return {
queryResponse: {
...queryResponse,
data: queryResponse.data,
},
queryResponse,
docType: legalDocumentID,
};
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/queryResponse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { QueryResponseInitial } from "@sanity/react-loader";

export function isNonNullQueryResponse<T>(
value: QueryResponseInitial<T | null>,
): value is QueryResponseInitial<T> {
return value.data !== null;
}