Skip to content

Commit

Permalink
Merge pull request #921 from ONEARMY/901_show-pins-amount-map-filter
Browse files Browse the repository at this point in the history
901 show pins amount map filter
  • Loading branch information
BenGamma authored Mar 28, 2020
2 parents 4fb701c + 0332a0f commit 578367a
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
16 changes: 14 additions & 2 deletions src/pages/Maps/Content/Controls/GroupingFilterDesktop.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import MultiSelect from '@khanacademy/react-multi-select'
import './GroupingFilter.css'
import ElWithBeforeIcon from 'src/components/ElWithBeforeIcon'
import { IMapGrouping } from 'src/models/maps.models'
import { inject } from 'mobx-react'
import { MapsStore } from 'src/stores/Maps/maps.store'
import { Box } from 'rebass'

interface IProps {
Expand All @@ -16,6 +18,9 @@ interface IState {
initialItems: Array<any>
selectedItems: Array<string>
}
interface IInjectedProps extends IProps {
mapsStore: MapsStore
}

const ItemRenderer = ({ checked, option, onClick }) => {
return (
Expand All @@ -42,13 +47,13 @@ const ItemRenderer = ({ checked, option, onClick }) => {
paddingBottom: '2px',
}}
>
{option.label}
{option.label} ({option.number})
</h4>
</ElWithBeforeIcon>
</div>
)
}

@inject('mapsStore')
class GroupingFilterDesktop extends React.Component<IProps, IState> {
constructor(props: IProps) {
super(props)
Expand All @@ -57,6 +62,9 @@ class GroupingFilterDesktop extends React.Component<IProps, IState> {
selectedItems: [],
}
}
get injected() {
return this.props as IInjectedProps
}

handleChange(selectedItems: Array<string>) {
this.setState({ selectedItems })
Expand All @@ -74,6 +82,10 @@ class GroupingFilterDesktop extends React.Component<IProps, IState> {
label: item.displayName,
value: item.subType ? item.subType : item.type,
icon: item.icon,
// add split(' ') method to convert to array
number: this.injected.mapsStore.getPinsNumberByFilterType(
item.subType ? item.subType.split(' ') : item.type.split(' '),
),
}))
}

Expand Down
15 changes: 14 additions & 1 deletion src/pages/Maps/Content/Controls/GroupingFilterMobile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import Text from 'src/components/Text'
import { IMapGrouping } from 'src/models/maps.models'
import checkmarkIcon from 'src/assets/icons/icon-checkmark.svg'
import { Flex, Image } from 'rebass'
import { inject } from 'mobx-react'
import { MapsStore } from 'src/stores/Maps/maps.store'

interface IProps {
items: Array<IMapGrouping>
Expand All @@ -14,7 +16,11 @@ interface IProps {
interface IState {
initialItems: Array<any>
}
interface IInjectedProps extends IProps {
mapsStore: MapsStore
}

@inject('mapsStore')
class GroupingFilterMobile extends React.Component<IProps, IState> {
constructor(props: IProps) {
super(props)
Expand All @@ -23,6 +29,10 @@ class GroupingFilterMobile extends React.Component<IProps, IState> {
}
}

get injected() {
return this.props as IInjectedProps
}

addOrRemove = (array, item) => {
// from https://stackoverflow.com/a/52531625
const exists = array.includes(item)
Expand Down Expand Up @@ -52,6 +62,9 @@ class GroupingFilterMobile extends React.Component<IProps, IState> {
label: item.displayName,
value: item.subType ? item.subType : item.type,
icon: item.icon,
number: this.injected.mapsStore.getPinsNumberByFilterType(
item.subType ? item.subType.split(' ') : item.type.split(' '),
),
}))
}

Expand Down Expand Up @@ -80,7 +93,7 @@ class GroupingFilterMobile extends React.Component<IProps, IState> {
<Flex alignItems="center">
<Image width="30px" src={filter.icon} />
<Text medium ml="10px">
{filter.label}
{filter.label} ({filter.number})
</Text>
</Flex>
{selectedItems.includes(filter.value) && (
Expand Down
14 changes: 12 additions & 2 deletions src/stores/Maps/maps.store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,18 @@ export class MapsStore extends ModuleStore {
return
}

const mapPins = this.filterMapPinsByType(filters)
this.filteredPins = mapPins
}

private filterMapPinsByType(filters: Array<string>) {
// filter pins to include matched pin type or subtype
const mapPins = this.mapPins.filter(pin => {
const filteredMapPins = this.mapPins.filter(pin => {
return pin.subType
? filters.includes(pin.subType)
: filters.includes(pin.type)
})
this.filteredPins = mapPins
return filteredMapPins
}

/**
Expand Down Expand Up @@ -223,6 +228,11 @@ export class MapsStore extends ModuleStore {
profileUrl: `${location.origin}/u/${u.userName}`,
}
}
@action
public getPinsNumberByFilterType(filter: Array<string>) {
const pinsNumber = this.filterMapPinsByType(filter)
return pinsNumber.length
}
}

/**********************************************************************************
Expand Down

0 comments on commit 578367a

Please sign in to comment.