Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add filter util function, tidy howto filter code #551

Merged
merged 1 commit into from
Aug 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 8 additions & 24 deletions src/stores/Howto/howto.store.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Storage } from '../storage'
import { ModuleStore } from '../common/module.store'
import { ISelectedTags } from 'src/models/tags.model'
import { RootStore } from '..'
import { includesAll } from 'src/utils/filters'

export class HowtoStore extends ModuleStore {
// we have two property relating to docs that can be observed
Expand Down Expand Up @@ -53,30 +54,13 @@ export class HowtoStore extends ModuleStore {
}

@computed get filteredHowtos() {
// Check if this.selectedTags is empty
if (
Object.keys(this.selectedTags).length === 0 &&
this.selectedTags.constructor === Object
) {
return this.allHowtos
} else {
const filtered: IHowto[] = []
this.allHowtos.map(howto => {
if (howto.tags !== undefined) {
// encapsulate howtoTags in const to avoid type error
const howtoTags = Object.keys(howto.tags)
// filter the howto containing the selected tags
const isMatching = Object.keys(this.selectedTags).every(val => {
return howtoTags.includes(val)
})
// push the matching howto to filtered array
if (isMatching) {
filtered.push(howto)
}
}
})
return filtered
}
const selectedTagsArr = Object.keys(this.selectedTags)
return selectedTagsArr.length > 0
? this.allHowtos.filter(howto => {
const tags = howto.tags ? Object.keys(howto.tags) : null
return tags ? includesAll(selectedTagsArr, tags) : false
})
: this.allHowtos
}

public updateSelectedTags(tagKey: ISelectedTags) {
Expand Down
12 changes: 12 additions & 0 deletions src/utils/filters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,18 @@ import { ISODateString } from 'src/models/common.models'
In the future this could possibly be replaced by more comprehensive libraries
*/

/************************************************************************
* Array Methods
***********************************************************************/
/**
* Test whether a one array contains all string values of another array
* @param arr1 The array that will be tested, e.g ["a","b","c"]
* @param arr2 The values to test, e.g. ["a","c"]
*/
export const includesAll = (arr1: string[], arr2: string[]) => {
return arr1.every(val => arr2.includes(val))
}

/************************************************************************
* Date Methods
***********************************************************************/
Expand Down