define([
  'react',
  'underscore',
  'jsx/shared/helpers/createStore',
  'jquery',
  'compiled/backbone-ext/DefaultUrlMixin',
  'compiled/fn/parseLinkHeader',
], (React, _, createStore, $, DefaultUrlMixin) => {

  var CourseActivitySummaryStore = createStore({streams: {}})

  CourseActivitySummaryStore.getStateForCourse = function(courseId) {
    if (_.isUndefined(courseId)) return CourseActivitySummaryStore.getState()

    if (_.has(CourseActivitySummaryStore.getState()['streams'], courseId)) {
      return CourseActivitySummaryStore.getState()['streams'][courseId]
    } else {
      CourseActivitySummaryStore._fetchForCourse(courseId)
      return {}
    }
  }

  CourseActivitySummaryStore._fetchForCourse = function(courseId) {
    var state

    $.getJSON('/api/v1/courses/' + courseId + '/activity_stream/summary', function(stream) {
      state = CourseActivitySummaryStore.getState()
      state['streams'][courseId] = {
        stream: stream
      }
      CourseActivitySummaryStore.setState(state)
    })
  }

  return CourseActivitySummaryStore
});

define([
  'react',
  'underscore',
  'jsx/shared/helpers/createStore',
  'jquery',
  'compiled/backbone-ext/DefaultUrlMixin',
  'compiled/fn/parseLinkHeader',
], (React, _, createStore, $, DefaultUrlMixin) => {

  var CourseActivitySummaryStore = createStore({streams: {}})

  CourseActivitySummaryStore.getStateForCourse = function(courseId) {
    if (_.isUndefined(courseId)) return CourseActivitySummaryStore.getState()

    if (_.has(CourseActivitySummaryStore.getState()['streams'], courseId)) {
      return CourseActivitySummaryStore.getState()['streams'][courseId]
    } else {
      CourseActivitySummaryStore.getState()['streams'][courseId] = {}
      CourseActivitySummaryStore._fetchForCourse(courseId)
      return {}
    }
  }

  CourseActivitySummaryStore._fetchForCourse = function(courseId) {
    var state

    $.getJSON('/api/v1/courses/' + courseId + '/activity_stream/summary', function(stream) {
      state = CourseActivitySummaryStore.getState()
      state['streams'][courseId] = {
        stream: stream
      }
      CourseActivitySummaryStore.setState(state)
    })
  }

  return CourseActivitySummaryStore
});

Reduce unnecessary activity stream XHR fetches
Reduce unnecessary activity stream XHR fetches

On a the dashboard page for a user with multiple courses,
duplicate XHR fetches are sent. For example with 12 cards shown,
there will be 78 fetches (12+11+10...+1) rather than 12.

Since all of the DashboardCard React views share a single
CourseActivitySummaryStore, any update to the store triggers
every card to request an update from the store and if it does
not exist, it fires another XHR fetch.

This PR prevents the additional fetches by storing an empty
result `{}` in the store which will be replaced when the XHR
call completes.

Test Plan
- Load the dashboard page for a user multiple course cards
- Observe the XHR requests for `/api/v1/courses/*/activity_stream/summary`
- There should only be 1 request per card and unread activity should show up

Change-Id: Idf5a07d03162a66ff6ec9db8913aab7a7283261a
