Skip to content

Commit

Permalink
refactor: fetching salary data
Browse files Browse the repository at this point in the history
  • Loading branch information
mikaelbr committed Dec 6, 2024
1 parent ea15b5a commit b8acf81
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ 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";
import { LOCALE_QUERY } from "studio/lib/queries/locale";
import { loadStudioQuery } from "studio/lib/store";

import { getHandbookLinksFromCompensationPage, getSalaryByYear } from "./api";
import {
getHandbookLinksFromCompensationPage,
getLatestSalaries,
getLocale,
} from "./api";
import Calculator from "./Calculator";
import styles from "./compensation-calculator.module.css";

Expand All @@ -23,10 +24,9 @@ export default async function CompensationCalculator({
section,
language,
}: CompensationCalculatorProps) {
const salariesRes = getSalaryByYear(2024, language);
const localeRes = loadStudioQuery<LocaleDocument>(LOCALE_QUERY).then(
(d) => d.data,
);
const salariesRes = getLatestSalaries();
const localeRes = getLocale();

const handbookLinksRes = await getHandbookLinksFromCompensationPage(language);

const calculatorBgClassname = cnIf({
Expand Down
22 changes: 12 additions & 10 deletions src/components/sections/compensation-calculator/api.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { isSalariesType } from "studio/components/salariesInput/utils/parseSalaries";
import { LocaleDocument } from "studio/lib/interfaces/locale";
import { ILink } from "studio/lib/interfaces/navigation";
import { LOCALE_QUERY } from "studio/lib/queries/locale";
import {
COMPENSATIONS_HANDBOOK_LINKS,
COMPENSATIONS_SALARY_BY_YEAR,
Expand All @@ -26,19 +28,19 @@ export async function getHandbookLinksFromCompensationPage(
return ResultOk(res.data.handbookLinks);
}

export async function getSalaryByYear(
year: number,
language: string,
): Promise<Result<SalaryData, unknown>> {
export async function getLocale() {
const res = await loadStudioQuery<LocaleDocument>(LOCALE_QUERY);
return res.data;
}

export async function getLatestSalaries(): Promise<
Result<SalaryData, unknown>
> {
const res = await loadStudioQuery<{
slug: string;
salariesByLocation: { yearlySalaries: { salaries: string } };
}>(
COMPENSATIONS_SALARY_BY_YEAR,
{
year,
language,
},
{},
{
cache: "force-cache",
next: {
Expand All @@ -52,7 +54,7 @@ export async function getSalaryByYear(
res.data.salariesByLocation.yearlySalaries.salaries,
);

if (!isSalariesType(parsedSalaries) || !parsedSalaries[year]) {
if (!isSalariesType(parsedSalaries)) {
return ResultError("Parsed salaries data was not valid");
}

Expand Down
12 changes: 2 additions & 10 deletions studio/lib/queries/specialPages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,12 @@ export const COMPENSATIONS_PAGE_BY_SLUG_QUERY = groq`
},
}
`;
export const COMPENSATIONS_SALARIES = groq`
*[_type == "compensations"][0] {
"slug": ${translatedFieldFragment("slug")},
"salariesByLocation": salariesByLocation[] {
...
},
}
`;

// Just select the first location and the first year..
// @TODO: make this a bit more robust.
// @TODO: Check if we need to make this more robust,
// but yearlySalaries is sorted by year so [0] should be the latest
export const COMPENSATIONS_SALARY_BY_YEAR = groq`
*[_type == "compensations"][0] {
"slug": ${translatedFieldFragment("slug")},
"salariesByLocation": salariesByLocation[0] {
"yearlySalaries": yearlySalaries[0] {
...
Expand Down

0 comments on commit b8acf81

Please sign in to comment.