forked from decentraland/marketplace
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add distance to plaza and adjacent to road filter (decentraland…
- Loading branch information
Melisa Anabella Rossi
authored
Mar 1, 2023
1 parent
f794d37
commit d279ae5
Showing
26 changed files
with
1,144 additions
and
882 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
webapp/src/components/AssetFilters/LocationFilter/LocationFilter.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
.checkboxFilter { | ||
margin-bottom: 25px; | ||
} | ||
|
||
.slider { | ||
margin-top: 20px; | ||
} | ||
|
||
.slider p { | ||
color: var(--secondary-text); | ||
} | ||
|
||
.locationContainer { | ||
display: flex; | ||
flex-direction: column; | ||
} |
57 changes: 57 additions & 0 deletions
57
webapp/src/components/AssetFilters/LocationFilter/LocationFilter.spec.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { render, waitFor } from '@testing-library/react' | ||
import userEvent from '@testing-library/user-event' | ||
import { t } from 'decentraland-dapps/dist/modules/translation/utils' | ||
import { LocationFilter, LocationFilterProps } from './LocationFilter' | ||
|
||
function renderLocationFilter(props: Partial<LocationFilterProps> = {}) { | ||
return render( | ||
<LocationFilter | ||
adjacentToRoad={false} | ||
onAdjacentToRoadChange={jest.fn()} | ||
onDistanceToPlazaChange={jest.fn()} | ||
{...props} | ||
/> | ||
) | ||
} | ||
describe('LocationFilter', () => { | ||
test('should render adjacent to road toggle', () => { | ||
const { getByRole } = renderLocationFilter() | ||
expect( | ||
getByRole('checkbox', { name: t('nft_filters.adjacent_to_road') }) | ||
).toBeInTheDocument() | ||
}) | ||
|
||
test('should render near a plaza toggle', () => { | ||
const { getByRole } = renderLocationFilter() | ||
expect( | ||
getByRole('checkbox', { name: t('nft_filters.distance_to_plaza.title') }) | ||
).toBeInTheDocument() | ||
}) | ||
|
||
test('should call onAdjacentToRoadChange callback when toggle changes', async () => { | ||
const onAdjacentToRoadChangeMock = jest.fn() | ||
const { getByTestId } = renderLocationFilter({ | ||
onAdjacentToRoadChange: onAdjacentToRoadChangeMock | ||
}) | ||
await userEvent.click(getByTestId('adjacent-to-road-toggle')) | ||
expect(onAdjacentToRoadChangeMock).toHaveBeenCalledWith(true) | ||
}) | ||
|
||
test('should call oDistanceToPlazaChange callback when toggle changes', async () => { | ||
const onDistanceToPlazaChangeMock = jest.fn() | ||
const { getByTestId } = renderLocationFilter({ | ||
onDistanceToPlazaChange: onDistanceToPlazaChangeMock | ||
}) | ||
await userEvent.click(getByTestId('near-to-plaza-toggle')) | ||
expect(onDistanceToPlazaChangeMock).toHaveBeenCalledWith(['2', '10']) | ||
}) | ||
|
||
test('should show distance slider when minDistanceToPlaza and maxDistanceToPlaza are defined', () => { | ||
const { getByRole } = renderLocationFilter({ | ||
minDistanceToPlaza: '3', | ||
maxDistanceToPlaza: '5' | ||
}) | ||
expect(getByRole('slider', { name: 'min value' })).toBeInTheDocument() | ||
expect(getByRole('slider', { name: 'max value' })).toBeInTheDocument() | ||
}) | ||
}) |
143 changes: 143 additions & 0 deletions
143
webapp/src/components/AssetFilters/LocationFilter/LocationFilter.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { useCallback, useMemo } from 'react' | ||
import { | ||
Box, | ||
CheckboxProps, | ||
Checkbox, | ||
useTabletAndBelowMediaQuery, | ||
SliderField | ||
} from 'decentraland-ui' | ||
import { t } from 'decentraland-dapps/dist/modules/translation/utils' | ||
import styles from './LocationFilter.module.css' | ||
import classNames from 'classnames' | ||
|
||
const DISTANCE_TO_PLAZA_MIN = 2; | ||
const DISTANCE_TO_PLAZA_MAX = 10; | ||
|
||
export type LocationFilterProps = { | ||
adjacentToRoad?: boolean | ||
minDistanceToPlaza?: string | ||
maxDistanceToPlaza?: string | ||
defaultCollapsed?: boolean | ||
onAdjacentToRoadChange: (value?: boolean) => void | ||
onDistanceToPlazaChange: (value: [string, string]) => void | ||
} | ||
|
||
export const LocationFilter = ({ | ||
adjacentToRoad, | ||
minDistanceToPlaza, | ||
maxDistanceToPlaza, | ||
defaultCollapsed = false, | ||
onAdjacentToRoadChange, | ||
onDistanceToPlazaChange | ||
}: LocationFilterProps): JSX.Element => { | ||
const isMobileOrTablet = useTabletAndBelowMediaQuery() | ||
|
||
const handleAdjacentToRoadChange = useCallback( | ||
(_evt, props: CheckboxProps) => { | ||
onAdjacentToRoadChange(!!props.checked || undefined) | ||
}, | ||
[onAdjacentToRoadChange] | ||
) | ||
|
||
const handleDistanceToPlazaChange = useCallback( | ||
(_evt, data) => { | ||
onDistanceToPlazaChange(data) | ||
}, | ||
[onDistanceToPlazaChange] | ||
) | ||
|
||
const handleToggleDistanceFilter = useCallback( | ||
(_evt, props: CheckboxProps) => { | ||
if (!!props.checked) { | ||
onDistanceToPlazaChange([DISTANCE_TO_PLAZA_MIN.toString(), DISTANCE_TO_PLAZA_MAX.toString()]) | ||
} else { | ||
onDistanceToPlazaChange(['', '']) | ||
} | ||
}, | ||
[onDistanceToPlazaChange] | ||
) | ||
|
||
const locationSelectionText = useMemo(() => { | ||
if (!adjacentToRoad && !minDistanceToPlaza && !maxDistanceToPlaza) { | ||
return t('nft_filters.all_locations') | ||
} | ||
|
||
let locationTexts = [] | ||
if (adjacentToRoad) { | ||
locationTexts.push(t('nft_filters.adjacent_to_road')) | ||
} | ||
|
||
if (minDistanceToPlaza || maxDistanceToPlaza) { | ||
locationTexts.push( | ||
t('nft_filters.distance_to_plaza.title') | ||
) | ||
} | ||
|
||
return locationTexts.join(', ') | ||
}, [minDistanceToPlaza, maxDistanceToPlaza, adjacentToRoad]) | ||
|
||
const header = useMemo( | ||
() => | ||
isMobileOrTablet ? ( | ||
<div className="mobile-box-header"> | ||
<span className="box-filter-name">{t('nft_filters.location')}</span> | ||
<span className="box-filter-value">{locationSelectionText}</span> | ||
</div> | ||
) : ( | ||
t('nft_filters.location') | ||
), | ||
[isMobileOrTablet, locationSelectionText] | ||
) | ||
|
||
const distanceFilterIsOn = !!(minDistanceToPlaza || maxDistanceToPlaza) | ||
|
||
return ( | ||
<Box | ||
header={header} | ||
className={classNames("filters-sidebar-box", styles.locationContainer)} | ||
collapsible | ||
defaultCollapsed={defaultCollapsed || isMobileOrTablet} | ||
> | ||
<Checkbox | ||
data-testid="adjacent-to-road-toggle" | ||
id="adjacent-to-road-toggle" | ||
label={t('nft_filters.adjacent_to_road')} | ||
toggle | ||
checked={adjacentToRoad} | ||
onChange={handleAdjacentToRoadChange} | ||
className={styles.checkboxFilter} | ||
/> | ||
<Checkbox | ||
data-testid="near-to-plaza-toggle" | ||
id="near-to-plaza-toggle" | ||
label={t('nft_filters.distance_to_plaza.title')} | ||
toggle | ||
checked={distanceFilterIsOn} | ||
onChange={handleToggleDistanceFilter} | ||
/> | ||
{distanceFilterIsOn && ( | ||
<SliderField | ||
data-testid="dkstance-to-plaza-slider" | ||
min={DISTANCE_TO_PLAZA_MIN} | ||
max={DISTANCE_TO_PLAZA_MAX} | ||
onChange={handleDistanceToPlazaChange} | ||
step={1} | ||
valueFrom={ | ||
minDistanceToPlaza | ||
? Number.parseFloat(minDistanceToPlaza) | ||
: undefined | ||
} | ||
valueTo={ | ||
maxDistanceToPlaza | ||
? Number.parseFloat(maxDistanceToPlaza) | ||
: undefined | ||
} | ||
range | ||
header="" | ||
label={t('nft_filters.distance_to_plaza.subtitle')} | ||
className={styles.slider} | ||
/> | ||
)} | ||
</Box> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './LocationFilter' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.