From af7e753b28a4e0096357b6a26715d126a26dda4d Mon Sep 17 00:00:00 2001 From: Mikael Brevik Date: Fri, 6 Dec 2024 08:47:25 +0100 Subject: [PATCH] fix: default initial year --- .../sections/compensation-calculator/Calculator.tsx | 13 ++++++++++--- .../CompensationCalculator.tsx | 3 +-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/components/sections/compensation-calculator/Calculator.tsx b/src/components/sections/compensation-calculator/Calculator.tsx index 2bdbf137b..a016b7307 100644 --- a/src/components/sections/compensation-calculator/Calculator.tsx +++ b/src/components/sections/compensation-calculator/Calculator.tsx @@ -1,5 +1,6 @@ "use client"; +import { useTranslations } from "next-intl"; import { use, useState } from "react"; import { calculateSalary } from "src/components/compensations/utils/salary"; @@ -15,14 +16,13 @@ import { Result } from "studio/utils/result"; import styles from "./compensation-calculator.module.css"; import { Degree, SalaryData } from "./types"; -import { useTranslations } from "next-intl"; type CalculatorProps = { localeRes: Promise; salariesRes: Promise>; background: "light" | "dark" | "violet"; initialDegree: Degree; - initialYear: number; + initialYear?: number; }; export default function Calculator({ @@ -35,7 +35,9 @@ export default function Calculator({ const t = useTranslations("compensation_calculator"); const locale = use(localeRes); const salaries = use(salariesRes); - const [year, setYear] = useState(initialYear); + const [year, setYear] = useState( + initialYear ?? getMaybeMaxYear(salaries) ?? new Date().getFullYear(), + ); const [degree, setDegree] = useState(initialDegree); if (!locale || !salaries.ok) { @@ -101,3 +103,8 @@ function getMinMaxYear(salaries: SalaryData) { const max = Math.max(...years); return { min, max }; } +function getMaybeMaxYear(salaries: Result) { + if (!salaries.ok) return undefined; + const years = Object.keys(salaries.value).map((s) => parseInt(s)); + return Math.max(...years); +} diff --git a/src/components/sections/compensation-calculator/CompensationCalculator.tsx b/src/components/sections/compensation-calculator/CompensationCalculator.tsx index eb8d1e611..65ad1a246 100644 --- a/src/components/sections/compensation-calculator/CompensationCalculator.tsx +++ b/src/components/sections/compensation-calculator/CompensationCalculator.tsx @@ -3,6 +3,7 @@ import { Suspense } from "react"; import LinkButton from "src/components/linkButton/LinkButton"; import Text from "src/components/text/Text"; +import { cnIf } from "src/utils/css"; import { getHref } from "src/utils/link"; import { LocaleDocument } from "studio/lib/interfaces/locale"; import { CompensationCalculatorSection } from "studio/lib/interfaces/pages"; @@ -12,7 +13,6 @@ import { loadStudioQuery } from "studio/lib/store"; import { getHandbookLinksFromCompensationPage, getSalaryByYear } from "./api"; import Calculator from "./Calculator"; import styles from "./compensation-calculator.module.css"; -import { cnIf } from "src/utils/css"; export interface CompensationCalculatorProps { language: string; @@ -55,7 +55,6 @@ export default async function CompensationCalculator({