import Redux from 'redux' ; import { initialState } from '../reducers/Filters' ; import { CardType } from '../reducers/StateTypes' ; import { getStore } from '../Store' ; import { FilterChangeAction , FiltersCalculateAction } from './ActionTypes' ; import { cardsFilter , downloadCards } from './Cards' ; declare var require ; declare var window ; const qs = require ( 'qs' ) ; export function filterChange ( name , value ) { return ( dispatch ) => { let cardstype = null ; if ( name === 'source' && value === 'custom' ) { cardstype = window . prompt ( 'Please enter a sheet type (Ability, Adventurer, Encounter, Helper, Loot, Persona, Skill)' ) ; value = window . prompt ( 'Please enter your card sheet publish CSV URL (see "?" in the top right for help)' , '' ) as string ; } dispatch ( { type : 'FILTER_CHANGE' , name , value } ) as FilterChangeAction ; const query = { ... qs . parse ( window . location . search . substring ( 1 ) ) , [ name ] : value } ; for ( const key in query ) { if ( initialState [ key ] && query [ key ] === initialState [ key ] . default ) { delete query [ key ] ; } } window . history . pushState ( null , 'Expedition Card Creator' , '?' + qs . stringify ( query ) ) ; const store = getStore ( ) ; if ( name === 'source' ) { dispatch ( downloadCards ( store . getState ( ) . filters . source . current , cardstype ) ) ; } else { dispatch ( cardsFilter ( store . getState ( ) . cards . data , store . getState ( ) . filters ) ) ; dispatch ( filtersCalculate ( store . getState ( ) . cards . filtered ) ) ; } } ; } export function loadFiltersFromUrl ( ) { return ( dispatch ) => { const query = qs . parse ( window . location . search . substring ( 1 ) ) ; for ( const key in query ) { if ( query . hasOwnProperty ( key ) ) { const val = ( isNaN ( query [ key ] ) ? query [ key ] : Number ( query [ key ] ) ) ; dispatch ( filterChange ( key , val ) ) ; } } } ; } export function filtersCalculate ( cardsFiltered ) { return { type : 'FILTERS_CALCULATE' , cardsFiltered } ; }