Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v3 - Add benefitgroup for a spesific location #589

Merged
merged 6 commits into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
62 changes: 62 additions & 0 deletions studio/schemas/objects/compensations/benefitsByLocation.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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:
"Specify the benefits offered at each office location. A benefit is only associated with a single 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) =>
mathiazom marked this conversation as resolved.
Show resolved Hide resolved
Rule.custom((benefitsByLocation) => {
const isNotDuplicate: boolean = checkForDuplicateLocations(
benefitsByLocation as DocumentWithLocation[] | undefined,
);
return (
isNotDuplicate ||
"Each location should be listed only once in the benefits list."
);
}),
});
20 changes: 20 additions & 0 deletions studio/utils/checkForDuplicateLocations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
interface LocationReference {
anemne marked this conversation as resolved.
Show resolved Hide resolved
_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;
};