import Redux from 'redux' ; import { FilterChangeAction , FiltersCalculateAction } from '../actions/ActionTypes' ; import { SHEETS } from '../Constants' ; import { CardType , FiltersState } from './StateTypes' ; export let initialState = { sheet : { current : 'All' , default : 'All' , options : [ 'All' ] , } , class : { current : 'All' , default : 'All' , options : [ 'All' ] , } , tier : { current : 'All' , default : 'All' , options : [ 'All' ] , } , theme : { current : 'BlackAndWhite' , default : 'BlackAndWhite' , options : [ 'BlackAndWhite' , 'Color' ] , } , export : { current : 'PrintAndPlay' , default : 'PrintAndPlay' , options : [ 'PrintAndPlay' , 'WebView' , 'DriveThruCards' , 'AdMagicFronts' , 'AdMagicBacks' , 'FrontsOnly' ] , } , source : { current : SHEETS [ 0 ] . name , default : SHEETS [ 0 ] . name , options : [ ... SHEETS . map ( ( s ) => s . name ) , ] , } , } ; export default function Filters ( state = initialState , action ) { let newState ; switch ( action . type ) { case 'FILTER_CHANGE' : const filterChange = action as FilterChangeAction ; newState = { ... state } ; if ( newState [ filterChange . name ] ) { newState [ filterChange . name ] . current = filterChange . value ; } return newState ; case 'FILTERS_CALCULATE' : newState = { ... state } ; return updateFilterOptions ( newState , ( action as FiltersCalculateAction ) . cardsFiltered ) ; default : return state ; } } function updateFilterOptions ( filters , cards ) { if ( cards === null ) { return filters ; } filters . sheet . options = [ filters . sheet . default ] . concat ( cards . reduce ( ( acc , card ) => { if ( acc . indexOf ( card . sheet ) === - 1 ) { acc . push ( card . sheet ) ; } return acc ; } , [ ] ) . sort ( ) ) ; filters . class . options = [ filters . class . default ] . concat ( cards . reduce ( ( acc , card ) => { if ( acc . indexOf ( card . class ) === - 1 && card . class !== '' && [ 'Ability' , 'Encounter' ] . indexOf ( card . sheet ) !== - 1 ) { acc . push ( card . class ) ; } return acc ; } , [ ] ) . sort ( ) ) ; filters . tier . options = [ filters . tier . default ] . concat ( cards . reduce ( ( acc , card ) => { if ( acc . indexOf ( card . tier ) === - 1 && typeof card . tier === 'number' && [ 'Encounter' , 'Loot' ] . indexOf ( card . sheet ) !== - 1 ) { acc . push ( card . tier ) ; } return acc ; } , [ ] ) . sort ( ) ) ; return filters ; }