diff --git a/src/salaryAndBenefits/SalaryAndBenefits.tsx b/src/salaryAndBenefits/SalaryAndBenefits.tsx index 7faf2ebb6..967277679 100644 --- a/src/salaryAndBenefits/SalaryAndBenefits.tsx +++ b/src/salaryAndBenefits/SalaryAndBenefits.tsx @@ -1,10 +1,14 @@ -"use client" +"use client"; import styles from "./salaryAndBenefits.module.css"; import Text from "src/components/text/Text"; import SalaryCalculator from "./components/salaryCalculator/SalaryCalculator"; import { useState } from "react"; -import { calculatePension } from "./utils/calculateSalary"; +import { + calculatePension, + calculateSalary, + maxExperience, +} from "./utils/calculateSalary"; import { SalaryAndBenefitsPage } from "studio/lib/payloads/salaryAndBenefits"; import { RichText } from "src/components/richText/RichText"; @@ -12,24 +16,63 @@ interface SalaryAndBenefitsProps { salaryAndBenefits: SalaryAndBenefitsPage; } -const SalaryAndBenefits = ({salaryAndBenefits}: SalaryAndBenefitsProps) => { +interface FormState { + examinationYear: number; + selectedDegree: "bachelor" | "master"; +} + +const SalaryAndBenefits = ({ salaryAndBenefits }: SalaryAndBenefitsProps) => { const [salary, setSalary] = useState(null); + const thisYear = new Date().getFullYear(); + const [formState, setFormState] = useState({ + examinationYear: thisYear-1, + selectedDegree: "master", + }); + + const updateSelectedDegree = (newDegree: "bachelor" | "master") => { + setFormState((prevState) => ({ + ...prevState, + selectedDegree: newDegree, + })); + }; - const handleSalaryUpdate = (newSalary: number) => { + const updateExaminationYear = (newYear: number) => { + setFormState((prevState) => ({ + ...prevState, + examinationYear: newYear, + })); + }; + + const handleSalaryUpdate = () => { + const newSalary = calculateSalary( + thisYear, + formState.examinationYear, + formState.selectedDegree + ); setSalary(newSalary); }; - + return (
{salaryAndBenefits.basicTitle} - {salaryAndBenefits.showSalaryCalculator && - } - {salary !== null ? -
- Du vil få en årlig lønn på {salary} - - Du vil få en årlig pensjon på omtrent {calculatePension(salary)} -
: null } + {salaryAndBenefits.showSalaryCalculator && ( + + )} + {salary !== null ? ( +
+ Du vil få en årlig lønn på {salary} + + Du vil få en årlig pensjon på omtrent {calculatePension(salary)} + +
+ ) : null}
{salaryAndBenefits.benefits.map((benefit) => (
diff --git a/src/salaryAndBenefits/components/salaryCalculator/SalaryCalculator.tsx b/src/salaryAndBenefits/components/salaryCalculator/SalaryCalculator.tsx index 838055d9a..ee3bea13a 100644 --- a/src/salaryAndBenefits/components/salaryCalculator/SalaryCalculator.tsx +++ b/src/salaryAndBenefits/components/salaryCalculator/SalaryCalculator.tsx @@ -1,17 +1,8 @@ -"use client"; - -import { useState } from "react"; import styles from "./salaryCalculator.module.css"; import InputField from "src/components/forms/inputField/InputField"; import { RadioButtonGroup } from "src/components/forms/radioButtonGroup/RadioButtonGroup"; -import { calculateSalary, maxExperience } from "src/salaryAndBenefits/utils/calculateSalary"; import Button from "src/components/buttons/Button"; - interface FormState { - examinationYear: number - selectedDegree: "bachelor" | "master", - } - interface IOption { id: string; label: string; @@ -20,58 +11,64 @@ interface IOption { } const options: IOption[] = [ - { id: "bachelor", label: "Bachelor", currentSelected: false, disabled: false }, + { + id: "bachelor", + label: "Bachelor", + currentSelected: false, + disabled: false, + }, { id: "master", label: "Master", currentSelected: true, disabled: false }, ]; interface SalaryCalculatorProps { - onSalaryUpdate: (salary: number) => void; - } - -export default function SalaryCalculator({ onSalaryUpdate }: SalaryCalculatorProps) { - const thisYear = new Date().getFullYear(); - - const [formState, setFormState] = useState({ - examinationYear: thisYear, - selectedDegree: "master", - }); - - const updateSelectedDegree = (newDegree: string) => { - setFormState((prevState) => ({ - ...prevState, - selectedDegree: newDegree as "bachelor" | "master", - })); - }; - - const updateExaminationYear = (newYear: number) => { - setFormState((prevState) => ({ - ...prevState, - examinationYear: newYear, - })); - }; - - const handleSubmit = (event: React.FormEvent) => { - event.preventDefault(); - const salary = calculateSalary(thisYear, formState.examinationYear, formState.selectedDegree); - onSalaryUpdate(salary); + formState: { + examinationYear: number; + selectedDegree: "bachelor" | "master"; }; + onUpdateSelectedDegree: (newDegree: "bachelor" | "master") => void; + onUpdateExaminationYear: (newYear: number) => void; + onSubmit: () => void; + thisYear: number; + maxExperience: (year: number) => number; +} +export default function SalaryCalculator({ + formState, + onUpdateSelectedDegree, + onUpdateExaminationYear, + onSubmit, + thisYear, + maxExperience, +}: SalaryCalculatorProps) { return ( -
+ { + e.preventDefault(); + onSubmit(); + }} + > updateSelectedDegree(value.id)} + onValueChange={(value) => + onUpdateSelectedDegree(value.id as "bachelor" | "master") + } /> updateExaminationYear(typeof value === "string" ? parseInt(value) : value)} + onChange={(_name, value) => + onUpdateExaminationYear( + typeof value === "string" ? parseInt(value) : value + ) + } required />