Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
BenGamma committed Aug 29, 2019
2 parents e15fb0d + 1f77831 commit f83c2e7
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 24 deletions.
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

0 comments on commit f83c2e7

Please sign in to comment.