Skip to content

Commit

Permalink
only save facet order in local storage if user initiated it (#2548)
Browse files Browse the repository at this point in the history
  • Loading branch information
RFSH authored Sep 27, 2024
1 parent b4bd561 commit 29cb2e1
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/components/faceting/faceting.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ const Faceting = ({
return res;
});

/**
* this boolean indicates whether users made any changes to the facet list or not.
* when this is set to true, we should save the changes in the local storage and then change it back to false.
*/
const [facetListModified, setFacetListModified] = useState(false);

const setFacetModelByIndex = (index: number, updatedVals: { [key: string]: boolean }) => {
setFacetModels((prevFacetModels: FacetModel[]) => {
return prevFacetModels.map((fm: FacetModel, fmIndex: number) => {
Expand Down Expand Up @@ -262,6 +268,7 @@ const Faceting = ({
*/
useEffect(() => {
if (!facetOrders || !facetOrders.length) return;
if (!facetListModified) return;
/**
* store isOpen state for facets to localStorage
*/
Expand All @@ -272,7 +279,10 @@ const Faceting = ({
};
}));

}, [facetModels, facetOrders])
// now that the state is saved, just set it to false so we don't update this until the next user action
setFacetListModified(false);

}, [facetListModified, facetModels, facetOrders])

//------------------- flow-control related functions: --------------------//

Expand Down Expand Up @@ -616,6 +626,9 @@ const Faceting = ({
return { ...fm, isOpen };
});
});

// make sure we're saving the new state
setFacetListModified(true);
};

/**
Expand Down Expand Up @@ -670,8 +683,8 @@ const Faceting = ({
return;
}


setFacetOrders(items);
setFacetListModified(true)
}
//------------------- render logic: --------------------//

Expand Down

0 comments on commit 29cb2e1

Please sign in to comment.