diff --git a/studio/schemas/documents/compensations.ts b/studio/schemas/documents/compensations.ts index 66a03dd98..46e1a430c 100644 --- a/studio/schemas/documents/compensations.ts +++ b/studio/schemas/documents/compensations.ts @@ -1,19 +1,19 @@ import { defineField, defineType } from "sanity"; import { titleSlug } from "../schemaTypes/slug"; import seo from "../objects/seo"; -import offices from "../objects/offices"; +import locations from "../objects/locations"; import { title } from "../fields/text"; import { benefitId } from "./benefit"; /* - maximum number of offices without preview string shortening + maximum number of locations without preview string shortening (should be at least 2) given array: ["Trondheim", "Oslo", "Bergen", "Molde"] 2 => Trondheim, and more 3 => Trondheim, Oslo, and more 4 => Trondheim, Oslo, Bergen, and Molde */ -const OFFICES_PREVIEW_CUTOFF = 3; +const LOCATIONS_PREVIEW_CUTOFF = 3; export const compensationsId = "compensations"; @@ -22,10 +22,7 @@ const compensations = defineType({ type: "document", title: "Compensations", fields: [ - { - ...offices, - description: "Which offices are these compensations relevant for?", - }, + locations, title, titleSlug, seo, @@ -48,44 +45,44 @@ const compensations = defineType({ select: { title: "basicTitle", /* - Array object values are accessed with dot notation and array index: "offices.0.basicTitle" + Array object values are accessed with dot notation and array index: "locations.0.basicTitle" This limits the preview to only select a subset of the array https://www.sanity.io/docs/previews-list-views#62febb15a63a */ - ...[...Array(OFFICES_PREVIEW_CUTOFF + 1).keys()].reduce( - (o, i) => ({ ...o, [`office${i}`]: `offices.${i}.basicTitle` }), + ...[...Array(LOCATIONS_PREVIEW_CUTOFF + 1).keys()].reduce( + (o, i) => ({ ...o, [`location${i}`]: `locations.${i}.basicTitle` }), {}, ), }, - prepare({ title, ...officesMap }) { + prepare({ title, ...locationsMap }) { return { title, - subtitle: previewStringFromOfficesMap( - officesMap, - OFFICES_PREVIEW_CUTOFF, + subtitle: previewStringFromLocationsMap( + locationsMap, + LOCATIONS_PREVIEW_CUTOFF, ), }; }, }, }); -function previewStringFromOfficesMap( - officesMap: { +function previewStringFromLocationsMap( + locationsMap: { [key: string]: string; }, cutoff: number, ): string | undefined { - const offices = Object.values(officesMap).filter( + const locations = Object.values(locationsMap).filter( (o) => o !== undefined, ); - if (offices.length === 0) { + if (locations.length === 0) { return undefined; - } else if (offices.length === 1) { - return `Office: ${offices[0]}`; - } else if (offices.length > cutoff) { - return `Offices: ${offices.toSpliced(cutoff - 1).join(", ")}, and more`; + } else if (locations.length === 1) { + return `Location: ${locations[0]}`; + } else if (locations.length > cutoff) { + return `Locations: ${locations.toSpliced(cutoff - 1).join(", ")}, and more`; } - return `Offices: ${offices.toSpliced(-1).join(", ")} and ${offices.at(-1)}`; + return `Locations: ${locations.toSpliced(-1).join(", ")} and ${locations.at(-1)}`; } export default compensations; diff --git a/studio/schemas/objects/locations.ts b/studio/schemas/objects/locations.ts new file mode 100644 index 000000000..36673297f --- /dev/null +++ b/studio/schemas/objects/locations.ts @@ -0,0 +1,22 @@ +import { defineField } from "sanity"; +import { locationId } from "../documents/location"; + +const locations = defineField({ + name: "locations", + type: "array", + title: "Relevant Locations", + description: + "You can tailor this content to specific office locations by selecting them here. If the content applies to all locations, just leave this field empty.", + of: [ + { + title: "Select a location", + type: "reference", + to: [{ type: locationId }], + options: { + disableNew: true, + }, + }, + ], +}); + +export default locations; diff --git a/studio/schemas/objects/office.ts b/studio/schemas/objects/office.ts deleted file mode 100644 index bd4f9030a..000000000 --- a/studio/schemas/objects/office.ts +++ /dev/null @@ -1,12 +0,0 @@ -import { defineField } from "sanity"; -import { officeId } from "../documents/office"; - -const office = defineField({ - name: "office", - title: "Office", - description: "Reference to a company office", - type: "reference", - to: [{ type: officeId }], -}); - -export default office; diff --git a/studio/schemas/objects/offices.ts b/studio/schemas/objects/offices.ts deleted file mode 100644 index c906f4459..000000000 --- a/studio/schemas/objects/offices.ts +++ /dev/null @@ -1,13 +0,0 @@ -import { defineField } from "sanity"; -import office from "./office"; - -const offices = defineField({ - name: "offices", - title: "Offices", - description: "List of company offices", - type: "array", - of: [office], - validation: (Rule) => Rule.required().min(1).unique(), -}); - -export default offices;