Advertisement
Guest User

Untitled

a guest
May 20th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { cloneElement } from 'react'
  2. import classnames from 'classnames'
  3. import { find, filter, size } from 'lodash'
  4.  
  5. export default class Filter {
  6.   constructor (addFilter, closeDropdown, filters) {
  7.     this.addFilter = addFilter
  8.     this.closeDropdown = closeDropdown
  9.  
  10.     const appliedFilter = find(filters, filter => filter.constructor.name === this.constructor.name)
  11.  
  12.     if (appliedFilter) {
  13.       this.id = appliedFilter.id
  14.       this.filterValues = appliedFilter.filterValues
  15.     } else {
  16.       this.id = Math.random()
  17.     }
  18.   }
  19.  
  20.   /**
  21.    * must be created from extended class
  22.    * This is the style that gets rendered on the first level of filter dropdown
  23.    * Type - node
  24.    */
  25.   filteringAttribute = null
  26.   /**
  27.    * must be created from extended class
  28.    * This item gets renderded after first level item is picked. This is options list.
  29.    * Type - function
  30.    */
  31.   renderOptions = null
  32.   /**
  33.    * must be created from extended class
  34.    * This is the style gets rendered in applied filters list before add filter button.
  35.    * Type - function
  36.    */
  37.   renderAppliedFilter = null
  38.  
  39.   optionSelected = false
  40.   filterValues = []
  41.  
  42.   renderFilteringAttributes = props => {
  43.     return cloneElement(this.filteringAttribute, {
  44.       ...this.filteringAttribute.props,
  45.       ...props,
  46.       className: classnames('requests-filter-add-attribute-item', this.filteringAttribute.props.className)
  47.     })
  48.   }
  49.  
  50.   selectOption = () => {
  51.     this.optionSelected = true
  52.   }
  53.  
  54.   applyFilter = () => {
  55.     this.addFilter(this)
  56.     this.closeDropdown()
  57.   }
  58.  
  59.   removeFilterValue = item => {
  60.     const i = this.filterValues.indexOf(item)
  61.     this.filterValues.splice(i, 1)
  62.   }
  63.  
  64.   addFilterValue = item => this.filterValues.push(item)
  65.  
  66.   toggleFilterValue = value => {
  67.     if (this.filterValues.includes(value)) {
  68.       this.removeFilterValue(value)
  69.     } else {
  70.       this.addFilterValue(value)
  71.     }
  72.   }
  73.  
  74.   filter = items => {
  75.     if (!size(this.filterValues)) {
  76.       return items
  77.     }
  78.  
  79.     return filter(items, this.filterRule)
  80.   }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement