18F/cg-dashboard

View on GitHub
static_src/stores/quota_store.js

Summary

Maintainability
A
1 hr
Test Coverage
/*
 * Store for quota data.
 */

import Immutable from "immutable";

import BaseStore from "./base_store.js";
import cfApi from "../util/cf_api.js";
import { quotaActionTypes } from "../constants.js";

class QuotaStore extends BaseStore {
  constructor() {
    super();
    this.storeData = new Immutable.List();
    this.subscribe(() => this.handleAction.bind(this));
  }

  handleAction(action) {
    switch (action.type) {
      case quotaActionTypes.ORGS_QUOTAS_FETCH: {
        this.load([cfApi.fetchOrgsQuotas()]);
        this.emitChange();
        break;
      }

      case quotaActionTypes.ORGS_QUOTAS_RECEIVED: {
        const quotas = action.quotas;
        this.mergeMany("guid", quotas, changed => {
          if (changed) this.emitChange();
        });
        break;
      }

      case quotaActionTypes.SPACES_QUOTAS_FETCH: {
        this.load([cfApi.fetchSpacesQuotas()]);
        this.emitChange();
        break;
      }

      case quotaActionTypes.SPACES_QUOTAS_RECEIVED: {
        const quotas = action.quotas;
        this.mergeMany("guid", quotas, changed => {
          if (changed) this.emitChange();
        });
        break;
      }

      default:
        break;
    }
  }
}

export default new QuotaStore();