All files / src/app/linqpad-review-pages/common/search search.actions.ts

100% Statements 35/35
100% Branches 0/0
100% Functions 12/12
100% Lines 33/33
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 861x     1x     1x     1x   1x 1x 1x 1x 1x 1x     1x     2x     1x 2x         1x 1x     1x 2x             1x 1x     1x 2x             1x 1x     1x 2x               1x 1x     1x 2x             1x 1x     1x  
import { Injectable,  } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { Action } from 'redux';
import { NgRedux, select, } from '@angular-redux/store';
import { IAppState } from '../../../store/state/AppState';
import { SearchActionType } from '../../../store/actions/searchActionType';
import { searchInitialState } from '../../../store/state/search.state';
 
@Injectable()
export class SearchActions {
 
  static RESET_RESULTS = 'RESET_RESULTS';
  static SET_PAGE = 'SET_PAGE';
  static SET_SEARCHTERM = 'SET_SEARCHTERM';
  static SET_RESULTS_SUCCESS = 'SET_RESULTS_SUCCESS';
  static SET_RESULTS_FAILED = 'SET_RESULTS_FAILED';
  static ACTIONS = [SearchActions.RESET_RESULTS, SearchActions.SET_PAGE, SearchActions.SET_SEARCHTERM,
    SearchActions.SET_RESULTS_SUCCESS, SearchActions.SET_RESULTS_FAILED, ];
 
  static NO_RESULTS_MESSAGE = 'No results found';
 
  constructor(
    private ngRedux: NgRedux<IAppState>,
  ) {}
 
  createResetResults(): SearchActionType {
    return {
      type: SearchActions.RESET_RESULTS,
      payload: searchInitialState
    };
  }
  resetResults() {
    this.ngRedux.dispatch(this.createResetResults());
  }
 
  createSetResultsSuccess(results: string[]): SearchActionType {
    return {
      type: SearchActions.SET_RESULTS_SUCCESS,
      payload: {
        results
      }
    };
  }
  setResultsSuccess(results: string[]) {
    this.ngRedux.dispatch(this.createSetResultsSuccess(results));
  }
 
  createSetResultsFailed(): SearchActionType {
    return {
      type: SearchActions.SET_RESULTS_FAILED,
      payload: {
        results: [SearchActions.NO_RESULTS_MESSAGE]
      }
    };
  }
  setResultsFailed() {
    this.ngRedux.dispatch(this.createSetResultsFailed());
  }
 
  createSetPage(page, pageIsSearchable): SearchActionType {
    return {
      type: SearchActions.SET_PAGE,
      payload: {
        page,
        pageIsSearchable
      }
    };
  }
  setPage(page, pageIsSearchable) {
    this.ngRedux.dispatch(this.createSetPage(page, pageIsSearchable));
  }
 
  createSetSearchTerm(searchTerm: string): SearchActionType {
    return {
      type: SearchActions.SET_SEARCHTERM,
      payload: {
        searchTerm
      }
    };
  }
  setSearchTerm(searchTerm: string) {
    this.ngRedux.dispatch(this.createSetSearchTerm(searchTerm));
  }
 
}