import {
  fetchTaxon,
  setCount,
  fetchTaxonChange,
  fetchNames,
  fetchTerms,
  fetchSpecies,
  fetchDescription,
  fetchLinks,
  fetchInteractions,
  fetchTrending,
  fetchWanted,
  fetchRecent,
  fetchSimilar
} from "../../shared/ducks/taxon";
import {
  fetchMonthFrequency,
  fetchMonthOfYearFrequency,
  resetObservationsState
} from "../ducks/observations";
import { fetchLeaders, resetLeadersState } from "../ducks/leaders";
import { windowStateForTaxon } from "../../shared/util";

export function fetchTaxonAssociates( t ) {
  return ( dispatch, getState ) => {
    dispatch( resetLeadersState( ) );
    dispatch( resetObservationsState( ) );
    const s = getState( );
    const taxon = t || s.taxon.taxon;
    if ( taxon.taxon_changes_count ) {
      dispatch( setCount( "taxonChangesCount", taxon.taxon_changes_count ) );
      if ( taxon.taxon_changes_count > 0 ) {
        dispatch( fetchTaxonChange( taxon ) );
      }
    }
    if ( taxon.taxon_schemes_count ) {
      dispatch( setCount( "taxonSchemesCount", taxon.taxon_schemes_count ) );
    }
    dispatch( fetchNames( ) );
    dispatch( fetchLeaders( taxon ) ).then( ( ) => {
      dispatch( fetchTerms( ( ) => {
        dispatch( fetchMonthOfYearFrequency( taxon ) ).then( ( ) => {
          dispatch( fetchMonthFrequency( taxon ) );
        } );
      } ) );
    } );
    if ( taxon.complete_species_count ) {
      dispatch( fetchSpecies( ) );
    }
    switch ( s.config.chosenTab ) {
      case "articles":
        dispatch( fetchDescription( ) );
        dispatch( fetchLinks( ) );
        break;
      case "taxonomy":
        dispatch( fetchNames( ) );
        break;
      case "interactions":
        dispatch( fetchInteractions( ) );
        break;
      case "highlights":
        dispatch( fetchTrending( ) );
        // dispatch( fetchRare( ) );
        dispatch( fetchWanted( ) );
        dispatch( fetchRecent( ) );
        break;
      case "similar":
        dispatch( fetchSimilar( ) );
        break;
      default:
        // it's cool, you probably have what you need
    }
  };
}

export function showNewTaxon( taxon, options ) {
  return dispatch => {
    dispatch( fetchTaxon( taxon ) ).then( ( ) => {
      // scroll to the top of the page when rendering a new taxon
      // (except when specified, e.g. on the taxonomy tab taxon links)
      if ( !( options && options.skipScrollTop === true ) ) {
        window.scrollTo( 0, 0 );
      }
      const s = windowStateForTaxon( taxon );
      history.pushState( s.state, s.title, s.url );
      document.title = s.title;
      dispatch( fetchTaxonAssociates( taxon ) );
    } );
  };
}

@@ -1,24 +1,48 @@
+import _ from %22lodash%22;%0A
 import %7B%0A  fetchTaxon,%0A 
@@ -22,24 +22,24 @@
 %22;%0Aimport %7B%0A
-
   fetchTaxon
@@ -739,32 +739,42 @@
 .taxon;%0A    if (
+ !_.isNil(
  taxon.taxon_cha
@@ -777,32 +777,34 @@
 _changes_count )
+ )
  %7B%0A      dispatc
@@ -974,32 +974,42 @@
 %7D%0A    %7D%0A    if (
+ !_.isNil(
  taxon.taxon_sch
@@ -1012,32 +1012,34 @@
 _schemes_count )
+ )
  %7B%0A      dispatc
@@ -1798,24 +1798,24 @@
 ighlights%22:%0A
+
         disp
@@ -1844,45 +1844,8 @@
  );%0A
-        // dispatch( fetchRare( ) );%0A
     

Set taxon scheme count correctly when showing 0 (closes #1702)