-
- {`Viser resultater for "${q}" ${hitcount ? "(" + hitcount + ")" : ""}`}
-
- {q ? (
+ {searchQuery && (
+
+ {`Viser resultater for ${searchQueryText} ${hitCountText}`}
+
+ )}
+ {searchQuery ? (
<>
- {!isLoadingFacets && facetData && facetData?.length > 0 ? (
+ {!isLoadingFacets && data.facets && data.facets.length > 0 ? (
) : (
@@ -144,28 +60,34 @@ const SearchPageLayout = ({ searchQuery }: { searchQuery?: string }) => {
)}
- {data?.pages.map(
- (page, i) =>
- page.search.works && (
-
-
-
- )
- )}
- {isLoading &&
}
+ {isNoSearchResult &&
Ingen søgeresultat
}
+ {data.search &&
+ data.search.pages.map(
+ (works, i) =>
+ works && (
+
+
+
+ )
+ )}
+ {isLoadingResults &&
}
-
>
) : (
-
+ <>
+ {machineIsReady && (
+
+ )}
+ >
)}
+
)
}
diff --git a/components/pages/searchPageLayout/SearchResults.tsx b/components/pages/searchPageLayout/SearchResults.tsx
index 08b23d0f..15ed1b16 100644
--- a/components/pages/searchPageLayout/SearchResults.tsx
+++ b/components/pages/searchPageLayout/SearchResults.tsx
@@ -3,10 +3,10 @@
import React from "react"
import WorkCard, { WorkCardGhost } from "@/components/shared/workCard/WorkCard"
-import { SearchWithPaginationQuery } from "@/lib/graphql/generated/fbi/graphql"
+import { WorkTeaserFragment } from "@/lib/graphql/generated/fbi/graphql"
type SearchResultProps = {
- works: SearchWithPaginationQuery["search"]["works"]
+ works: WorkTeaserFragment[]
}
const SearchResults = ({ works }: SearchResultProps) => {
diff --git a/components/pages/searchPageLayout/helper.ts b/components/pages/searchPageLayout/helper.ts
index ad042ae9..ca9a4937 100644
--- a/components/pages/searchPageLayout/helper.ts
+++ b/components/pages/searchPageLayout/helper.ts
@@ -1,10 +1,13 @@
import { GetNextPageParamFunction } from "@tanstack/react-query"
+import { useSelector } from "@xstate/react"
import { ReadonlyURLSearchParams } from "next/navigation"
import { getFacetMachineNames } from "@/components/shared/searchFilters/helper"
import goConfig from "@/lib/config/config"
import { TConfigSearchFacets } from "@/lib/config/resolvers/search"
import { SearchFiltersInput, SearchWithPaginationQuery } from "@/lib/graphql/generated/fbi/graphql"
+import { TFilters } from "@/lib/machines/search/types"
+import useSearchMachineActor from "@/lib/machines/search/useSearchMachineActor"
export const getSearchQueryArguments = ({
q,
@@ -32,7 +35,7 @@ export const getFacetsForSearchRequest = (searchParams: ReadonlyURLSearchParams)
const facetsMachineNames = getFacetMachineNames()
return facetsMachineNames.reduce(
- (acc: SearchFiltersInput, machineName) => {
+ (acc: TFilters, machineName) => {
const values = searchParams.getAll(facets[machineName].filter)
if (values.length > 0) {
return {
@@ -42,7 +45,7 @@ export const getFacetsForSearchRequest = (searchParams: ReadonlyURLSearchParams)
}
return acc
},
- {} as { [key: string]: keyof SearchFiltersInput[] }
+ {} as { [key: string]: keyof TFilters[] }
)
}
@@ -57,3 +60,30 @@ export const getNextPageParamsFunc = (
return currentPage < totalPages ? nextPage : undefined // By returning undefined if there are no more pages, hasNextPage boolean will be set to false
}
}
+
+export const useSearchDataAndLoadingStates = () => {
+ const actor = useSearchMachineActor()
+ const searchQuery = useSelector(actor, snapshot => {
+ return snapshot.context.submittedQuery
+ })
+ const data = useSelector(actor, snapshot => {
+ const { facetData: facets, searchData: search } = snapshot.context
+ return { facets, search }
+ })
+ const isLoadingFacets =
+ !data.facets || actor.getSnapshot().matches({ filteringAndSearching: "filter" })
+ const isLoadingResults =
+ !data.search || actor.getSnapshot().matches({ filteringAndSearching: "search" })
+ const machineIsReady = !actor.getSnapshot().matches("bootstrap")
+
+ const selectedFilters = useSelector(actor, snapshot => snapshot.context.selectedFilters)
+
+ return {
+ searchQuery,
+ data,
+ selectedFilters,
+ isLoadingFacets,
+ isLoadingResults,
+ machineIsReady,
+ }
+}
diff --git a/components/shared/searchFilters/SearchFiltersColumn.tsx b/components/shared/searchFilters/SearchFiltersColumn.tsx
index 325f5eeb..0f152174 100644
--- a/components/shared/searchFilters/SearchFiltersColumn.tsx
+++ b/components/shared/searchFilters/SearchFiltersColumn.tsx
@@ -1,15 +1,17 @@
-import { useRouter, useSearchParams } from "next/navigation"
import React, { useEffect, useRef, useState } from "react"
+import { useSearchDataAndLoadingStates } from "@/components/pages/searchPageLayout/helper"
import BadgeButton from "@/components/shared/badge/BadgeButton"
import Icon from "@/components/shared/icon/Icon"
import {
+ facetTermIsSelected,
getFacetTranslation,
sortByActiveFacets,
- toggleFilter,
} from "@/components/shared/searchFilters/helper"
-import { SearchFacetFragment, SearchFiltersInput } from "@/lib/graphql/generated/fbi/graphql"
+import { SearchFacetFragment } from "@/lib/graphql/generated/fbi/graphql"
import { cn } from "@/lib/helpers/helper.cn"
+import { TFilters } from "@/lib/machines/search/types"
+import useSearchMachineActor from "@/lib/machines/search/useSearchMachineActor"
import { AnimateChangeInHeight } from "../animateChangeInHeight/AnimateChangeInHeight"
@@ -26,12 +28,11 @@ const SearchFiltersColumn = ({
isExpanded,
setIsExpanded,
}: SearchFiltersColumnProps) => {
- const router = useRouter()
- const facetFilter = facet.name as keyof SearchFiltersInput
-
- const searchParams = useSearchParams()
+ const actor = useSearchMachineActor()
+ const facetFilter = facet.name as keyof TFilters
const elementRef = useRef