Skip to content

Commit

Permalink
feat: add initial context as an globally accessible reference
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasGross committed Dec 6, 2024
1 parent b06b165 commit ecc014f
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
7 changes: 4 additions & 3 deletions lib/machines/search/search.machine.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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: ({
Expand Down
30 changes: 21 additions & 9 deletions lib/machines/search/search.machine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 N4IgpgJg5mDOIC5SzAQwE4GMAWA6ARgPaEAusJ6qADgMQDKAogCoD6AigKoMBKAmiwGEAMgEkGAOSYBtAAwBdRKCqFYASxKrCAO0UgAHogC0ANgCMAVlwAWAJzGATDIAcNmeYDM9zwBoQAT0QLAHZcYycnIKdTSNcndysAXwTfFAwcAmIyCmp6ZhZGAEFuAQAJfKZuEXEAcVkFJBBlNQ1tXQMEQytjS0cbKyCg0xdwpytzXwCEd2jcGxt3d1Gne273AaSUtCw8IlJySlpGViqRJ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jc+K3P9kE4WSg4PNA+ifOnaH2Dh17hemTQYsS1LMskHL6CKxj5dq6NYeQ2XrtRyT+sU-IVMmyFZvWHFuf9giykW3nfXop6hCoBANlcRLYCt9g3BwAArgZsBK7DfsB+j7mz-Pi-L6v6+wFvZC70T3ex1o8fXU236GHydNdPFxj9P9CwrRR3zcx2nMMI3MxhZWPgvUyS90ArwfBfK+O8RaV3QOLSW0tZYKyDhA0+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: {
Expand Down

0 comments on commit ecc014f

Please sign in to comment.