Skip to content

Commit

Permalink
feat: filter customer cases on domain
Browse files Browse the repository at this point in the history
  • Loading branch information
trulshj committed Nov 22, 2024
1 parent bf32476 commit d282bcb
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 9 deletions.
8 changes: 7 additions & 1 deletion src/components/customerCases/CustomerCases.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import { headers } from "next/headers";
import Link from "next/link";

import { SanitySharedImage } from "src/components/image/SanityImage";
import LinkButton from "src/components/linkButton/LinkButton";
import Text from "src/components/text/Text";
import { sharedCustomerCasesLink } from "src/components/utils/linkTypes";
import { getDraftModeInfo } from "src/utils/draftmode";
import { domainFromHostname } from "src/utils/url";
import { CustomerCasePage } from "studio/lib/interfaces/specialPages";
import { CustomerCaseBase } from "studioShared/lib/interfaces/customerCases";
import { CUSTOMER_CASES_QUERY } from "studioShared/lib/queries/customerCases";
Expand All @@ -19,14 +21,18 @@ interface CustomerCasesProps {
const CustomerCases = async ({ customerCasesPage }: CustomerCasesProps) => {
const { perspective } = getDraftModeInfo();

const domain = domainFromHostname(headers().get("host"));

const [sharedCustomerCases] = await Promise.all([
loadSharedQuery<CustomerCaseBase[]>(
CUSTOMER_CASES_QUERY,
{ language: customerCasesPage.language },
{ language: customerCasesPage.language, domain },
{ perspective },
),
]);

console.log(sharedCustomerCases);

return (
<div className={styles.wrapper}>
<div className={styles.content}>
Expand Down
6 changes: 6 additions & 0 deletions src/utils/pageData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { customerCaseID } from "studioShared/schemas/documents/customerCase";

import { emailFromAliasAndHostname, fetchChewbaccaEmployee } from "./employees";
import { isNonNullQueryResponse } from "./queryResponse";
import { domainFromHostname } from "./url";

type PageFromParams<D, T> = {
queryResponse: D;
Expand Down Expand Up @@ -150,6 +151,7 @@ async function fetchCompensationsPage({

async function fetchCustomerCase({
language,
hostname,
path,
perspective,
}: PageDataParams): Promise<
Expand All @@ -166,6 +168,9 @@ async function fetchCustomerCase({
if (path.length === 0) {
return null;
}

const domain = hostname === null ? null : domainFromHostname(hostname);

const customerCasesPageResult =
await loadStudioQuery<CustomerCasePage | null>(
CUSTOMER_CASES_PAGE_QUERY,
Expand Down Expand Up @@ -197,6 +202,7 @@ async function fetchCustomerCase({
const customerCaseResult = await loadSharedQuery<CustomerCase | null>(
CUSTOMER_CASE_QUERY,
{
domain,
slug: path[1],
language,
},
Expand Down
1 change: 1 addition & 0 deletions studioShared/lib/interfaces/customerCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export interface CustomerCaseBase {
_id: string;
language: string;
slug: string;
domains: string[];
basicTitle: string;
description: string;
image: IImage;
Expand Down
17 changes: 9 additions & 8 deletions studioShared/lib/queries/customerCases.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const INTERNATIONALIZED_IMAGE_FRAGMENT = groq`

const CUSTOMER_CASE_BASE_FRAGMENT = groq`
_id,
domains,
${LANGUAGE_FIELD_FRAGMENT},
"slug": ${translatedFieldFragment("slug")},
"basicTitle": ${translatedFieldFragment("basicTitle")},
Expand All @@ -23,7 +24,7 @@ const CUSTOMER_CASE_BASE_FRAGMENT = groq`
`;

export const CUSTOMER_CASES_QUERY = groq`
*[_type == "customerCase"]{
*[_type == "customerCase" && ($domain == null || $domain in domains)]{
${CUSTOMER_CASE_BASE_FRAGMENT}
}
`;
Expand All @@ -34,7 +35,7 @@ export const SPLIT_SECTIONS_FRAGMENT = groq`
"sectionTitle": ${translatedFieldFragment("sectionTitle")},
"text": ${translatedFieldFragment("text")},
url,
textBlockType,
textBlockType,
},
_type == "imageBlock" => {
"image": image {${INTERNATIONALIZED_IMAGE_FRAGMENT}},
Expand All @@ -43,14 +44,14 @@ export const SPLIT_SECTIONS_FRAGMENT = groq`
`;

export const CUSTOMER_CASE_QUERY = groq`
*[_type == "customerCase" && ${translatedFieldFragment("slug")} == $slug][0] {
*[_type == "customerCase" && ${translatedFieldFragment("slug")} == $slug && ($domain == null || $domain in domains)][0] {
${CUSTOMER_CASE_BASE_FRAGMENT},
"projectInfo": projectInfo {
customer,
customer,
"customerSectors": customerSectors[] {
"customerSector": ${translatedFieldFragment("customerSectorItem")}
},
url,
url,
"deliveries": {
"design": deliveries.design[] {
"designDelivery": ${translatedFieldFragment("designDelivery")}
Expand All @@ -62,7 +63,7 @@ export const CUSTOMER_CASE_QUERY = groq`
"projectManagementDelivery": ${translatedFieldFragment("projectManagementDelivery")}
}
},
collaborators,
collaborators,
consultants
},
"sections": sections[] {
Expand All @@ -72,7 +73,7 @@ export const CUSTOMER_CASE_QUERY = groq`
"sections": sections[] {
_key,
_type,
${SPLIT_SECTIONS_FRAGMENT}
}
},
Expand All @@ -95,7 +96,7 @@ export const CUSTOMER_CASE_QUERY = groq`
_key,
"text": ${translatedFieldFragment("text")},
},
},
},
_type == "imageBlock" => {
"image": image {${INTERNATIONALIZED_IMAGE_FRAGMENT}},
fullWidth
Expand Down
2 changes: 2 additions & 0 deletions studioShared/schemas/documents/customerCase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { titleSlug } from "studio/schemas/schemaTypes/slug";
import { buildDraftId, buildPublishedId } from "studio/utils/documentUtils";
import { firstTranslation } from "studio/utils/i18n";
import { customerCaseProjectInfo } from "studioShared/schemas/fields/customerCaseProjectInfo";
import { domainsField } from "studioShared/schemas/fields/domains";
import imageBlock from "studioShared/schemas/objects/imageBlock";
import listBlock from "studioShared/schemas/objects/listBlock";
import resultsBlock from "studioShared/schemas/objects/resultsBlock";
Expand Down Expand Up @@ -38,6 +39,7 @@ const customerCase = defineType({
type: "internationalizedArrayString",
title: "Title",
}),
defineField({ ...domainsField, validation: (rule) => rule.required() }),
defineField({
name: "description",
type: "internationalizedArrayText",
Expand Down
18 changes: 18 additions & 0 deletions studioShared/schemas/fields/domains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { defineField } from "sanity";

export const domains = ["variant.no", "variant.se"] as const;

export const domainsField = defineField({
name: "domains",
type: "array",
title: "Domains",
description:
"The customer case will only be shown on the domains checked here. IMPORTANT: Remember to have translations in the language for the domains you add here!",
of: [{ type: "string" }],
options: {
list: domains.map((domain) => {
return { title: domain, value: domain };
}),
},
initialValue: domains.map((domain) => domain),
});

0 comments on commit d282bcb

Please sign in to comment.