all files / src/app/views/ PageView.js

100% Statements 14/14
100% Branches 4/4
100% Functions 7/7
100% Lines 14/14
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                                                                         
import {NO_REPO, NO_USER, BAD_ABSTRACT} from '../utils/error.constants';
 
const ERROR_MSG = {
  repos: NO_REPO,
  users: NO_USER
};
 
/**
 * This class is indended to be extended by RepoView and UserView components.
 * Here we abstract the duplicate operations required to query the db.
 *
 * Note the lack of `@ngInject` above the constructor.
 * Angular injection needs to be carried out in the child constructor.
 */
export class PageViewAbstract {
  constructor($timeout, $state, githubService) {
    if (this.constructor === PageViewAbstract) {
      throw new TypeError(BAD_ABSTRACT);
    }
 
    this.$state = $state;
    this.$timeout = $timeout;
    this.github = githubService;
  }
 
  initView(page, query) {
    if (!query) {
      return this.$state.go('app');
    }
 
    return this.$timeout(() => {
      return this.github.getResource(page, query)
        .then(
          res => {
            return {
              repos: () => {
                this.repo = res;
              },
              users: () => {
                this.user = res;
              }
            }[page]();
          },
          () => {
            this.error = {message: ERROR_MSG[page]};
          }
        );
    }, 350);
  }
}