Skip to content

Commit

Permalink
adjust onFilter method to call when change the checked value
Browse files Browse the repository at this point in the history
  • Loading branch information
TiagoBehenck committed May 28, 2022
1 parent fe14a39 commit 2b517fa
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
12 changes: 8 additions & 4 deletions src/components/ExploreSidebar/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useState, useEffect } from 'react'
import { ParsedUrlQueryInput } from 'node:querystring'
import xor from 'lodash.xor'

Expand Down Expand Up @@ -38,6 +38,11 @@ const ExploreSidebar = ({
const [values, setValues] = useState(initialValues)
const [isOpen, setIsOpen] = useState(false)

useEffect(() => {
onFilter(values)
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [values])

const handleCheckbox = (name: string, value: string | boolean) => {
const currentList = (values[name] as []) || []
setValues((s) => ({ ...s, [name]: xor(currentList, [value]) }))
Expand All @@ -47,8 +52,7 @@ const ExploreSidebar = ({
setValues((s) => ({ ...s, [name]: value }))
}

const handleFilter = () => {
onFilter(values)
const handleFilterMenu = () => {
setIsOpen(false)
}

Expand Down Expand Up @@ -101,7 +105,7 @@ const ExploreSidebar = ({
</S.Content>

<S.Footer>
<Button fullWidth size="medium" onClick={handleFilter}>
<Button fullWidth size="medium" onClick={handleFilterMenu}>
Filter
</Button>
</S.Footer>
Expand Down
3 changes: 2 additions & 1 deletion src/components/ExploreSidebar/stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export default {
title: 'ExploreSidebar',
component: ExploreSidebar,
args: {
items
items,
onFilter: () => ({})
},
parameters: {
backgrounds: {
Expand Down
10 changes: 7 additions & 3 deletions src/components/ExploreSidebar/test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ describe('<ExploreSidebar />', () => {
/>
)

userEvent.click(screen.getByRole('button', { name: /filter/i }))
expect(onFilter).toBeCalledWith({
plataforms: ['windows'],
sort_by: 'high-to-low'
Expand All @@ -86,7 +85,7 @@ describe('<ExploreSidebar />', () => {
userEvent.click(screen.getByLabelText(/linux/i))
userEvent.click(screen.getByLabelText(/low to high/i))

userEvent.click(screen.getByRole('button', { name: /filter/i }))
expect(onFilter).toHaveBeenCalledTimes(4)
expect(onFilter).toBeCalledWith({
plataforms: ['windows', 'linux'],
sort_by: 'low-to-high'
Expand All @@ -101,7 +100,6 @@ describe('<ExploreSidebar />', () => {
userEvent.click(screen.getByLabelText(/low to high/i))
userEvent.click(screen.getByLabelText(/high to low/i))

userEvent.click(screen.getByRole('button', { name: /filter/i }))
expect(onFilter).toBeCalledWith({ sort_by: 'high-to-low' })
})

Expand All @@ -128,5 +126,11 @@ describe('<ExploreSidebar />', () => {
userEvent.click(screen.getByLabelText(/close filters/))

expect(Element).not.toHaveStyleRule('opacity', '1', variant)

userEvent.click(screen.getByLabelText(/open filters/))

userEvent.click(screen.getByRole('button', { name: /filter/i }))

expect(Element).not.toHaveStyleRule('opacity', '1', variant)
})
})

0 comments on commit 2b517fa

Please sign in to comment.