-
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.
- Loading branch information
Showing
5 changed files
with
219 additions
and
8 deletions.
There are no files selected for viewing
140 changes: 140 additions & 0 deletions
140
src/components/sections/compensation-calculator/Calculator.tsx
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,140 @@ | ||
"use client"; | ||
|
||
import { use, useEffect, useOptimistic, useState } from "react"; | ||
import Text from "src/components/text/Text"; | ||
import { formatAsCurrency } from "src/utils/i18n"; | ||
import { LocaleDocument } from "studio/lib/interfaces/locale"; | ||
import { getSalaryByYear } from "./actions"; | ||
import { | ||
IOption, | ||
RadioButtonGroup, | ||
} from "src/components/forms/radioButtonGroup/RadioButtonGroup"; | ||
import InputField from "src/components/forms/inputField/InputField"; | ||
import Button from "src/components/buttons/Button"; | ||
|
||
export type Degree = "bachelor" | "master"; | ||
|
||
type CalculatorProps = { | ||
initialSalary: number; | ||
localeRes: Promise<LocaleDocument>; | ||
}; | ||
|
||
type SalarySettings = { | ||
year: number; | ||
degree: Degree; | ||
}; | ||
|
||
export default function Calculator({ | ||
localeRes, | ||
initialSalary, | ||
}: CalculatorProps) { | ||
const locale = use(localeRes); | ||
const [year, setYear] = useState(2024); | ||
const [degree, setDegree] = useState<Degree>("bachelor"); | ||
const [salary, setSalary] = useState(initialSalary); | ||
|
||
if (!locale) { | ||
console.error( | ||
"[CompensationCalculator]: Sanity data not found. Not rendering CompensationCalculator.", | ||
); | ||
return null; | ||
} | ||
|
||
useEffect(() => { | ||
async function fetchSalary() { | ||
const salaryByYear = await getSalaryByYear(year); | ||
console.log(salaryByYear); | ||
|
||
setSalary(salaryByYear.ok ? salaryByYear.value : initialSalary); | ||
} | ||
|
||
fetchSalary(); | ||
}, [year, degree]); | ||
|
||
return ( | ||
<div> | ||
<SalaryCalculator | ||
examinationYearValue={year} | ||
minExaminationYear={1985} | ||
maxExaminationYear={2024} | ||
selectedDegree={degree} | ||
onDegreeChanged={setDegree} | ||
onExaminationYearChanged={setYear} | ||
action={async () => { | ||
const salaryByYear = await getSalaryByYear(2024); | ||
|
||
console.log(salaryByYear); | ||
}} | ||
/> | ||
|
||
{salary !== null ? ( | ||
<div aria-live="polite"> | ||
<Text>Din årslønn</Text> | ||
<Text> | ||
{salary} | ||
{/* {formatAsCurrency(salary, locale.locale, locale.currency)} */} | ||
</Text> | ||
<Text>+ bonus</Text> | ||
</div> | ||
) : null} | ||
</div> | ||
); | ||
} | ||
|
||
const degreeOptions: IOption[] = [ | ||
{ id: "bachelor", label: "Bachelor" }, | ||
{ id: "master", label: "Master" }, | ||
]; | ||
|
||
interface SalaryCalculatorProps { | ||
examinationYearValue: number; | ||
minExaminationYear: number; | ||
maxExaminationYear: number; | ||
selectedDegree: Degree; | ||
onDegreeChanged: (degree: Degree) => void; | ||
onExaminationYearChanged: (examinationYear: number) => void; | ||
action: (data: FormData) => void; | ||
} | ||
|
||
function SalaryCalculator({ | ||
examinationYearValue: yearValue, | ||
minExaminationYear, | ||
maxExaminationYear, | ||
selectedDegree, | ||
onDegreeChanged, | ||
onExaminationYearChanged, | ||
action, | ||
}: SalaryCalculatorProps) { | ||
return ( | ||
<form | ||
//TODO: replace aria-label with static translation from Sanity | ||
aria-label="salary calculator" | ||
action={action} | ||
> | ||
<RadioButtonGroup | ||
id="degree-group" | ||
label="Velg utdanning" | ||
options={degreeOptions} | ||
selectedId={selectedDegree} | ||
onValueChange={(selectedOption) => | ||
onDegreeChanged(selectedOption.id as Degree) | ||
} | ||
/> | ||
<div> | ||
<InputField | ||
label="Year" | ||
name="examinationYear" | ||
type="number" | ||
min={minExaminationYear} | ||
max={maxExaminationYear} | ||
value={yearValue} | ||
onChange={(_name, value) => onExaminationYearChanged(parseInt(value))} | ||
required | ||
/> | ||
</div> | ||
<Button type="secondary" size="small"> | ||
Regn ut | ||
</Button> | ||
</form> | ||
); | ||
} |
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
29 changes: 29 additions & 0 deletions
29
src/components/sections/compensation-calculator/actions.ts
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,29 @@ | ||
"use server"; | ||
|
||
import { isSalariesType } from "studio/components/salariesInput/utils/parseSalaries"; | ||
import { COMPENSATIONS_SALARY_BY_YEAR } from "studio/lib/queries/specialPages"; | ||
import { loadStudioQuery } from "studio/lib/store"; | ||
import { Result, ResultError, ResultOk } from "studio/utils/result"; | ||
|
||
export async function getSalaryByYear( | ||
year: number, | ||
): Promise<Result<number, unknown>> { | ||
const res = await loadStudioQuery<{ | ||
salariesByLocation: { yearlySalaries: { salaries: string } }; | ||
}>(COMPENSATIONS_SALARY_BY_YEAR, { | ||
year, | ||
}); | ||
|
||
try { | ||
const parsedSalaries = JSON.parse( | ||
res.data.salariesByLocation.yearlySalaries.salaries, | ||
); | ||
if (!isSalariesType(parsedSalaries) || !parsedSalaries[year]) { | ||
return ResultError("Parsed salaries data was not valid"); | ||
} | ||
|
||
return ResultOk(parsedSalaries[year] as number); | ||
} catch (e) { | ||
return ResultError("Parsed salaries data was not valid"); | ||
} | ||
} |
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,6 @@ | ||
import { SalariesByLocation } from "studio/lib/interfaces/compensations"; | ||
|
||
export type CompensationData = { | ||
slug: string; | ||
salariesByLocation: SalariesByLocation[]; | ||
}; |
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