Skip to content

Commit

Permalink
feat(compensations): rename office -> locations
Browse files Browse the repository at this point in the history
  • Loading branch information
mathiazom committed Sep 3, 2024
1 parent a1b16dd commit b8f417d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 48 deletions.
43 changes: 20 additions & 23 deletions studio/schemas/documents/compensations.ts
Original file line number Diff line number Diff line change
@@ -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";

Expand All @@ -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,
Expand All @@ -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<string>(officesMap).filter(
const locations = Object.values<string>(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;
22 changes: 22 additions & 0 deletions studio/schemas/objects/locations.ts
Original file line number Diff line number Diff line change
@@ -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;
12 changes: 0 additions & 12 deletions studio/schemas/objects/office.ts

This file was deleted.

13 changes: 0 additions & 13 deletions studio/schemas/objects/offices.ts

This file was deleted.

0 comments on commit b8f417d

Please sign in to comment.