Skip to content

Commit

Permalink
Add benefitgroup for a spesific location
Browse files Browse the repository at this point in the history
  • Loading branch information
anemne committed Sep 5, 2024
1 parent 011f15f commit e9917c8
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 32 deletions.
34 changes: 2 additions & 32 deletions studio/schemas/documents/compensations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { defineField, defineType } from "sanity";
import { titleSlug } from "../schemaTypes/slug";
import seo from "../objects/seo";
import { title } from "../fields/text";
import { benefitId } from "./benefit";
import { bonusesByLocation } from "../objects/compensations/bonusesByLocation";
import { pension } from "../objects/compensations/pension";
import { benefitsByLocation } from "../objects/compensations/benefitsByLocation";

export const compensationsId = "compensations";

Expand All @@ -30,38 +30,8 @@ const compensations = defineType({
}),
pension,
bonusesByLocation,
benefitsByLocation,
// add salary here
// benefits should be updated to match the grouping by location:
// defineField({
// name: "benefits",
// title: "Benefits",
// type: "array",
// of: [
// defineField({
// name: "benefitGroup",
// title: "Benefit Group",
// type: "object",
// fields: [
// locations,
// defineField({
// name: "benefitsList",
// title: "List of Benefits",
// type: "array",
// of: [{ type: benefitId }],
// }),
// ],
// }),
// ],
// }),
// IMPORTANT: this is just a very simple mockup and might not represent a good ux
defineField({
name: "benefits",
title: "Included Benefits",
description:
"Add and manage information on the benefits included with the compensation package, such as health insurance, retirement plans, and paid time off.",
type: "array",
of: [{ type: benefitId }],
}),
seo,
],
preview: {
Expand Down
55 changes: 55 additions & 0 deletions studio/schemas/objects/compensations/benefitsByLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { defineField } from "sanity";
import { location, locationID } from "../locations";
import { companyLocationNameID } from "studio/schemas/documents/companyLocation";
import { benefitId } from "studio/schemas/documents/benefit";
import { checkForDuplicateLocations, DocumentWithLocation } from "studio/utils/checkForDuplicateLocations";

export const benefitsByLocation = defineField({
name: "benefitsByLocation",
title: "Benefits by Location",
description:
"Enter the benefits offered at each office location. You can only have one location entry, but you can list multiple benefits for each office location.",
type: "array",
of: [
{
type: "object",
fields: [
{
...location,
description:
"Select the office location for which you are entering benefits information. Each location must be unique.",
validation: (Rule) => Rule.required(),
},
defineField({
name: "benefitsGroup",
title: "Benefits Group",
description: "Enter the benefits offered at the location selected above.",
type: "array",
of: [{ type: benefitId }],
}),
],
preview: {
select: {
location: `${locationID}.${companyLocationNameID}`,
benefitsGroup: "benefitsGroup",
},
prepare({ location, benefitsGroup }) {
const benefitsCount = benefitsGroup ? benefitsGroup.length : 0;
return {
title: location ? `Benefits group for ${location}` : "No location selected",
subtitle: `Number of benefits: ${benefitsCount}`,
};
},
},
},
],
validation: (Rule) =>
Rule.custom((benefitsByLocation) => {
return (
checkForDuplicateLocations(
benefitsByLocation as DocumentWithLocation[] | undefined,
) ||
"Each location should be listed only once in the benefits list."
);
}),
});
21 changes: 21 additions & 0 deletions studio/utils/checkForDuplicateLocations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
interface LocationReference {
_ref: string;
_type: string;
title?: string;
}

export interface DocumentWithLocation {
location: LocationReference;
}


export const checkForDuplicateLocations = (
documents: DocumentWithLocation[] | undefined,
): boolean => {
if (!documents) return true;
const locationRefs = documents
.map((entry) => entry.location?._ref)
.filter(Boolean);
const uniqueRefs = new Set(locationRefs);
return uniqueRefs.size === locationRefs.length;
};

0 comments on commit e9917c8

Please sign in to comment.