Skip to content

Commit

Permalink
Replace enums with union strings
Browse files Browse the repository at this point in the history
This makes the code much more readable and concise
  • Loading branch information
JacobArrow committed Jan 16, 2025
1 parent 40650ac commit 5adc558
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 55 deletions.
19 changes: 13 additions & 6 deletions __tests__/search.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,25 @@ vi.mock(import("@/lib/config/goConfig"), async importOriginal => {
}
})

export const facets = {
[FacetFieldEnum.Materialtypesgeneral]: {
type Facets = {
[key in FacetFieldEnum]?: {
filter: string
translation: string
}
}

export const facets: Facets = {
MATERIALTYPESGENERAL: {
filter: "materialTypesGeneral",
translation: "Type",
},
[FacetFieldEnum.Mainlanguages]: {
MAINLANGUAGES: {
filter: "mainLanguages",
translation: "Sprog",
},
[FacetFieldEnum.Age]: { filter: "age", translation: "Alder" },
[FacetFieldEnum.Lix]: { filter: "lixRange", translation: "Lix" },
[FacetFieldEnum.Subjects]: { filter: "subjects", translation: "Emne" },
AGE: { filter: "age", translation: "Alder" },
LIX: { filter: "lixRange", translation: "Lix" },
SUBJECTS: { filter: "subjects", translation: "Emne" },
} as const

beforeEach(() => {
Expand Down
11 changes: 8 additions & 3 deletions components/pages/workPageLayout/WorkPageHeader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@ import {
import { Badge } from "@/components/shared/badge/Badge"
import { CoverPicture } from "@/components/shared/coverPicture/CoverPicture"
import SlideSelect, { SlideSelectOption } from "@/components/shared/slideSelect/SlideSelect"
import { WorkFullWorkPageFragment } from "@/lib/graphql/generated/fbi/graphql"
import {
GeneralMaterialTypeCodeEnum,
WorkFullWorkPageFragment,
} from "@/lib/graphql/generated/fbi/graphql"
import { getCoverUrls, getLowResCoverUrl } from "@/lib/helpers/helper.covers"
import { displayCreators } from "@/lib/helpers/helper.creators"
import { getIsbnsFromManifestation } from "@/lib/helpers/ids"
Expand Down Expand Up @@ -103,8 +106,10 @@ const WorkPageHeader = ({ work }: WorkPageHeaderProps) => {
useEffect(() => {
if (!!searchParams.get("type")) {
setSelectedManifestation(
getManifestationByMaterialType(work, searchParams.get("type") as string) ||
selectedManifestation
getManifestationByMaterialType(
work,
searchParams.get("type") as GeneralMaterialTypeCodeEnum
) || selectedManifestation
)
}
// eslint-disable-next-line react-hooks/exhaustive-deps
Expand Down
12 changes: 9 additions & 3 deletions components/pages/workPageLayout/WorkPageLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { CoverPictureSkeleton } from "@/components/shared/coverPicture/CoverPict
import InfoBox from "@/components/shared/infoBox/InfoBox"
import InfoBoxDetails from "@/components/shared/infoBox/InfoBoxDetails"
import { SlideSelectSkeleton } from "@/components/shared/slideSelect/SlideSelect"
import { GetMaterialQuery, useGetMaterialQuery } from "@/lib/graphql/generated/fbi/graphql"
import {
GeneralMaterialTypeCodeEnum,
GetMaterialQuery,
useGetMaterialQuery,
} from "@/lib/graphql/generated/fbi/graphql"
import { useSelectedManifestationStore } from "@/store/selectedManifestation.store"

type WorkPageLayoutProps = {
Expand Down Expand Up @@ -47,8 +51,10 @@ function WorkPageLayout({ workId, dehydratedQueryData }: WorkPageLayoutProps) {
// Select work manifestation on the initial load - 1. by URL params, 2. by best representation
if (!!searchParams.get("type")) {
setSelectedManifestation(
getManifestationByMaterialType(data.work, searchParams.get("type") as string) ||
getBestRepresentation(data.work)
getManifestationByMaterialType(
data.work,
searchParams.get("type") as GeneralMaterialTypeCodeEnum
) || getBestRepresentation(data.work)
)
} else {
setSelectedManifestation(getBestRepresentation(data.work))
Expand Down
48 changes: 24 additions & 24 deletions components/pages/workPageLayout/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const getManifestationMaterialType = (

export const getManifestationByMaterialType = (
work: WorkFullWorkPageFragment,
materialType: GeneralMaterialTypeCodeEnum[0]
materialType: GeneralMaterialTypeCodeEnum
): ManifestationWorkPageFragment | undefined => {
return work.manifestations.all.find(manifestation =>
manifestation.materialTypes.some(type => type.materialTypeGeneral.display === materialType)
Expand All @@ -41,15 +41,15 @@ export const getBestRepresentation = (
}
// If best representation doesn't exist we choose in the following order:
// 1. Ebook / 2. Audiobook / 3. Book / 4. First in all manifestations (any)
const eBook = getManifestationByMaterialType(work, GeneralMaterialTypeCodeEnum.Ebooks)
const eBook = getManifestationByMaterialType(work, "EBOOKS")
if (eBook) {
return eBook
}
const audioBook = getManifestationByMaterialType(work, GeneralMaterialTypeCodeEnum.AudioBooks)
const audioBook = getManifestationByMaterialType(work, "AUDIO_BOOKS")
if (audioBook) {
return audioBook
}
const book = getManifestationByMaterialType(work, GeneralMaterialTypeCodeEnum.Books)
const book = getManifestationByMaterialType(work, "BOOKS")
if (book) {
return book
}
Expand All @@ -58,24 +58,24 @@ export const getBestRepresentation = (

const isOfMaterialType = (
manifestation: ManifestationWorkPageFragment,
materialType: GeneralMaterialTypeCodeEnum[0]
materialType: GeneralMaterialTypeCodeEnum
) => {
return manifestation.materialTypes.some(type => type.materialTypeGeneral.code === materialType)
}

export const isEbook = (manifestation: ManifestationWorkPageFragment | undefined | null) => {
if (!manifestation) return false
return isOfMaterialType(manifestation, GeneralMaterialTypeCodeEnum.Ebooks)
return isOfMaterialType(manifestation, "EBOOKS")
}

export const isAudioBook = (manifestation: ManifestationWorkPageFragment | undefined | null) => {
if (!manifestation) return false
return isOfMaterialType(manifestation, GeneralMaterialTypeCodeEnum.AudioBooks)
return isOfMaterialType(manifestation, "AUDIO_BOOKS")
}

export const isPodcast = (manifestation: ManifestationWorkPageFragment | undefined | null) => {
if (!manifestation) return false
return isOfMaterialType(manifestation, GeneralMaterialTypeCodeEnum.Podcasts)
return isOfMaterialType(manifestation, "PODCASTS")
}

export const getManifestationLanguageIsoCode = (
Expand All @@ -96,22 +96,22 @@ export const getManifestationLanguageIsoCode = (
return undefined
}

export const materialTypeTranslations = {
[GeneralMaterialTypeCodeEnum.Articles]: "Artikel",
[GeneralMaterialTypeCodeEnum.Books]: "Bog",
[GeneralMaterialTypeCodeEnum.Comics]: "Tegneserie",
[GeneralMaterialTypeCodeEnum.Ebooks]: "E-bog",
[GeneralMaterialTypeCodeEnum.ImageMaterials]: "Billedmateriale",
[GeneralMaterialTypeCodeEnum.NewspaperJournals]: "Avis",
[GeneralMaterialTypeCodeEnum.AudioBooks]: "Lydbog",
[GeneralMaterialTypeCodeEnum.Music]: "Musik",
[GeneralMaterialTypeCodeEnum.Podcasts]: "Podcast",
[GeneralMaterialTypeCodeEnum.SheetMusic]: "Noder",
[GeneralMaterialTypeCodeEnum.BoardGames]: "Brætspil",
[GeneralMaterialTypeCodeEnum.ComputerGames]: "Computerspil",
[GeneralMaterialTypeCodeEnum.Films]: "Film",
[GeneralMaterialTypeCodeEnum.TvSeries]: "Tv-serie",
[GeneralMaterialTypeCodeEnum.Other]: "Andet",
export const materialTypeTranslations: { [key in GeneralMaterialTypeCodeEnum]: string } = {
ARTICLES: "Artikel",
BOOKS: "Bog",
COMICS: "Tegneserie",
EBOOKS: "E-bog",
IMAGE_MATERIALS: "Billedmateriale",
NEWSPAPER_JOURNALS: "Avis",
AUDIO_BOOKS: "Lydbog",
MUSIC: "Musik",
PODCASTS: "Podcast",
SHEET_MUSIC: "Noder",
BOARD_GAMES: "Brætspil",
COMPUTER_GAMES: "Computerspil",
FILMS: "Film",
TV_SERIES: "Tv-serie",
OTHER: "Andet",
}

export const translateMaterialTypesForRender = (option: SlideSelectOption): SlideSelectOption => {
Expand Down
29 changes: 12 additions & 17 deletions components/shared/workCard/helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,18 @@ import {
WorkTeaserSearchPageFragment,
} from "@/lib/graphql/generated/fbi/graphql"

export const materialTypeCategories = {
reading: [
GeneralMaterialTypeCodeEnum.Articles,
GeneralMaterialTypeCodeEnum.Books,
GeneralMaterialTypeCodeEnum.Comics,
GeneralMaterialTypeCodeEnum.Ebooks,
GeneralMaterialTypeCodeEnum.ImageMaterials,
GeneralMaterialTypeCodeEnum.NewspaperJournals,
],
listening: [
GeneralMaterialTypeCodeEnum.AudioBooks,
GeneralMaterialTypeCodeEnum.Music,
GeneralMaterialTypeCodeEnum.Podcasts,
GeneralMaterialTypeCodeEnum.SheetMusic,
],
gaming: [GeneralMaterialTypeCodeEnum.BoardGames, GeneralMaterialTypeCodeEnum.ComputerGames],
video: [GeneralMaterialTypeCodeEnum.Films, GeneralMaterialTypeCodeEnum.TvSeries],
type MaterialTypeCategories = {
reading: GeneralMaterialTypeCodeEnum[]
listening: GeneralMaterialTypeCodeEnum[]
gaming: GeneralMaterialTypeCodeEnum[]
video: GeneralMaterialTypeCodeEnum[]
}

export const materialTypeCategories: MaterialTypeCategories = {
reading: ["ARTICLES", "BOOKS", "COMICS", "EBOOKS", "IMAGE_MATERIALS", "NEWSPAPER_JOURNALS"],
listening: ["AUDIO_BOOKS", "MUSIC", "PODCASTS", "SHEET_MUSIC"],
gaming: ["BOARD_GAMES", "COMPUTER_GAMES"],
video: ["FILMS", "TV_SERIES"],
}

export const isOfWorkTypeCategory = (
Expand Down
3 changes: 1 addition & 2 deletions lib/helpers/ids.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { flatten } from "lodash"
import { filterFalsyValuesFromArray } from "@/lib/helpers/helper.arrays"

import {
IdentifierTypeEnum,
ManifestationIdentifiersFragment,
ManifestationWorkPageFragment,
WorkTeaserSearchPageFragment,
Expand All @@ -15,7 +14,7 @@ export const getIsbnsFromManifestation = (
if (!manifestation) return []

return manifestation.identifiers.reduce((acc, identifier) => {
if (identifier.type === IdentifierTypeEnum.Isbn) {
if (identifier.type === "ISBN") {
acc.push(identifier.value)
}
return acc
Expand Down

0 comments on commit 5adc558

Please sign in to comment.