-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: adds calculator to compensation page for temp release (#977)
- Loading branch information
Showing
12 changed files
with
124 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
"use client"; | ||
import { useState } from "react"; | ||
|
||
import { | ||
IOption, | ||
RadioButtonGroup, | ||
} from "src/components/forms/radioButtonGroup/RadioButtonGroup"; | ||
import { CompanyLocation } from "studio/lib/interfaces/companyDetails"; | ||
import { CompensationsPage } from "studio/lib/interfaces/compensations"; | ||
import { LocaleDocument } from "studio/lib/interfaces/locale"; | ||
|
||
import BenefitsByLocation from "./components/benefitsByLocation/BenefitsByLocation"; | ||
import YearlyBonuses from "./components/yearlyBonuses/YearlyBonuses"; | ||
|
||
interface CompensationsProps { | ||
compensations: CompensationsPage; | ||
locations: CompanyLocation[]; | ||
locale: LocaleDocument; | ||
} | ||
|
||
export default function CompensationSelector({ | ||
compensations, | ||
locations, | ||
locale, | ||
}: CompensationsProps) { | ||
const [selectedLocation, setSelectedLocation] = useState<string>( | ||
locations[0]._id, | ||
); | ||
|
||
const locationOptions: IOption[] = locations.map((companyLocation) => ({ | ||
id: companyLocation._id, | ||
label: companyLocation.companyLocationName, | ||
})); | ||
|
||
const benefitsFilteredByLocation = | ||
compensations.benefitsByLocation.find( | ||
(benefit) => benefit.location._ref === selectedLocation, | ||
)?.benefits || []; | ||
|
||
const yearlyBonusesForLocation = compensations.bonusesByLocation.find( | ||
(b) => b.location._ref === selectedLocation, | ||
)?.yearlyBonuses; | ||
|
||
return ( | ||
<> | ||
{yearlyBonusesForLocation && ( | ||
<YearlyBonuses bonuses={yearlyBonusesForLocation} locale={locale} /> | ||
)} | ||
<RadioButtonGroup | ||
id="location-group" | ||
label="Velg lokasjon" | ||
options={locationOptions} | ||
selectedId={selectedLocation} | ||
onValueChange={(option) => setSelectedLocation(option.id)} | ||
/> | ||
|
||
<BenefitsByLocation benefits={benefitsFilteredByLocation} /> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,156 +1,39 @@ | ||
"use client"; | ||
import { useMemo, useState } from "react"; | ||
|
||
import { | ||
IOption, | ||
RadioButtonGroup, | ||
} from "src/components/forms/radioButtonGroup/RadioButtonGroup"; | ||
import CompensationCalculator from "src/components/sections/compensation-calculator/CompensationCalculator"; | ||
import Text from "src/components/text/Text"; | ||
import { formatAsCurrency } from "src/utils/i18n"; | ||
import { CompanyLocation } from "studio/lib/interfaces/companyDetails"; | ||
import { CompensationsPage } from "studio/lib/interfaces/compensations"; | ||
import { LocaleDocument } from "studio/lib/interfaces/locale"; | ||
|
||
import styles from "./compensations.module.css"; | ||
import BenefitsByLocation from "./components/benefitsByLocation/BenefitsByLocation"; | ||
import SalaryCalculator, { | ||
Degree, | ||
} from "./components/salaryCalculator/SalaryCalculator"; | ||
import YearlyBonuses from "./components/yearlyBonuses/YearlyBonuses"; | ||
import { | ||
calculatePension, | ||
calculateSalary, | ||
maxSalariesExaminationYear, | ||
minSalariesExaminationYear, | ||
salariesFromLocation, | ||
} from "./utils/salary"; | ||
import CompensationSelector from "./CompensationSelector"; | ||
|
||
interface CompensationsProps { | ||
compensations: CompensationsPage; | ||
locations: CompanyLocation[]; | ||
locale: LocaleDocument; | ||
language: string; | ||
} | ||
|
||
interface SalaryCalculatorFormState { | ||
examinationYear: number; | ||
selectedDegree: Degree; | ||
} | ||
|
||
const Compensations = ({ | ||
export default async function Compensations({ | ||
compensations, | ||
locations, | ||
locale, | ||
}: CompensationsProps) => { | ||
const [selectedLocation, setSelectedLocation] = useState<string>( | ||
locations[0]._id, | ||
); | ||
const currentYear = new Date().getFullYear(); | ||
const [salary, setSalary] = useState<number | null>(null); | ||
const [formState, setFormState] = useState<SalaryCalculatorFormState>({ | ||
examinationYear: currentYear - 1, | ||
selectedDegree: "bachelor", | ||
}); | ||
|
||
const currentYearSalariesResult = useMemo( | ||
() => | ||
salariesFromLocation( | ||
currentYear, | ||
selectedLocation, | ||
compensations.salariesByLocation, | ||
), | ||
[currentYear, selectedLocation, compensations.salariesByLocation], | ||
); | ||
|
||
const updateSelectedDegree = (newDegree: Degree) => { | ||
setFormState((prevState) => ({ | ||
...prevState, | ||
selectedDegree: newDegree, | ||
})); | ||
}; | ||
|
||
const updateExaminationYear = (newYear: number) => { | ||
setFormState((prevState) => ({ | ||
...prevState, | ||
examinationYear: newYear, | ||
})); | ||
}; | ||
|
||
const handleSubmit = (event: React.FormEvent) => { | ||
event.preventDefault(); | ||
if (!currentYearSalariesResult.ok) return; | ||
const salary = calculateSalary( | ||
formState.examinationYear, | ||
formState.selectedDegree, | ||
currentYearSalariesResult.value, | ||
); | ||
if (salary === undefined) return; | ||
setSalary(salary); | ||
}; | ||
|
||
const locationOptions: IOption[] = locations.map((companyLocation) => ({ | ||
id: companyLocation._id, | ||
label: companyLocation.companyLocationName, | ||
})); | ||
|
||
const benefitsFilteredByLocation = | ||
compensations.benefitsByLocation.find( | ||
(benefit) => benefit.location._ref === selectedLocation, | ||
)?.benefits || []; | ||
|
||
const yearlyBonusesForLocation = compensations.bonusesByLocation.find( | ||
(b) => b.location._ref === selectedLocation, | ||
)?.yearlyBonuses; | ||
|
||
language, | ||
}: CompensationsProps) { | ||
return ( | ||
<div className={styles.wrapper}> | ||
<Text type="h1">{compensations.basicTitle}</Text> | ||
<RadioButtonGroup | ||
id="location-group" | ||
label="Velg lokasjon" | ||
options={locationOptions} | ||
selectedId={selectedLocation} | ||
onValueChange={(option) => setSelectedLocation(option.id)} | ||
|
||
<CompensationCalculator | ||
section={compensations.compensationCalculator} | ||
language={language} | ||
/> | ||
|
||
<CompensationSelector | ||
compensations={compensations} | ||
locations={locations} | ||
locale={locale} | ||
/> | ||
{compensations.showSalaryCalculator && currentYearSalariesResult.ok && ( | ||
<> | ||
<SalaryCalculator | ||
examinationYearValue={formState.examinationYear} | ||
minExaminationYear={minSalariesExaminationYear( | ||
currentYearSalariesResult.value, | ||
)} | ||
maxExaminationYear={ | ||
maxSalariesExaminationYear(currentYearSalariesResult.value) - 1 | ||
} | ||
selectedDegree={formState.selectedDegree} | ||
onDegreeChanged={updateSelectedDegree} | ||
onExaminationYearChanged={updateExaminationYear} | ||
onSubmit={handleSubmit} | ||
/> | ||
{salary !== null ? ( | ||
<div aria-live="polite"> | ||
<Text> | ||
{`Du vil få en årlig lønn på ${formatAsCurrency(salary, locale.locale, locale.currency)}`} | ||
</Text> | ||
{compensations.pensionPercent && ( | ||
<Text> | ||
Du vil få en årlig pensjon på omtrent{" "} | ||
{formatAsCurrency( | ||
calculatePension(salary, compensations.pensionPercent), | ||
locale.locale, | ||
locale.currency, | ||
)} | ||
</Text> | ||
)} | ||
</div> | ||
) : null} | ||
</> | ||
)} | ||
{yearlyBonusesForLocation && ( | ||
<YearlyBonuses bonuses={yearlyBonusesForLocation} locale={locale} /> | ||
)} | ||
<BenefitsByLocation benefits={benefitsFilteredByLocation} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Compensations; | ||
} |
This file was deleted.
Oops, something went wrong.
69 changes: 0 additions & 69 deletions
69
src/components/compensations/components/salaryCalculator/SalaryCalculator.tsx
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.