diff --git a/src/app/(sok)/_components/filters/Engagement.tsx b/src/app/(sok)/_components/filters/Engagement.tsx index 686d80c59..e628e0c47 100644 --- a/src/app/(sok)/_components/filters/Engagement.tsx +++ b/src/app/(sok)/_components/filters/Engagement.tsx @@ -2,7 +2,7 @@ import React, { ReactElement } from "react"; import { BodyShort, Checkbox, CheckboxGroup } from "@navikt/ds-react"; import moveFilterToBottom from "@/app/(sok)/_components/utils/moveFilterToBottom"; import mergeCount from "@/app/(sok)/_components/utils/mergeCount"; -import sortValuesByFirstLetter from "@/app/(sok)/_components/utils/sortValuesByFirstLetter"; +import sortFiltersAlphabetically from "@/app/(sok)/_components/utils/sortFiltersAlphabetically"; import { logFilterChanged } from "@/app/_common/monitoring/amplitude"; import { ENGAGEMENT_TYPE } from "@/app/(sok)/_components/searchParamNames"; import useSearchQuery from "@/app/(sok)/_components/SearchQueryProvider"; @@ -27,7 +27,7 @@ interface EngagementProps { } export default function Engagement({ initialValues, updatedValues }: EngagementProps): ReactElement { - const sortedValuesByFirstLetter = sortValuesByFirstLetter(initialValues); + const sortedValuesByFirstLetter = sortFiltersAlphabetically(initialValues); const sortedValues = moveFilterToBottom(sortedValuesByFirstLetter, "Annet"); const values = mergeCount(sortedValues, updatedValues); const searchQuery = useSearchQuery(); diff --git a/src/app/(sok)/_components/filters/Occupations.tsx b/src/app/(sok)/_components/filters/Occupations.tsx index 7f2bc7891..0c733ff5e 100644 --- a/src/app/(sok)/_components/filters/Occupations.tsx +++ b/src/app/(sok)/_components/filters/Occupations.tsx @@ -2,7 +2,7 @@ import React, { ReactElement } from "react"; import { BodyShort, Box, Checkbox, CheckboxGroup, ReadMore } from "@navikt/ds-react"; import moveFilterToBottom from "@/app/(sok)/_components/utils/moveFilterToBottom"; import { mergeCountOccupations } from "@/app/(sok)/_components/utils/mergeCount"; -import sortValuesByFirstLetter from "@/app/(sok)/_components/utils/sortValuesByFirstLetter"; +import sortFiltersAlphabetically from "@/app/(sok)/_components/utils/sortFiltersAlphabetically"; import { logFilterChanged } from "@/app/_common/monitoring/amplitude"; import { OCCUPATION_FIRST_LEVEL, OCCUPATION_SECOND_LEVEL } from "@/app/(sok)/_components/searchParamNames"; import useSearchQuery from "@/app/(sok)/_components/SearchQueryProvider"; @@ -21,14 +21,14 @@ interface OccupationsProps { export default function Occupations({ initialValues, updatedValues }: OccupationsProps): ReactElement { const withSortedSecondLevelOccupations = initialValues.map((item) => { - const secondLevel = sortValuesByFirstLetter(item.occupationSecondLevels); + const secondLevel = sortFiltersAlphabetically(item.occupationSecondLevels); return { secondLevel, ...item, }; }); - const sortedByLetterFirstLevelOccupations = sortValuesByFirstLetter(withSortedSecondLevelOccupations); + const sortedByLetterFirstLevelOccupations = sortFiltersAlphabetically(withSortedSecondLevelOccupations); const sortedValues = moveFilterToBottom( sortedByLetterFirstLevelOccupations, OCCUPATION_LEVEL_OTHER, diff --git a/src/app/(sok)/_components/filters/Published.tsx b/src/app/(sok)/_components/filters/Published.tsx index c607eb1c1..ca2069a6a 100644 --- a/src/app/(sok)/_components/filters/Published.tsx +++ b/src/app/(sok)/_components/filters/Published.tsx @@ -1,7 +1,7 @@ import React, { ReactElement } from "react"; import { Radio, RadioGroup } from "@navikt/ds-react"; import mergeCount from "@/app/(sok)/_components/utils/mergeCount"; -import sortPublishedValues from "@/app/(sok)/_components/utils/sortPublishedValues"; +import sortPublishedFiltersByDayOffset from "@/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset"; import { logFilterChanged } from "@/app/_common/monitoring/amplitude"; import { PUBLISHED } from "@/app/(sok)/_components/searchParamNames"; import useSearchQuery from "@/app/(sok)/_components/SearchQueryProvider"; @@ -15,7 +15,7 @@ interface PublishedProps { } export default function Published({ initialValues, updatedValues, publishedTotalCount }: PublishedProps): ReactElement { - const sortedValues = sortPublishedValues(initialValues); + const sortedValues = sortPublishedFiltersByDayOffset(initialValues); const values = mergeCount(sortedValues, updatedValues); const searchQuery = useSearchQuery(); diff --git a/src/app/(sok)/_components/filters/Remote.tsx b/src/app/(sok)/_components/filters/Remote.tsx index abc42b238..1cbeb82bc 100644 --- a/src/app/(sok)/_components/filters/Remote.tsx +++ b/src/app/(sok)/_components/filters/Remote.tsx @@ -2,7 +2,7 @@ import React, { ReactElement } from "react"; import { BodyShort, Checkbox, CheckboxGroup } from "@navikt/ds-react"; import moveFilterToBottom from "@/app/(sok)/_components/utils/moveFilterToBottom"; import mergeCount from "@/app/(sok)/_components/utils/mergeCount"; -import sortRemoteValues from "@/app/(sok)/_components/utils/sortRemoteValues"; +import sortRemoteFilters from "@/app/(sok)/_components/utils/sortRemoteFilters"; import { logFilterChanged } from "@/app/_common/monitoring/amplitude"; import { REMOTE } from "@/app/(sok)/_components/searchParamNames"; import useSearchQuery from "@/app/(sok)/_components/SearchQueryProvider"; @@ -14,7 +14,7 @@ interface RemoteProps { } export default function Remote({ initialValues, updatedValues }: RemoteProps): ReactElement { - const sortedValuesByFirstLetter = sortRemoteValues(initialValues); + const sortedValuesByFirstLetter = sortRemoteFilters(initialValues); const sortedValues = moveFilterToBottom(sortedValuesByFirstLetter, "Ikke oppgitt"); const values = mergeCount(sortedValues, updatedValues); const searchQuery = useSearchQuery(); diff --git a/src/app/(sok)/_components/filters/Sector.tsx b/src/app/(sok)/_components/filters/Sector.tsx index 51961382c..60290488e 100644 --- a/src/app/(sok)/_components/filters/Sector.tsx +++ b/src/app/(sok)/_components/filters/Sector.tsx @@ -2,7 +2,7 @@ import React, { ReactElement } from "react"; import { BodyShort, Checkbox, CheckboxGroup } from "@navikt/ds-react"; import moveFilterToBottom from "@/app/(sok)/_components/utils/moveFilterToBottom"; import mergeCount from "@/app/(sok)/_components/utils/mergeCount"; -import sortValuesByFirstLetter from "@/app/(sok)/_components/utils/sortValuesByFirstLetter"; +import sortFiltersAlphabetically from "@/app/(sok)/_components/utils/sortFiltersAlphabetically"; import { logFilterChanged } from "@/app/_common/monitoring/amplitude"; import { SECTOR } from "@/app/(sok)/_components/searchParamNames"; import useSearchQuery from "@/app/(sok)/_components/SearchQueryProvider"; @@ -14,7 +14,7 @@ interface SectorProps { } export default function Sector({ initialValues, updatedValues }: SectorProps): ReactElement { - const sortedValuesByFirstLetter = sortValuesByFirstLetter(initialValues); + const sortedValuesByFirstLetter = sortFiltersAlphabetically(initialValues); const sortedValues = moveFilterToBottom(sortedValuesByFirstLetter, "Ikke oppgitt"); const values = mergeCount(sortedValues, updatedValues); const searchQuery = useSearchQuery(); diff --git a/src/app/(sok)/_components/searchBox/buildSearchBoxOptions.ts b/src/app/(sok)/_components/searchBox/buildSearchBoxOptions.ts index 29809b8ec..d764fa154 100644 --- a/src/app/(sok)/_components/searchBox/buildSearchBoxOptions.ts +++ b/src/app/(sok)/_components/searchBox/buildSearchBoxOptions.ts @@ -1,6 +1,6 @@ import { editedItemKey } from "@/app/(sok)/_components/filters/Engagement"; import { editedItemKey as editedOccupation } from "@/app/(sok)/_components/filters/Occupations"; -import sortValuesByFirstLetter from "@/app/(sok)/_components/utils/sortValuesByFirstLetter"; +import sortFiltersAlphabetically from "@/app/(sok)/_components/utils/sortFiltersAlphabetically"; import fixLocationName from "@/app/_common/utils/fixLocationName"; import buildLocations from "@/app/(sok)/_components/utils/buildLocations"; import { labelForNeedDriversLicense } from "@/app/(sok)/_components/filters/DriversLicense"; @@ -87,7 +87,7 @@ function getCountryOptions(locationList: LocationList[]): ComboboxOption[] { // eslint-disable-next-line @typescript-eslint/explicit-function-return-type function withSortedSecondLevelOccupations(aggregations: FilterAggregations) { return aggregations.occupationFirstLevels.map((item) => { - const secondLevel = sortValuesByFirstLetter(item.occupationSecondLevels); + const secondLevel = sortFiltersAlphabetically(item.occupationSecondLevels); return { secondLevel, ...item, @@ -96,7 +96,7 @@ function withSortedSecondLevelOccupations(aggregations: FilterAggregations) { } function getFirstLevelOccupationsOptions(aggregations: FilterAggregations): ComboboxOption[] { - return sortValuesByFirstLetter(withSortedSecondLevelOccupations(aggregations)).map( + return sortFiltersAlphabetically(withSortedSecondLevelOccupations(aggregations)).map( (occupation: { key: string }): ComboboxOption => editedOccupation(occupation.key) === "Ikke oppgitt" ? { label: "Yrke ikke oppgitt", value: `${OCCUPATION_FIRST_LEVEL}-${occupation.key}` } diff --git a/src/app/(sok)/_components/utils/sortFiltersAlphabetically.test.ts b/src/app/(sok)/_components/utils/sortFiltersAlphabetically.test.ts new file mode 100644 index 000000000..f6a9698bf --- /dev/null +++ b/src/app/(sok)/_components/utils/sortFiltersAlphabetically.test.ts @@ -0,0 +1,27 @@ +import { describe, expect, test } from "vitest"; +import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; +import sortFiltersAlphabetically from "@/app/(sok)/_components/utils/sortFiltersAlphabetically"; + +describe("sortFiltersAlphabetically", () => { + test("Should sort filters alphabetically", () => { + const filters: FilterAggregation[] = [ + { key: "a", count: 1 }, + { key: "a2", count: 1 }, + { key: "a1", count: 1 }, + { key: "c", count: 1 }, + { key: "b", count: 1 }, + ]; + + const expected: FilterAggregation[] = [ + { key: "a", count: 1 }, + { key: "a1", count: 1 }, + { key: "a2", count: 1 }, + { key: "b", count: 1 }, + { key: "c", count: 1 }, + ]; + + const result = sortFiltersAlphabetically(filters); + + expect(result).toEqual(expected); + }); +}); diff --git a/src/app/(sok)/_components/utils/sortFiltersAlphabetically.ts b/src/app/(sok)/_components/utils/sortFiltersAlphabetically.ts new file mode 100644 index 000000000..341e501c4 --- /dev/null +++ b/src/app/(sok)/_components/utils/sortFiltersAlphabetically.ts @@ -0,0 +1,9 @@ +import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; + +export default function sortFiltersAlphabetically(filters: FilterAggregation[]): FilterAggregation[] { + const clone = filters; + + clone.sort((a, b) => a.key.localeCompare(b.key, "no")); + + return clone; +} diff --git a/src/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset.test.ts b/src/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset.test.ts new file mode 100644 index 000000000..1aa30c32f --- /dev/null +++ b/src/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, test } from "vitest"; +import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; +import sortPublishedFiltersByDayOffset from "@/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset"; + +describe("sortPublishedFiltersByDayOffset", () => { + test("Should sort filters by days offset", () => { + const filters: FilterAggregation[] = [ + { key: "now-3d", count: 1 }, + { key: "now-7d", count: 1 }, + { key: "now/d", count: 1 }, + ]; + + const expected: FilterAggregation[] = [ + { key: "now/d", count: 1 }, + { key: "now-3d", count: 1 }, + { key: "now-7d", count: 1 }, + ]; + + const result = sortPublishedFiltersByDayOffset(filters); + + expect(result).toEqual(expected); + }); +}); diff --git a/src/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset.ts b/src/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset.ts new file mode 100644 index 000000000..9e45410d8 --- /dev/null +++ b/src/app/(sok)/_components/utils/sortPublishedFiltersByDayOffset.ts @@ -0,0 +1,10 @@ +import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; + +export default function sortPublishedFiltersByDayOffset(filters: FilterAggregation[]): FilterAggregation[] { + const publishedValuesByDayOffset = ["now/d", "now-3d", "now-7d"]; + const clone = filters; + + clone.sort((a, b) => publishedValuesByDayOffset.indexOf(a.key) - publishedValuesByDayOffset.indexOf(b.key)); + + return clone; +} diff --git a/src/app/(sok)/_components/utils/sortPublishedValues.js b/src/app/(sok)/_components/utils/sortPublishedValues.js deleted file mode 100644 index 2644160e7..000000000 --- a/src/app/(sok)/_components/utils/sortPublishedValues.js +++ /dev/null @@ -1,11 +0,0 @@ -/** - * Sorter publisert basert på egen array - */ -export default function sortPublishedValues(facets) { - const sortedPublishedValues = ["now/d", "now-3d", "now-7d"]; - const clone = facets; - - clone.sort((a, b) => sortedPublishedValues.indexOf(a.key) - sortedPublishedValues.indexOf(b.key)); - - return clone; -} diff --git a/src/app/(sok)/_components/utils/sortRemoteFilters.test.ts b/src/app/(sok)/_components/utils/sortRemoteFilters.test.ts new file mode 100644 index 000000000..e81df935c --- /dev/null +++ b/src/app/(sok)/_components/utils/sortRemoteFilters.test.ts @@ -0,0 +1,23 @@ +import { describe, expect, test } from "vitest"; +import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; +import sortRemoteFilters from "@/app/(sok)/_components/utils/sortRemoteFilters"; + +describe("sortRemoteFilters", () => { + test("Should sort filters by expected order", () => { + const filters: FilterAggregation[] = [ + { key: "Hjemmekontor ikke mulig", count: 1 }, + { key: "Hybridkontor", count: 1 }, + { key: "Hjemmekontor", count: 1 }, + ]; + + const expected: FilterAggregation[] = [ + { key: "Hybridkontor", count: 1 }, + { key: "Hjemmekontor", count: 1 }, + { key: "Hjemmekontor ikke mulig", count: 1 }, + ]; + + const result = sortRemoteFilters(filters); + + expect(result).toEqual(expected); + }); +}); diff --git a/src/app/(sok)/_components/utils/sortRemoteValues.js b/src/app/(sok)/_components/utils/sortRemoteFilters.ts similarity index 52% rename from src/app/(sok)/_components/utils/sortRemoteValues.js rename to src/app/(sok)/_components/utils/sortRemoteFilters.ts index b1fd82d23..37ac0ae5f 100644 --- a/src/app/(sok)/_components/utils/sortRemoteValues.js +++ b/src/app/(sok)/_components/utils/sortRemoteFilters.ts @@ -1,9 +1,8 @@ -/** - * Sorter publisert basert på egen array - */ -export default function sortRemoteValues(facets) { +import { FilterAggregation } from "@/app/(sok)/_types/FilterAggregations"; + +export default function sortRemoteFilters(filter: FilterAggregation[]): FilterAggregation[] { const sortedPublishedValues = ["Hybridkontor", "Hjemmekontor", "Hjemmekontor ikke mulig"]; - const clone = facets; + const clone = filter; clone.sort((a, b) => sortedPublishedValues.indexOf(a.key) - sortedPublishedValues.indexOf(b.key)); diff --git a/src/app/(sok)/_components/utils/sortValuesByFirstLetter.js b/src/app/(sok)/_components/utils/sortValuesByFirstLetter.js deleted file mode 100644 index 33ebf7af5..000000000 --- a/src/app/(sok)/_components/utils/sortValuesByFirstLetter.js +++ /dev/null @@ -1,10 +0,0 @@ -/** - * Sorter verdier alfabetisk - */ -export default function sortValuesByFirstLetter(facets) { - const clone = facets; - - clone.sort((a, b) => a.key.localeCompare(b.key, "no")); - - return clone; -}