diff --git a/src/app/(main)/[lang]/page.tsx b/src/app/(main)/[lang]/page.tsx index 58e067bca..b8644b5c7 100644 --- a/src/app/(main)/[lang]/page.tsx +++ b/src/app/(main)/[lang]/page.tsx @@ -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"; @@ -40,7 +41,7 @@ const Home = async ({ params }: Props) => { { perspective }, ); - if (initialLandingPage.data === null) { + if (!isNonNullQueryResponse(initialLandingPage)) { return ( { ); } - const initialData = { - ...initialLandingPage, - data: initialLandingPage.data, - }; - return initialLandingPage.data.sections.map((section, index) => ( diff --git a/src/utils/pageData.ts b/src/utils/pageData.ts index 7d3fdbb2d..6018370d3 100644 --- a/src/utils/pageData.ts +++ b/src/utils/pageData.ts @@ -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 = { query: string; queryParams: QueryParams; @@ -49,21 +51,18 @@ async function fetchDynamicPage({ slug: path[0], language, }; - const pageResult = await loadStudioQuery( + const queryResponse = await loadStudioQuery( PAGE_BY_SLUG_QUERY, queryParams, { perspective }, ); - if (pageResult.data === null) { + if (!isNonNullQueryResponse(queryResponse)) { return null; } return { query: PAGE_BY_SLUG_QUERY, queryParams, - queryResponse: { - ...pageResult, - data: pageResult.data, - }, + queryResponse, docType: pageBuilderID, }; } @@ -95,7 +94,7 @@ async function fetchCompensationsPage({ perspective, }, ); - if (compensationsPageResult.data === null) { + if (!isNonNullQueryResponse(compensationsPageResult)) { return null; } const companyLocationsResult = await loadStudioQuery( @@ -103,7 +102,7 @@ async function fetchCompensationsPage({ {}, { perspective }, ); - if (companyLocationsResult.data === null) { + if (!isNonNullQueryResponse(companyLocationsResult)) { return null; } const localeDocumentResult = await loadStudioQuery( @@ -111,25 +110,16 @@ async function fetchCompensationsPage({ {}, { perspective }, ); - if (localeDocumentResult.data === null) { + if (!isNonNullQueryResponse(localeDocumentResult)) { return null; } return { query: COMPENSATIONS_PAGE_BY_SLUG_QUERY, queryParams, queryResponse: { - compensationsPage: { - ...compensationsPageResult, - data: compensationsPageResult.data, - }, - companyLocations: { - ...companyLocationsResult, - data: companyLocationsResult.data, - }, - locale: { - ...localeDocumentResult, - data: localeDocumentResult.data, - }, + compensationsPage: compensationsPageResult, + companyLocations: companyLocationsResult, + locale: localeDocumentResult, }, docType: compensationsId, }; @@ -157,17 +147,14 @@ async function fetchCustomerCase({ customerCasesPageParams, { perspective }, ); - if (customerCasesPageResult.data === null) { + if (!isNonNullQueryResponse(customerCasesPageResult)) { return null; } if (path.length === 1) { return { query: CUSTOMER_CASES_PAGE_QUERY, queryParams: customerCasesPageParams, - queryResponse: { - ...customerCasesPageResult, - data: customerCasesPageResult.data, - }, + queryResponse: customerCasesPageResult, docType: customerCasesPageID, }; } @@ -182,16 +169,13 @@ async function fetchCustomerCase({ perspective, }, ); - if (customerCaseResult.data === null) { + if (!isNonNullQueryResponse(customerCaseResult)) { return null; } return { query: CUSTOMER_CASE_QUERY, queryParams: customerCaseParams, - queryResponse: { - ...customerCaseResult, - data: customerCaseResult.data, - }, + queryResponse: customerCaseResult, docType: customerCaseID, }; } @@ -218,16 +202,13 @@ async function fetchLegalDocument({ perspective, }, ); - if (queryResponse.data === null) { + if (!isNonNullQueryResponse(queryResponse)) { return null; } return { query: LEGAL_DOCUMENT_BY_SLUG_AND_LANG_QUERY, queryParams, - queryResponse: { - ...queryResponse, - data: queryResponse.data, - }, + queryResponse, docType: legalDocumentID, }; } diff --git a/src/utils/queryResponse.ts b/src/utils/queryResponse.ts new file mode 100644 index 000000000..6862ca393 --- /dev/null +++ b/src/utils/queryResponse.ts @@ -0,0 +1,7 @@ +import { QueryResponseInitial } from "@sanity/react-loader"; + +export function isNonNullQueryResponse( + value: QueryResponseInitial, +): value is QueryResponseInitial { + return value.data !== null; +}