-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add benefitgroup for a spesific location
- Loading branch information
Showing
3 changed files
with
78 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
studio/schemas/objects/compensations/benefitsByLocation.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
); | ||
}), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |