Skip to content

Commit

Permalink
fix: default initial year
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed Dec 6, 2024
1 parent d5b459d commit 382a367
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 10 additions & 3 deletions src/components/sections/compensation-calculator/Calculator.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"use client";

import { useTranslations } from "next-intl";
import { use, useState } from "react";

import { calculateSalary } from "src/components/compensations/utils/salary";
Expand All @@ -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<LocaleDocument>;
salariesRes: Promise<Result<SalaryData, unknown>>;
background: "light" | "dark" | "violet";
initialDegree: Degree;
initialYear: number;
initialYear?: number;
};

export default function Calculator({
Expand All @@ -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<Degree>(initialDegree);

if (!locale || !salaries.ok) {
Expand Down Expand Up @@ -101,3 +103,8 @@ function getMinMaxYear(salaries: SalaryData) {
const max = Math.max(...years);
return { min, max };
}
function getMaybeMaxYear(salaries: Result<SalaryData, unknown>) {
if (!salaries.ok) return undefined;
const years = Object.keys(salaries.value).map((s) => parseInt(s));
return Math.max(...years);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -55,7 +55,6 @@ export default async function CompensationCalculator({
<Calculator
localeRes={localeRes}
salariesRes={salariesRes}
initialYear={2024}
initialDegree={"master"}
background={radioBackground}
/>
Expand Down

0 comments on commit 382a367

Please sign in to comment.