Skip to content

Commit

Permalink
Merge pull request #196 from vtex-apps/fix/category-selection
Browse files Browse the repository at this point in the history
append all categories filters to operation list
  • Loading branch information
jgfidelis authored Jun 24, 2019
2 parents 512a105 + 1c4c613 commit 443b757
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

## [Unreleased]

### Fixed

- Issues with selecting categories in mobile.

## [3.22.1] - 2019-06-21

### Changed
Expand Down
22 changes: 13 additions & 9 deletions react/components/FilterSidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ const FilterSidebar = ({ filters, tree }) => {
const [open, setOpen] = useState(false)

const [filterOperations, setFilterOperations] = useState([])
const [categoryTreeOperations, setCategoryTreeOperations] = useState([])
// eslint-disable-next-line @typescript-eslint/no-use-before-define
const currentTree = useCategoryTree(tree, filterOperations)
const currentTree = useCategoryTree(tree, categoryTreeOperations)

const navigateToFacet = useFacetNavigation()

Expand Down Expand Up @@ -61,10 +62,17 @@ const FilterSidebar = ({ filters, tree }) => {
? maybeCategories
: [maybeCategories]

const categoriesSelected = filterOperations.filter(op => op.map === 'c')
const newCategories = [...categoriesSelected, ...categories]

// Just save the newest operation here to be recorded at the category tree hook and update the tree
setCategoryTreeOperations(categories)

// Save all filters along with the new categories, appended to the old ones
setFilterOperations(filters => {
return filters
.filter(operations => operations.map !== 'c')
.concat(categories)
.concat(newCategories)
})
}

Expand Down Expand Up @@ -183,20 +191,16 @@ const updateTree = categories =>
// in a user-friendly manner should reflect to the changes
// we make in the tree, the same as it would with a tree fetched
// from the API.
const useCategoryTree = (initialTree, filterOperations) => {
const useCategoryTree = (initialTree, categoryTreeOperations) => {
const [tree, setTree] = useState(initialTree)

useEffect(() => {
setTree(initialTree)
}, [initialTree])

useEffect(() => {
const categoryOperations = filterOperations.filter(
operation => operation.map === 'c'
)

setTree(updateTree(categoryOperations))
}, [filterOperations, initialTree])
setTree(updateTree(categoryTreeOperations))
}, [categoryTreeOperations, initialTree])

return tree
}
Expand Down

0 comments on commit 443b757

Please sign in to comment.