Skip to content

Commit

Permalink
prettier fix
Browse files Browse the repository at this point in the history
  • Loading branch information
anemne committed Sep 5, 2024
1 parent e9917c8 commit d095085
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 45 deletions.
61 changes: 33 additions & 28 deletions studio/schemas/objects/compensations/benefitsByLocation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: [
{
Expand All @@ -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."
);
}),
});
],
validation: (Rule) =>
Rule.custom((benefitsByLocation) => {
return (
checkForDuplicateLocations(
benefitsByLocation as DocumentWithLocation[] | undefined,
) || "Each location should be listed only once in the benefits list."
);
}),
});
33 changes: 16 additions & 17 deletions studio/utils/checkForDuplicateLocations.ts
Original file line number Diff line number Diff line change
@@ -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;
};
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 d095085

Please sign in to comment.