From b5ad281c7963fe40e743b5a2eb71f6149a205352 Mon Sep 17 00:00:00 2001 From: thomasgross Date: Fri, 6 Dec 2024 12:54:31 +0100 Subject: [PATCH 1/3] fix: bug that returns an empty search result when navigation with native browser navigation buttons --- components/pages/searchPageLayout/SearchPageLayout.tsx | 4 ++++ lib/machines/search/search.machine.ts | 3 +++ 2 files changed, 7 insertions(+) diff --git a/components/pages/searchPageLayout/SearchPageLayout.tsx b/components/pages/searchPageLayout/SearchPageLayout.tsx index 850f8aca..1795e8a3 100644 --- a/components/pages/searchPageLayout/SearchPageLayout.tsx +++ b/components/pages/searchPageLayout/SearchPageLayout.tsx @@ -34,6 +34,10 @@ const SearchPageLayout = () => { // eslint-disable-next-line react-hooks/exhaustive-deps }, [isInView]) + useEffect(() => { + actor.send({ type: "RESET_BOOTSTRAP_STATE" }) + }, [actor]) + const isNoSearchResult = !isLoadingResults && (!data.search || !data.search.pages[0].length) const hitCountText = data.search?.hitcount ? `(${data.search.hitcount})` : "" const searchQueryText = searchQuery ? `"${searchQuery}"` : "" diff --git a/lib/machines/search/search.machine.ts b/lib/machines/search/search.machine.ts index 5bc60e42..4385c091 100644 --- a/lib/machines/search/search.machine.ts +++ b/lib/machines/search/search.machine.ts @@ -73,6 +73,9 @@ export default searchMachineSetup.createMachine({ actions: ["setLoadMoreValuesInContext"], target: "loadingMoreSearchResults", }, + RESET_BOOTSTRAP_STATE: { + target: "bootstrap", + }, }, }, filteringAndSearching: { From b06b16515527f96ef85b557406e6c831474ff2f8 Mon Sep 17 00:00:00 2001 From: thomasgross Date: Fri, 6 Dec 2024 13:25:39 +0100 Subject: [PATCH 2/3] fix: reset offset when making new search and filtering --- lib/machines/search/search.machine.setup.ts | 3 +++ lib/machines/search/search.machine.ts | 21 ++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/lib/machines/search/search.machine.setup.ts b/lib/machines/search/search.machine.setup.ts index 31bff0d6..f8b42104 100644 --- a/lib/machines/search/search.machine.setup.ts +++ b/lib/machines/search/search.machine.setup.ts @@ -57,6 +57,9 @@ export default setup({ resetSearchData: assign(() => ({ searchData: undefined, })), + resetOffset: assign(() => ({ + searchOffset: 0, + })), setFacetDataInContext: assign({ facetData: ({ event: { diff --git a/lib/machines/search/search.machine.ts b/lib/machines/search/search.machine.ts index 4385c091..18cb43ff 100644 --- a/lib/machines/search/search.machine.ts +++ b/lib/machines/search/search.machine.ts @@ -45,7 +45,12 @@ export default searchMachineSetup.createMachine({ TOGGLE_FILTER: [ { guard: "contextHasSearchString", - actions: ["resetSearchData", "toggleFilterInContext", "emitFilterToggled"], + actions: [ + "resetSearchData", + "resetOffset", + "toggleFilterInContext", + "emitFilterToggled", + ], target: "filteringAndSearching", }, { @@ -60,7 +65,12 @@ export default searchMachineSetup.createMachine({ SEARCH: [ { guard: "contextHasSearchString", - actions: ["resetSearchData", "resetFilters", "setSubmittedQueryInContext"], + actions: [ + "resetSearchData", + "resetFilters", + "setSubmittedQueryInContext", + "resetOffset", + ], target: "filteringAndSearching", }, { @@ -90,7 +100,12 @@ export default searchMachineSetup.createMachine({ SEARCH: [ { guard: "contextHasSearchString", - actions: ["resetSearchData", "resetFilters", "setSubmittedQueryInContext"], + actions: [ + "resetSearchData", + "resetFilters", + "resetOffset", + "setSubmittedQueryInContext", + ], target: "filteringAndSearching", }, { From ecc014fb694265778a7a0418cd47e9a4be9bfd37 Mon Sep 17 00:00:00 2001 From: thomasgross Date: Fri, 6 Dec 2024 13:40:38 +0100 Subject: [PATCH 3/3] feat: add initial context as an globally accessible reference --- lib/machines/search/search.machine.setup.ts | 7 ++--- lib/machines/search/search.machine.ts | 30 ++++++++++++++------- 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/lib/machines/search/search.machine.setup.ts b/lib/machines/search/search.machine.setup.ts index f8b42104..85bee67b 100644 --- a/lib/machines/search/search.machine.setup.ts +++ b/lib/machines/search/search.machine.setup.ts @@ -2,6 +2,7 @@ import { assign, emit, setup } from "xstate" import { correctFacetNames } from "./helpers" import { getFacets, performSearch } from "./queries" +import { initialContext } from "./search.machine" import { TContext, TFilters, TInput } from "./types" export default setup({ @@ -52,13 +53,13 @@ export default setup({ submittedQuery: ({ context }) => (context.submittedQuery = context.currentQuery), }), resetFilters: assign(() => ({ - selectedFilters: {}, + selectedFilters: initialContext.selectedFilters, })), resetSearchData: assign(() => ({ - searchData: undefined, + searchData: initialContext.searchData, })), resetOffset: assign(() => ({ - searchOffset: 0, + searchOffset: initialContext.searchOffset, })), setFacetDataInContext: assign({ facetData: ({ diff --git a/lib/machines/search/search.machine.ts b/lib/machines/search/search.machine.ts index 18cb43ff..a9a7bbb1 100644 --- a/lib/machines/search/search.machine.ts +++ b/lib/machines/search/search.machine.ts @@ -2,20 +2,32 @@ import { and, not } from "xstate" import searchMachineSetup from "./search.machine.setup" +export const initialContext = { + searchOffset: 0, + searchPageSize: 0, + facetLimit: 0, + currentQuery: "", + submittedQuery: undefined, + searchData: undefined, + facetData: undefined, + selectedFilters: {}, + queryClient: null, +} + export default searchMachineSetup.createMachine({ /** @xstate-layout N4IgpgJg5mDOIC5SzAQwE4GMAWA6ARgPaEAusJ6qADgMQDKAogCoD6AigKoMBKAmiwGEAMgEkGAOSYBtAAwBdRKCqFYASxKrCAO0UgAHogC0ANgCMAVlwAWAJzGATDIAcNmeYDM9zwBoQAT0QLAHZcYycnIKdTSNcndysAXwTfFAwcAmIyCmp6ZhZGAEFuAQAJfKZuEXEAcVkFJBBlNQ1tXQMEQytjS0cbKyCg0xdwpytzXwCEd2jcGxt3d1Gne273AaSUtCw8IlJySlpGViqRJhECoRYAMREhJh46Ot0m9U0dBvaTR1Cw0ysFpzmLxuCaIKxDXCmdw2cwyFamYyLKIbECpbYZPbZWgAIQA8rimHQKgUAAosAAiuPEDCeDReLXeoE+5iiuCC0Js9isK36phhoIQphkNlC4Ui0RczniphRaPSuyyBxoeIJRO4pIpVJppnqShUr1aHyMLNMbI5XJ5g35-kQZlFESiMSl4NlW3SqggABswDQmLjqt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0tZYKyDhA0+MDz6b23jfKA0dSb30foJamL8XqtSoS9WCrYPBWDGApLw7hxKLRAmzaY4I853i0IQCAcBdDISfsFEShhASsIiK2YcX8rTM0mJ0ECWZtr-G5BYNYcQsoKn2NQERScjBODcFREc7IryyO2utNY4kGJZ0YdzXOMIDrT2Yt6PRs1QqZnCMmScYwrxxDCOBGQdMNEMQsK4KwwonH3mBp3bGq8bJuObOyEIwIcw9jtg7Lwfw0rzEBFEpusSW6C2EeQgeIlIiWA8AOZMGcWZCgWnCBMY5ohf2gu4F2g0i7xM1u5EWiTTYLCzKEmp8jECjmsN9J2U9okF0KQLXGQc4ZgH6SFF6n1aLTBaT2cU6ZoKhCBPEJmHSsZFIWXzFZ5TATWM-hEUZCBCJDivOlREUjui9RmekZu8yPYR2bhc9o+ZrlpLucmEUw4hjO3zp8uZ7s26e1drgJZ-zAgySzMMII3IryczWizf6pozCT3AXPSBUBoGwOLPA7eyKOi2BFFCH+QEezxCYXUv4lgMUOCzg4IYYQiUnygWfOB+Dr59NKc-EKnQByQgYvMRlDCWWTBeoM9KwFslcIsEkJIQA */ id: "search", initial: "bootstrap", context: ({ input }) => ({ - searchOffset: input.initialOffset ?? 0, - searchPageSize: input.searchPageSize, - facetLimit: input.facetLimit, - currentQuery: input.q ?? "", - submittedQuery: undefined, - searchData: undefined, - facetData: undefined, - selectedFilters: input.filters ?? {}, - queryClient: input.queryClient ?? null, + searchOffset: input.initialOffset ?? initialContext.searchOffset, + searchPageSize: input.searchPageSize ?? initialContext.searchPageSize, + facetLimit: input.facetLimit ?? initialContext.facetLimit, + currentQuery: input.q ?? initialContext.currentQuery, + submittedQuery: initialContext.submittedQuery, + searchData: initialContext.searchData, + facetData: initialContext.facetData, + selectedFilters: input.filters ?? initialContext.selectedFilters, + queryClient: input.queryClient ?? initialContext.queryClient, }), states: { bootstrap: {