WikiEducationFoundation/WikiEduDashboard

View on GitHub
app/assets/javascripts/reducers/did_you_know.js

Summary

Maintainability
B
4 hrs
Test Coverage
D
66%
import { RECEIVE_DYK, SORT_DYK } from '../constants';
import { sortByKey } from '../utils/model_utils';

const initialState = {
  articles: [],
  sortKey: null,
  loading: true
};

export default function didYouKnow(state = initialState, action) {
  switch (action.type) {
    case RECEIVE_DYK: {
      return {
        articles: action.payload.data.articles,
        sortKey: state.sortKey,
        loading: false
      };
    }
    case SORT_DYK: {
      const newArticles = sortByKey(state.articles, action.key, state.sortKey);
      return {
        articles: newArticles.newModels,
        sortKey: newArticles.newKey,
        loading: state.loading
      };
    }
    default:
      return state;
  }
}