import { Record } from 'immutable'
import keyedReducer from '../reducers/keyed-reducer'
import {
  AUDIO_MANAGER_INITIALIZED,
  CHAT_LOADING_COMPLETE,
  SUBSCRIPTIONS_CLIENT_LOADING_COMPLETE,
  SUBSCRIPTIONS_USER_LOADING_COMPLETE,
  WHISPERS_LOADING_COMPLETE,
} from '../actions'

export const LoadingState = new Record({
  audio: true,
  chat: true,
  clientSubscriptions: true,
  userSubscriptions: true,
  whispers: true,
})

export default keyedReducer(new LoadingState(), {
  [AUDIO_MANAGER_INITIALIZED](state, action) {
    return state.set('audio', false)
  },

  [CHAT_LOADING_COMPLETE](state, action) {
    return state.set('chat', false)
  },

  [SUBSCRIPTIONS_CLIENT_LOADING_COMPLETE](state, action) {
    return state.set('clientSubscriptions', false)
  },

  [SUBSCRIPTIONS_USER_LOADING_COMPLETE](state, action) {
    return state.set('userSubscriptions', false)
  },

  [WHISPERS_LOADING_COMPLETE](state, action) {
    return state.set('whispers', false)
  },
})

import { Record } from 'immutable'
import keyedReducer from '../reducers/keyed-reducer'
import {
  AUDIO_MANAGER_INITIALIZED,
  CHAT_LOADING_COMPLETE,
  NETWORK_SITE_DISCONNECTED,
  SUBSCRIPTIONS_CLIENT_LOADING_COMPLETE,
  SUBSCRIPTIONS_USER_LOADING_COMPLETE,
  WHISPERS_LOADING_COMPLETE,
} from '../actions'

export const LoadingState = new Record({
  audio: true,
  chat: true,
  clientSubscriptions: true,
  userSubscriptions: true,
  whispers: true,
})

export default keyedReducer(new LoadingState(), {
  [AUDIO_MANAGER_INITIALIZED](state, action) {
    return state.set('audio', false)
  },

  [CHAT_LOADING_COMPLETE](state, action) {
    return state.set('chat', false)
  },

  [SUBSCRIPTIONS_CLIENT_LOADING_COMPLETE](state, action) {
    return state.set('clientSubscriptions', false)
  },

  [SUBSCRIPTIONS_USER_LOADING_COMPLETE](state, action) {
    return state.set('userSubscriptions', false)
  },

  [WHISPERS_LOADING_COMPLETE](state, action) {
    return state.set('whispers', false)
  },

  [NETWORK_SITE_DISCONNECTED](state, action) {
    // Reset the loading state of the stuff that gets initialized through sockets
    return state.withMutations(s =>
      s
        .set('chat', true)
        .set('clientSubscriptions', true)
        .set('userSubscriptions', true)
        .set('whispers', true),
    )
  },
})

Reset the loading state after the site socket gets disconnected.
Reset the loading state after the site socket gets disconnected.

This fixes a bug where after navigating to a route which is not below
the SiteConnectedFilter, the site socket would get disconnected, but all
the loading state would remain "loaded". Then after going back to a
route under the SiteConnectedFilter, site socket would get connected
again, but the LoadingFilter would not wait for all the state to be
loaded again since it thought it was already loaded.

This lead the `goToIndex` action creator to incorrectly change the route
based on the current state, which was wrong.

Also, we only reset the loading state of stuff that gets initialized
through the web sockets; other stuff can remain "loaded", eg. an audio
manager.
