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

100% Statements 59/59
100% Branches 0/0
100% Functions 22/22
100% Lines 57/57
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 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 1761x   1x       1x       1x   1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x                 16x     1x   3x             1x 1x     1x 3x                       1x 1x     1x 3x                 1x 1x     1x 3x             1x 1x     1x 3x                     1x 1x     1x 3x                 1x 1x     1x 3x                 1x 1x     1x 3x                 1x 1x     1x 3x               1x 1x     1x 2x               1x 1x     1x  
import { Injectable} from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { NgRedux, select } from '@angular-redux/store';
 
import { IAppState } from '../../store/state/AppState';
import { IFileInfo } from '../../model/fileInfo.model';
import { formatAMPM } from '../../common/mw.common.module';
import { PageActionType } from '../../store/actions/pageActionType';
 
@Injectable()
export abstract class PageActions {
 
  static INITIALIZE_FILES_REQUEST = 'INITIALIZE_FILES_REQUEST';
  static INITIALIZE_FILES_SUCCESS = 'INITIALIZE_FILES_SUCCESS';
  static INITIALIZE_FILES_FAILED = 'INITIALIZE_FILES_FAILED';
  static UPDATE_FILES_REQUEST = 'UPDATE_FILES_REQUEST';
  static UPDATE_FILES_SUCCESS = 'UPDATE_FILES_SUCCESS';
  static UPDATE_FILES_FAILED = 'UPDATE_FILES_FAILED';
  static SET_NUM_VISIBLE = 'SET_NUM_VISIBLE';
  static CHANGE_FILE = 'CHANGE_FILE';
  static RELOAD_FILE = 'RELOAD_FILE';
  static SET_LAST_REFRESH = 'SET_LAST_REFRESH';
  static ACTIONS = [
    PageActions.INITIALIZE_FILES_REQUEST, PageActions.INITIALIZE_FILES_SUCCESS, PageActions.INITIALIZE_FILES_FAILED,
    PageActions.UPDATE_FILES_REQUEST, PageActions.UPDATE_FILES_SUCCESS, PageActions.UPDATE_FILES_FAILED,
    PageActions.SET_NUM_VISIBLE, PageActions.CHANGE_FILE, PageActions.RELOAD_FILE, PageActions.SET_LAST_REFRESH 
  ];
 
  abstract PAGE: string;
 
  constructor(
    protected ngRedux: NgRedux<IAppState>,
  ) {}
 
  createInitializeListRequest(): PageActionType {
    // Used to activate the spinner on start of page load
    return {
      type: PageActions.INITIALIZE_FILES_REQUEST,
      subState: this.PAGE,
      uiStartLoading: `initialize_${this.PAGE}`,
      excludeFromLog: true,
    };
  }
  initializeListRequest() {
    this.ngRedux.dispatch( this.createInitializeListRequest() );
  }
 
  createInitializeListSuccess( files: IFileInfo[], numToDisplay: number): PageActionType {
    return {
      type: PageActions.INITIALIZE_FILES_SUCCESS,
      subState: this.PAGE,
      uiEndLoading: `initialize_${this.PAGE}_success`,
      payload: {
        files,
        fileInfo: files[0],
        numVisible: numToDisplay,
        lastRefresh: formatAMPM(new Date())
      }
    };
  }
  initializeListSuccess(files: IFileInfo[], numToDisplay: number) {
    this.ngRedux.dispatch( this.createInitializeListSuccess(files, numToDisplay) );
  }
 
  createInitializeListFailed(error: Error): PageActionType {
    return {
      type: PageActions.INITIALIZE_FILES_FAILED,
      subState: this.PAGE,
      uiEndLoading: `update_${this.PAGE}_fail`,
      payload: {
        error
      }
    };
  }
  initializeListFailed(error: Error) {
    this.ngRedux.dispatch( this.createInitializeListFailed(error) );
  }
 
  createUpdateListRequest(): PageActionType {
    return {
      type: PageActions.UPDATE_FILES_REQUEST,
      subState: this.PAGE,
      uiStartLoading: `update_${this.PAGE}`,
      excludeFromLog: true,
    };
  }
  updateListRequest() {
    this.ngRedux.dispatch( this.createUpdateListRequest() );
  }
 
  createUpdateListSuccess(files: IFileInfo[], numToDisplay: number): PageActionType {
    return {
      type: PageActions.UPDATE_FILES_SUCCESS,
      subState: this.PAGE,
      uiEndLoading: `update_${this.PAGE}_success`,
      payload: {
        files,
        numVisible: numToDisplay,
        lastRefresh: formatAMPM(new Date()),
      },
    };
  }
  updateListSuccess(files: IFileInfo[], numToDisplay: number) {
    this.ngRedux.dispatch( this.createUpdateListSuccess(files, numToDisplay) );
  }
 
  createUpdateListFailed(error) {
    return {
      type: PageActions.UPDATE_FILES_FAILED,
      subState: this.PAGE,
      uiEndLoading: `update_${this.PAGE}_fail`,
      payload: {
        error
      },
    };
  }
  updateListFailed(error) {
    this.ngRedux.dispatch( this.createUpdateListFailed(error) );
  }
 
  createChangeFile(fileInfo: IFileInfo): PageActionType {
    return {
      type: PageActions.CHANGE_FILE,
      subState: this.PAGE,
      payload: {
        fileInfo,
        lastRefresh: formatAMPM(new Date()),
      },
    };
  }
  changeFile(fileInfo: IFileInfo) {
    this.ngRedux.dispatch( this.createChangeFile(fileInfo) );
  }
 
  createRefresh(fileInfo: IFileInfo): PageActionType {
    return {
      type: PageActions.RELOAD_FILE,
      subState: this.PAGE,
      payload: {
        fileInfo,
        lastRefresh: formatAMPM(new Date()),
      },
    };
  }
  refresh(fileInfo: IFileInfo) {
    this.ngRedux.dispatch( this.createRefresh(fileInfo) );
  }
 
  createSetNumToDisplay(numToDisplay: number): PageActionType {
    return {
      type: PageActions.SET_NUM_VISIBLE,
      subState: this.PAGE,
      payload: {
        numVisible: numToDisplay,
      },
    };
  }
  setNumToDisplay(numToDisplay: number) {
    this.ngRedux.dispatch( this.createSetNumToDisplay(numToDisplay) );
  }
 
  createSetLastRefresh(): PageActionType {
    return {
      type: PageActions.SET_LAST_REFRESH,
      subState: this.PAGE,
      payload: {
        lastRefresh: formatAMPM(new Date()),
      },
    };
  }
  setLastRefresh() {
    this.ngRedux.dispatch( this.createSetLastRefresh() );
  }
 
}