diff --git a/languages.ts b/languages.ts index eaf9ff349..0bb192609 100644 --- a/languages.ts +++ b/languages.ts @@ -1,4 +1,4 @@ -const languages = [ +export const languages = [ { id: "en", title: "English" }, { id: "se", title: "Swedish" }, { id: "no", title: "Norwegian" }, diff --git a/studio/schema.ts b/studio/schema.ts index 557d904a1..a9dff28b2 100644 --- a/studio/schema.ts +++ b/studio/schema.ts @@ -15,6 +15,7 @@ import companyLocation from "./schemas/documents/companyLocation"; import compensations from "./schemas/documents/compensations"; import redirect from "./schemas/documents/redirect"; import benefitsByLocation from "./schemas/objects/compensations/benefitsByLocation"; +import supportedLanguages from "./schemas/documents/supportedLanguages"; import defaultSeo from "./schemas/documents/admin/defaultSeo"; export const schema: { types: SchemaTypeDefinition[] } = { @@ -35,6 +36,7 @@ export const schema: { types: SchemaTypeDefinition[] } = { redirect, benefitsByLocation, companyLocation, + supportedLanguages, defaultSeo, ], }; diff --git a/studio/schemas/deskStructure.ts b/studio/schemas/deskStructure.ts index c7a2c3b5c..6d4cdbadb 100644 --- a/studio/schemas/deskStructure.ts +++ b/studio/schemas/deskStructure.ts @@ -10,6 +10,7 @@ import { HeartIcon, SparkleIcon, CaseIcon, + TranslateIcon, DoubleChevronRightIcon, PinIcon, SearchIcon, @@ -20,6 +21,7 @@ import { legalDocumentID } from "./documents/legalDocuments"; import { compensationsId } from "./documents/compensations"; import { redirectId } from "./documents/redirect"; import { companyLocationID } from "./documents/companyLocation"; +import { supportedLanguagesID } from "./documents/supportedLanguages"; import { defaultSeoID } from "./documents/admin/defaultSeo"; // Admin Section @@ -79,6 +81,15 @@ const siteSettingSection = (S: StructureBuilder) => .child( S.document().schemaType(soMeLinksID).documentId(soMeLinksID), ), + S.listItem() + .title("Supported Languages") + .icon(TranslateIcon) + .child( + S.document() + .schemaType(supportedLanguagesID) + .documentId(supportedLanguagesID) + .title("Supported Languages"), + ), S.listItem() .title("Default SEO") .icon(SearchIcon) @@ -92,8 +103,6 @@ const siteSettingSection = (S: StructureBuilder) => .title("Broken Links") .icon(DoubleChevronRightIcon) .child(S.documentTypeList(redirectId).title("Redirects")), - //TODO: Add SEO Settings - //TODO: Add Language selector ]), ); @@ -135,7 +144,7 @@ const SpecialPagesSection = (S: StructureBuilder) => ); // Main export -export default (S: StructureBuilder) => +const buildDeskStructure = (S: StructureBuilder) => S.list() .title("Content") .items([ @@ -144,3 +153,5 @@ export default (S: StructureBuilder) => pagesSection(S), SpecialPagesSection(S), ]); + +export default buildDeskStructure; diff --git a/studio/schemas/documents/supportedLanguages.ts b/studio/schemas/documents/supportedLanguages.ts new file mode 100644 index 000000000..4ab709cf6 --- /dev/null +++ b/studio/schemas/documents/supportedLanguages.ts @@ -0,0 +1,71 @@ +import { defineType } from "sanity"; +import { languages } from "languages"; + +const languageOptions = languages.map((language) => { + return { title: language.title, value: language.id }; +}); + +export const supportedLanguagesID = "supportedLanguages"; + +const supportedLanguages = defineType({ + name: supportedLanguagesID, + type: "document", + fields: [ + { + name: "languages", + type: "array", + description: + "Select languages available for translation and designate one as the default language for the homepage.", + of: [ + { + type: "object", + fields: [ + { + name: "language", + type: "string", + description: "Select the language available for translation", + title: "Language", + options: { + list: languageOptions, + }, + }, + { + name: "isDefault", + type: "boolean", + title: "Default Language", + description: + "Indicate whether the language should be set as the default. Please note that only one default language is allowed.", + }, + ], + preview: { + select: { + title: "language", + }, + prepare(selection) { + const { title } = selection; + const languageOption = languageOptions.find( + (option) => option.value === title, + ); + return { + title: languageOption ? languageOption.title : title, + }; + }, + }, + }, + ], + validation: (Rule) => + Rule.custom((languages: { language: string; isDefault: boolean }[]) => { + const defaultLanguages = languages.filter((lang) => lang.isDefault); + if (defaultLanguages.length > 1) { + return "Only one default language is allowed"; + } + if (defaultLanguages.length === 0 && languages.length > 0) { + return "At least one language must be marked as default"; + } + return true; + }), + }, + ], +}); + +export default supportedLanguages;