Skip to content

Commit

Permalink
fix: fix bug preventing search geometry from being cached between ses…
Browse files Browse the repository at this point in the history
…sions
  • Loading branch information
stdavis committed Sep 8, 2023
1 parent 0eb1162 commit 55b61dd
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/SearchMachineProvider.jsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { fromJSON } from '@arcgis/core/geometry';
import { useMachine } from '@xstate/react';
import localforage from 'localforage';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -76,8 +77,13 @@ const machine = createMachine(
invoke: {
src: async () => {
const cachedContext = JSON.parse(
await localforage.getItem(CACHE_KEY)
await localforage.getItem(CACHE_KEY),
);
if (cachedContext?.filter?.geometry) {
cachedContext.filter.geometry = fromJSON(
cachedContext.filter.geometry,
);
}

return cachedContext || {};
},
Expand Down Expand Up @@ -172,7 +178,7 @@ const machine = createMachine(
selectedDownloadLayers: (context) =>
context.resultLayers
.filter(
(result) => result.supportsExport && result.features.length > 0
(result) => result.supportsExport && result.features.length > 0,
)
.map((result) => result[fieldNames.queryLayers.uniqueId]),
}),
Expand Down Expand Up @@ -293,15 +299,15 @@ const machine = createMachine(
const newData = context.searchLayers.filter(
(config) =>
config[fieldNames.queryLayers.uniqueId] !==
event.queryLayer[fieldNames.queryLayers.uniqueId]
event.queryLayer[fieldNames.queryLayers.uniqueId],
);
cacheSearchContext({ searchLayers: newData, filter: context.filter });

return newData;
},
}),
},
}
},
);

// exported for mocking in Storybook
Expand All @@ -328,7 +334,7 @@ export function useSearchMachine() {

if (context === undefined) {
throw new Error(
'useSearchMachine must be used within a SearchMachineProvider'
'useSearchMachine must be used within a SearchMachineProvider',
);
}

Expand Down

0 comments on commit 55b61dd

Please sign in to comment.