From 9473e23978a1451f33a3509c95376a69799d4616 Mon Sep 17 00:00:00 2001 From: Mathias Oterhals Myklebust Date: Mon, 9 Sep 2024 08:52:16 +0200 Subject: [PATCH] feat(salariesByLocation): show latest year in preview subtitle --- studio/lib/payloads/compensations.ts | 7 +++++++ .../objects/compensations/salariesByLocation.ts | 15 +++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/studio/lib/payloads/compensations.ts b/studio/lib/payloads/compensations.ts index 468e3733b..b341685df 100644 --- a/studio/lib/payloads/compensations.ts +++ b/studio/lib/payloads/compensations.ts @@ -8,6 +8,13 @@ export interface Benefit { richText: PortableTextBlock[]; } +export interface SalariesPage { + _type: string; + _key: string; + year: number; + salaries: string; +} + export interface CompensationsPage { _createdAt: string; _id: string; diff --git a/studio/schemas/objects/compensations/salariesByLocation.ts b/studio/schemas/objects/compensations/salariesByLocation.ts index a92bb8875..5bcacd433 100644 --- a/studio/schemas/objects/compensations/salariesByLocation.ts +++ b/studio/schemas/objects/compensations/salariesByLocation.ts @@ -6,6 +6,7 @@ import { } from "./utils/validation"; import { companyLocationNameID } from "../../documents/companyLocation"; import { SalariesInput } from "../../../components/salariesInput/SalariesInput"; +import { SalariesPage } from "../../../lib/payloads/compensations"; export const salariesByLocation = defineField({ name: "salaries", @@ -69,11 +70,21 @@ export const salariesByLocation = defineField({ preview: { select: { location: `${locationID}.${companyLocationNameID}`, + yearlySalaries: `yearlySalaries`, }, - prepare({ location }) { + prepare({ location, yearlySalaries }) { + const latestYear = + yearlySalaries && yearlySalaries.length > 0 + ? yearlySalaries.reduce((acc: number, salaries: SalariesPage) => { + if (salaries.year > acc) { + return salaries.year; + } + return acc; + }, yearlySalaries[0].year) + : undefined; return { title: location || "No location selected", - subtitle: "Location Salaries", + subtitle: latestYear ? `Latest year: ${latestYear}` : "N/A", }; }, },