Skip to content

Commit

Permalink
refactor(queries): extract reusable i18n query fragments
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Oct 9, 2024
1 parent 3658cd4 commit 87fad53
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 36 deletions.
28 changes: 28 additions & 0 deletions studio/lib/queries/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { groq } from "next-sanity";

import { translatedFieldFragment } from "studio/lib/queries/utils/i18n";

export const LANGUAGE_FIELD_FRAGMENT = groq`
"language": $language
`;
export const TRANSLATED_LINK_TITLE_FRAGMENT = groq`
"linkTitle": ${translatedFieldFragment("linkTitle")}
`;
export const TRANSLATED_SLUG_VALUE_FRAGMENT = groq`
select(
slug._type == "slug" => slug.current,
${translatedFieldFragment("slug")}
)
`;
export const TRANSLATED_INTERNAL_LINK_FRAGMENT = groq`
...select(linkType == "internal" => {
"internalLink": internalLink->{
"_ref": ${TRANSLATED_SLUG_VALUE_FRAGMENT}
}
})
`;
export const TRANSLATED_LINK_FRAGMENT = groq`
${LANGUAGE_FIELD_FRAGMENT},
${TRANSLATED_LINK_TITLE_FRAGMENT},
${TRANSLATED_INTERNAL_LINK_FRAGMENT}
`;
33 changes: 1 addition & 32 deletions studio/lib/queries/pages.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,6 @@
import { groq } from "next-sanity";

export const LANGUAGE_FIELD_FRAGMENT = groq`
"language": $language
`;

export function translatedFieldFragment(fieldName: string) {
return groq`${fieldName}[_key == $language][0].value`;
}

export const TRANSLATED_LINK_TITLE_FRAGMENT = groq`
"linkTitle": ${translatedFieldFragment("linkTitle")}
`;

export const TRANSLATED_SLUG_VALUE_FRAGMENT = groq`
select(
slug._type == "slug" => slug.current,
${translatedFieldFragment("slug")}
)
`;

export const TRANSLATED_INTERNAL_LINK_FRAGMENT = groq`
...select(linkType == "internal" => {
"internalLink": internalLink->{
"_ref": ${TRANSLATED_SLUG_VALUE_FRAGMENT}
}
})
`;

export const TRANSLATED_LINK_FRAGMENT = groq`
${LANGUAGE_FIELD_FRAGMENT},
${TRANSLATED_LINK_TITLE_FRAGMENT},
${TRANSLATED_INTERNAL_LINK_FRAGMENT}
`;
import { LANGUAGE_FIELD_FRAGMENT, TRANSLATED_LINK_FRAGMENT } from "./i18n";

const SECTIONS_FRAGMENT = groq`
sections[]{
Expand Down
4 changes: 2 additions & 2 deletions studio/lib/queries/siteSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import { groq } from "next-sanity";

import {
LANGUAGE_FIELD_FRAGMENT,
PAGE_FRAGMENT,
TRANSLATED_LINK_FRAGMENT,
TRANSLATED_SLUG_VALUE_FRAGMENT,
} from "./pages";
} from "./i18n";
import { PAGE_FRAGMENT } from "./pages";

//Brand Assets
export const BRAND_ASSETS_QUERY = groq`
Expand Down
2 changes: 1 addition & 1 deletion studio/lib/queries/slugTranslations.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { groq } from "next-sanity";

import { translatedFieldFragment } from "./pages";
import { translatedFieldFragment } from "./utils/i18n";

export const SLUG_FIELD_TRANSLATIONS_FROM_LANGUAGE_QUERY = groq`
*[${translatedFieldFragment("slug")} == $slug][0] {
Expand Down
2 changes: 1 addition & 1 deletion studio/lib/queries/specialPages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { groq } from "next-sanity";

import { translatedFieldFragment } from "./pages";
import { translatedFieldFragment } from "./utils/i18n";

//Compensations
export const COMPENSATIONS_PAGE_QUERY = groq`
Expand Down
5 changes: 5 additions & 0 deletions studio/lib/queries/utils/i18n.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { groq } from "next-sanity";

export function translatedFieldFragment(fieldName: string) {
return groq`${fieldName}[_key == $language][0].value`;
}

0 comments on commit 87fad53

Please sign in to comment.