<commit_before>import Marty from "marty";
import AdvertiserConstants from "../constants/advertiserConstants";
import AdvertiserApi from "../sources/advertiserApi";
import AdvertiserActionCreators from "../actions/advertiserActionCreators";

class AdvertiserStore extends Marty.Store {
  constructor() {
    super();

    AdvertiserActionCreators.refreshAdvertisers();

    this.state = { advertisers: {} };

    this.handlers = {
      receiveAdvertisers: AdvertiserConstants.RECEIVE_ADVERTISERS,
      createAdvertiser: AdvertiserConstants.CREATE_ADVERTISER,
      refreshAdvertisers: AdvertiserConstants.REFRESH_ADVERTISERS
    };
  }

  receiveAdvertisers(advertisers) {
    // TODO: why aren't you just using _.merge?
    advertisers.forEach(function(advertiser) {
      this.state.advertisers[advertiser.id] = advertiser;
    }, this);
    this.hasChanged();
  }

  getAdvertisers() {
    return this.fetch({
      id: 'GET_ADVERTISERS',
      locally() {
        return this.state.advertisers || undefined;
      },
      remotely() {
        return AdvertiserApi.getAllAdvertisers();
      }
    });
  }

  getAdvertiser(id) {
    return this.fetch({
      id: 'GET_ADVERTISER',
      locally() {
        return this.state.advertisers ? this.state.advertisers[id] :  undefined;
      },
      remotely() {
        return AdvertiserApi.getAllAdvertisers();
      }
    });
  }

  refreshAdvertisers() {<commit_msg>Fix bug when no advertisers are clientside<commit_after>import Marty from "marty";
import _ from "lodash";
import AdvertiserConstants from "../constants/advertiserConstants";
import AdvertiserApi from "../sources/advertiserApi";
import AdvertiserActionCreators from "../actions/advertiserActionCreators";

class AdvertiserStore extends Marty.Store {
  constructor() {
    super();

    AdvertiserActionCreators.refreshAdvertisers();

    this.state = { advertisers: {} };

    this.handlers = {
      receiveAdvertisers: AdvertiserConstants.RECEIVE_ADVERTISERS,
      createAdvertiser: AdvertiserConstants.CREATE_ADVERTISER,
      refreshAdvertisers: AdvertiserConstants.REFRESH_ADVERTISERS
    };
  }

  receiveAdvertisers(advertisers) {
    // TODO: why aren't you just using _.merge?
    advertisers.forEach(function(advertiser) {
      this.state.advertisers[advertiser.id] = advertiser;
    }, this);
    this.hasChanged();
  }

  getAdvertisers() {
    return this.fetch({
      id: 'GET_ADVERTISERS',
      locally() {
        return _.isEmpty(this.state.advertisers) ? undefined : this.state.advertisers;
      },
      remotely() {
        return AdvertiserApi.getAllAdvertisers();
      }
    });
  }

  getAdvertiser(id) {
    return this.fetch({
      id: 'GET_ADVERTISER',
      locally() {
        return this.state.advertisers ? this.state.advertisers[id] :  undefined;
      },
      remotely() {
        return AdvertiserApi.getAllAdvertisers();
      }
    });
  }

  refreshAdvertisers() {
{'fraction_non_alphanumeric': 0.1133879781420765, 'fraction_numerical': 0.0, 'mean_word_length': 3.2572674418604652, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6317889', 'n_tokens_mistral': 949, 'n_tokens_neox': 899, 'n_words': 152}