{"size": 1325, "ext": "js", "max_stars_repo_path": "app/js/stores/ComponentStore.js", "max_stars_repo_name": "theappbusiness/tab-hq", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "app/js/stores/ComponentStore.js", "max_issues_repo_name": "theappbusiness/tab-hq", "max_issues_repo_issues_event_min_datetime": "2015-09-22T10:17:25.000Z", "max_issues_repo_issues_event_max_datetime": "2015-10-06T17:56:52.000Z", "max_forks_repo_path": "app/js/stores/ComponentStore.js", "max_forks_repo_name": "theappbusiness/tab-hq", "max_forks_repo_forks_event_min_datetime": "2015-09-23T15:48:24.000Z", "max_forks_repo_forks_event_max_datetime": "2015-09-23T15:48:24.000Z"}
{"max_stars_count": null, "max_issues_count": 6, "max_forks_count": 1, "avg_line_length": 22.0833333333, "max_line_length": 66, "alphanum_fraction": 0.7018867925}
'use strict';

import _ from 'lodash';
import ComponentActions from '../actions/ComponentActionCreators';
import ServerActions from '../actions/ServerActionCreators';
import Reflux from 'reflux';

let _components = [];

function _addComponents(rawComponents) {
  _components = _components.concat(rawComponents);
}

const ComponentStore = Reflux.createStore({
  listenables: [ComponentActions, ServerActions],

  onReceiveAllComponents(rawComponents) {
    _addComponents(rawComponents);
    this.trigger();
  },

  onReceiveCreatedComponent(rawComponent) {
    _components.push(rawComponent);
    this.trigger();
  },

  onReceiveDeletedComponent(componentId) {
    _.remove(_components, { id: componentId });
    this.trigger();
  },

  onReceiveUpdatedComponent(componentId, data) {
    _.find(_components, function(component) {
      if (component.id === componentId) {
        component.data = data.data;
      }
    });
    this.trigger();
  },

  onReceiveUpdatedComponents(rawComponents) {
    _components = rawComponents;
    this.trigger();
  },

  getAllBySectionId(sectionId) {
    return _.where(_components, { sectionid: sectionId });
  },

  clearAllBySectionId(sectionId) {
    _.remove(_components, { sectionid: sectionId });
  },

  getAll() {
    return _components;
  }
});

export default ComponentStore;
