Giter Club home page Giter Club logo

redux-plus's People

Contributors

ashtonsix avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

redux-plus's Issues

Dynamic selector not working

Investigate this (see TODO comment):

import {
  createStore, combineReducers, createReducer, createSelector,
  applyMiddleware, compose, plus,
} from 'redux-plus'
import api from './api'
import toTrees, {getMaxDepth, getNodes} from './toTrees'

const reducer = combineReducers({
  categories: createReducer({
    GET_DATA: api('GET', '/categories'),
    GET_DATA_SUCCESS: (state, {payload: categories}) => categories,
  }, []),
  overview: createReducer({
    SELECT: (state, {payload: _id}) => {
      const index = state.selected.findIndex(id => id === _id)
      let selected
      if (index === -1) selected = state.selected.concat(_id)
      else selected = state.selected.slice(0, index + 1)
      return {...state, selected}
    },
    CLEAR_SELECTION: (state) => ({...state, selected: []})
  }, {selected: [], sortBy: 'allocation'}),
  detailID: createReducer({
    SET_DETAIL_VIEW: (state, {payload: id}) => id,
    CLEAR_DETAIL_VIEW: () => '',
    SELECT: () => '',
    CLEAR_SELECTION: () => '',
  }, ''),
  detail: createSelector(
    'detailID',
    'overview.selected', // TODO (redux-plus): investigate why dynamic selector doesn't work here
    'dataTree.id',
    'treeStats.nodes',
    (state, id1, selected, id3, nodes) => {
      console.log('1', id1, '2', selected.length, '3', id3)
      const id = id1 || selected.slice(-1)[0] || id3
      if (!nodes || !id) return null
      return nodes.find(node => id === node.id)
    }
  ),
  dataTree: createSelector(
    'categories',
    'overview.sortBy',
    (state, categories, sortBy) =>
      toTrees(categories, sortBy, ['desc'])[0]
  ),
  treeStats: createSelector(
    'dataTree',
    (state, dataTree) => ({
      maxDepth: !dataTree ? 0 : getMaxDepth(dataTree),
      leafNodes: !dataTree ? [] : getNodes(dataTree, ({children}) => !children.length),
      nodes: !dataTree ? [] : getNodes(dataTree),
    })
  )
})

const logger = applyMiddleware(store => next => action => {
  const result = next(action)
  console.log(action, store.getState().detail)
  return result
})
export const store = createStore(reducer, compose(plus, logger))
export default store

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.