ali322/CNodeRN

View on GitHub
app/module/common/reducer.js

Summary

Maintainability
A
3 hrs
Test Coverage
import * as constants from './constant'

export function messageCountReducer(state = {}, action) {
  switch (action.type) {
    case constants.REQUEST_MESSAGECOUNT:
      return {
        ...state,
        messageCountFetching: true
      }
    case constants.RESPONSE_MESSAGECOUNT:
      return {
        ...state,
        messageCountFetching: false,
        count: action.ret.data,
        messageCountFetched: action.ret.success
      }
    default:
      return state
  }
}

export function userPrefsReducer(
  state = {
    userPrefs: {
      preferredFontSize: 14,
      preferredTheme: 'default'
    }
  },
  action
) {
  switch (action.type) {
    case constants.START_SAVEUSERPREFS:
      return {
        ...state,
        userPrefsSaving: true
      }
    case constants.FINISH_SAVEUSERPREFS:
      return {
        ...state,
        userPrefs: action.ret,
        userPrefsSaving: false,
        userPrefsSaved: true
      }
    case constants.REQUEST_USERPREFS:
      return {
        ...state,
        userPrefsFetching: true
      }
    case constants.RESPONSE_USERPREFS:
      return {
        ...state,
        userPrefsFetching: false,
        userPrefsFetched: true,
        userPrefs: action.ret ? action.ret : state.userPrefs
      }
    default:
      return state
  }
}

export function commonReducer(state = {}, action) {
  switch (action.type) {
    case constants.FAIL_REQUEST:
      return {
        ...state,
        requestFailed: true
      }
    default:
      return state
  }
}