import Redux from 'redux' ; import { Language } from 'shared/schema/Constants' ; import { SearchChangeParamsAction , SearchResponseAction } from '../actions/ActionTypes' ; import { getStorageString , setStorageKeyValue } from '../LocalStorage' ; import { SearchState } from './StateTypes' ; const LANGUAGE_KEY = 'language' ; export const initialSearch = { results : null , params : { language : getStorageString ( LANGUAGE_KEY , Language . english ) as Language , order : '+ratingavg' , showOfficial : false , showPrivate : true , text : '' , expansions : [ ] , } , searching : false , } ; export function search ( state = initialSearch , action ) { switch ( action . type ) { case 'CHANGE_SETTINGS' : return { ... state , results : null } ; case 'SEARCH_CHANGE_PARAMS' : const changes = ( action as SearchChangeParamsAction ) . params || { } ; if ( changes . language ) { setStorageKeyValue ( LANGUAGE_KEY , changes . language ) ; } return { ... state , params : { ... state . params , ... changes } , results : null } ; case 'SEARCH_REQUEST' : return { ... state , results : null , searching : true } ; case 'SEARCH_ERROR' : return { ... state , results : [ ] , searching : false } ; case 'SEARCH_RESPONSE' : return { ... state , results : ( action as SearchResponseAction ) . quests , params : ( action as SearchResponseAction ) . params , searching : false , } ; default : return state ; } }