From e9917c86f5684dbe4579c255a3c638c81a452833 Mon Sep 17 00:00:00 2001 From: Ane Date: Thu, 5 Sep 2024 11:39:07 +0200 Subject: [PATCH 1/6] Add benefitgroup for a spesific location --- studio/schemas/documents/compensations.ts | 34 +----------- .../compensations/benefitsByLocation.ts | 55 +++++++++++++++++++ studio/utils/checkForDuplicateLocations.ts | 21 +++++++ 3 files changed, 78 insertions(+), 32 deletions(-) create mode 100644 studio/schemas/objects/compensations/benefitsByLocation.ts create mode 100644 studio/utils/checkForDuplicateLocations.ts diff --git a/studio/schemas/documents/compensations.ts b/studio/schemas/documents/compensations.ts index 33e828c67..881bea522 100644 --- a/studio/schemas/documents/compensations.ts +++ b/studio/schemas/documents/compensations.ts @@ -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"; @@ -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: { diff --git a/studio/schemas/objects/compensations/benefitsByLocation.ts b/studio/schemas/objects/compensations/benefitsByLocation.ts new file mode 100644 index 000000000..b6ba82b85 --- /dev/null +++ b/studio/schemas/objects/compensations/benefitsByLocation.ts @@ -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." + ); +}), +}); \ No newline at end of file diff --git a/studio/utils/checkForDuplicateLocations.ts b/studio/utils/checkForDuplicateLocations.ts new file mode 100644 index 000000000..425b62812 --- /dev/null +++ b/studio/utils/checkForDuplicateLocations.ts @@ -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; + }; \ No newline at end of file From d095085b4c4c96d1228cc8d6696637bfbe5a05de Mon Sep 17 00:00:00 2001 From: Ane Date: Thu, 5 Sep 2024 12:28:58 +0200 Subject: [PATCH 2/6] prettier fix --- .../compensations/benefitsByLocation.ts | 61 ++++++++++--------- studio/utils/checkForDuplicateLocations.ts | 33 +++++----- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/studio/schemas/objects/compensations/benefitsByLocation.ts b/studio/schemas/objects/compensations/benefitsByLocation.ts index b6ba82b85..872508378 100644 --- a/studio/schemas/objects/compensations/benefitsByLocation.ts +++ b/studio/schemas/objects/compensations/benefitsByLocation.ts @@ -2,16 +2,19 @@ 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"; +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.", + "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: [ { @@ -23,33 +26,35 @@ export const benefitsByLocation = defineField({ defineField({ name: "benefitsGroup", title: "Benefits Group", - description: "Enter the benefits offered at the location selected above.", + 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}`, - }; + 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." - ); -}), -}); \ No newline at end of file + ], + validation: (Rule) => + Rule.custom((benefitsByLocation) => { + return ( + checkForDuplicateLocations( + benefitsByLocation as DocumentWithLocation[] | undefined, + ) || "Each location should be listed only once in the benefits list." + ); + }), +}); diff --git a/studio/utils/checkForDuplicateLocations.ts b/studio/utils/checkForDuplicateLocations.ts index 425b62812..5369f7d40 100644 --- a/studio/utils/checkForDuplicateLocations.ts +++ b/studio/utils/checkForDuplicateLocations.ts @@ -1,21 +1,20 @@ interface LocationReference { - _ref: string; - _type: string; - title?: string; - } - - export interface DocumentWithLocation { - location: 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; - }; \ No newline at end of file + 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; +}; From f866682f6de522fb8cf95f9f71cc660c464301b9 Mon Sep 17 00:00:00 2001 From: anemne <63043552+anemne@users.noreply.github.com> Date: Thu, 5 Sep 2024 13:13:53 +0200 Subject: [PATCH 3/6] Update studio/schemas/objects/compensations/benefitsByLocation.ts Co-authored-by: Mathias Oterhals Myklebust <24361490+mathiazom@users.noreply.github.com> --- studio/schemas/objects/compensations/benefitsByLocation.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/studio/schemas/objects/compensations/benefitsByLocation.ts b/studio/schemas/objects/compensations/benefitsByLocation.ts index 872508378..dff1067ba 100644 --- a/studio/schemas/objects/compensations/benefitsByLocation.ts +++ b/studio/schemas/objects/compensations/benefitsByLocation.ts @@ -11,7 +11,7 @@ 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.", + "Specify the benefits offered at each office location. A benefit is only associated with a single location", type: "array", of: [ { From a6ae1a84ee67506f3226e507891b2340f1e41deb Mon Sep 17 00:00:00 2001 From: Ane Date: Thu, 5 Sep 2024 16:48:09 +0200 Subject: [PATCH 4/6] replace validation --- .../schemas/objects/compensations/benefitsByLocation.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/studio/schemas/objects/compensations/benefitsByLocation.ts b/studio/schemas/objects/compensations/benefitsByLocation.ts index dff1067ba..71f0d2c34 100644 --- a/studio/schemas/objects/compensations/benefitsByLocation.ts +++ b/studio/schemas/objects/compensations/benefitsByLocation.ts @@ -51,10 +51,12 @@ export const benefitsByLocation = defineField({ ], validation: (Rule) => Rule.custom((benefitsByLocation) => { + const isNotDuplicate: boolean = checkForDuplicateLocations( + benefitsByLocation as DocumentWithLocation[] | undefined, + ); return ( - checkForDuplicateLocations( - benefitsByLocation as DocumentWithLocation[] | undefined, - ) || "Each location should be listed only once in the benefits list." + isNotDuplicate || + "Each location should be listed only once in the benefits list." ); }), }); From c6e1570f717a8190436b2d9a553fdcdd8f5f41e2 Mon Sep 17 00:00:00 2001 From: Ane Date: Mon, 9 Sep 2024 08:10:55 +0200 Subject: [PATCH 5/6] created utils folder inside compensations folder and created validation file to align with PR 535 --- .../compensations/benefitsByLocation.ts | 6 ++--- .../objects/compensations/utils/validation.ts | 27 +++++++++++++++++++ studio/utils/checkForDuplicateLocations.ts | 20 -------------- 3 files changed, 29 insertions(+), 24 deletions(-) create mode 100644 studio/schemas/objects/compensations/utils/validation.ts delete mode 100644 studio/utils/checkForDuplicateLocations.ts diff --git a/studio/schemas/objects/compensations/benefitsByLocation.ts b/studio/schemas/objects/compensations/benefitsByLocation.ts index 71f0d2c34..2ca07b2a1 100644 --- a/studio/schemas/objects/compensations/benefitsByLocation.ts +++ b/studio/schemas/objects/compensations/benefitsByLocation.ts @@ -2,10 +2,8 @@ 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"; +import { checkForDuplicateLocations, DocumentWithLocation } from "./utils/validation"; + export const benefitsByLocation = defineField({ name: "benefitsByLocation", diff --git a/studio/schemas/objects/compensations/utils/validation.ts b/studio/schemas/objects/compensations/utils/validation.ts new file mode 100644 index 000000000..27d84d302 --- /dev/null +++ b/studio/schemas/objects/compensations/utils/validation.ts @@ -0,0 +1,27 @@ +interface LocationReference { + _ref: string; + _type: string; + title?: string; + } + + export interface DocumentWithLocation { + location: LocationReference; + } + + /** + * Checks for duplicate location references in the documents array. + * Ensures each location has a unique document entry. + * + * @param {DocumentWithLocation[] | undefined} documents - The array of documents, each with one or more locations. + * @returns {string | true} - Returns an error message if duplicate locations are found, or true if all are unique. + */ + 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; + }; \ No newline at end of file diff --git a/studio/utils/checkForDuplicateLocations.ts b/studio/utils/checkForDuplicateLocations.ts deleted file mode 100644 index 5369f7d40..000000000 --- a/studio/utils/checkForDuplicateLocations.ts +++ /dev/null @@ -1,20 +0,0 @@ -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; -}; From 4a5d6cb04ed4fb8f45c40e7b71185e5d97a44cbe Mon Sep 17 00:00:00 2001 From: Ane Date: Mon, 9 Sep 2024 08:12:36 +0200 Subject: [PATCH 6/6] prettier fix --- .../compensations/benefitsByLocation.ts | 6 ++- .../objects/compensations/utils/validation.ts | 52 +++++++++---------- 2 files changed, 30 insertions(+), 28 deletions(-) diff --git a/studio/schemas/objects/compensations/benefitsByLocation.ts b/studio/schemas/objects/compensations/benefitsByLocation.ts index 2ca07b2a1..cf06767f1 100644 --- a/studio/schemas/objects/compensations/benefitsByLocation.ts +++ b/studio/schemas/objects/compensations/benefitsByLocation.ts @@ -2,8 +2,10 @@ 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 "./utils/validation"; - +import { + checkForDuplicateLocations, + DocumentWithLocation, +} from "./utils/validation"; export const benefitsByLocation = defineField({ name: "benefitsByLocation", diff --git a/studio/schemas/objects/compensations/utils/validation.ts b/studio/schemas/objects/compensations/utils/validation.ts index 27d84d302..3f1bd0796 100644 --- a/studio/schemas/objects/compensations/utils/validation.ts +++ b/studio/schemas/objects/compensations/utils/validation.ts @@ -1,27 +1,27 @@ interface LocationReference { - _ref: string; - _type: string; - title?: string; - } - - export interface DocumentWithLocation { - location: LocationReference; - } - - /** - * Checks for duplicate location references in the documents array. - * Ensures each location has a unique document entry. - * - * @param {DocumentWithLocation[] | undefined} documents - The array of documents, each with one or more locations. - * @returns {string | true} - Returns an error message if duplicate locations are found, or true if all are unique. - */ - 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; - }; \ No newline at end of file + _ref: string; + _type: string; + title?: string; +} + +export interface DocumentWithLocation { + location: LocationReference; +} + +/** + * Checks for duplicate location references in the documents array. + * Ensures each location has a unique document entry. + * + * @param {DocumentWithLocation[] | undefined} documents - The array of documents, each with one or more locations. + * @returns {string | true} - Returns an error message if duplicate locations are found, or true if all are unique. + */ +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; +};