Skip to content
This repository has been archived by the owner on Aug 9, 2020. It is now read-only.

Commit

Permalink
selectors that are easier to read
Browse files Browse the repository at this point in the history
  • Loading branch information
mariusandra committed Dec 6, 2016
1 parent 56ef88f commit 241d30f
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 52 deletions.
13 changes: 6 additions & 7 deletions app/scenes/homepage/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ class HomepageLogic extends Logic {
})

// SELECTORS
selectors = ({ path, structure, constants, selectors, addSelector }) => {
addSelector('capitalizedName', PropTypes.string, [
selectors.name
], (name) => {
return name.trim().split(' ').map(k => `${k.charAt(0).toUpperCase()}${k.slice(1).toLowerCase()}`).join(' ')
})
}
selectors = ({ path, structure, constants, selectors }) => ({
capitalizedName: [
() => [PropTypes.string, selectors.name],
(name) => name.trim().split(' ').map(k => `${k.charAt(0).toUpperCase()}${k.slice(1).toLowerCase()}`).join(' ')
]
})
}

export default new HomepageLogic().init()
22 changes: 12 additions & 10 deletions app/scenes/homepage/slider/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,21 +35,23 @@ class SliderLogic extends Logic {

// REDUCER
structure = ({ actions, constants }) => ({
currentSlide: [1, PropTypes.number, {
currentSlide: [0, PropTypes.number, {
[actions.updateSlide]: (state, payload) => payload.index % images.length
}]
})

// SELECTORS (data from reducer + more)
selectors = ({ path, structure, constants, selectors, addSelector }) => {
addSelector('currentImage', PropTypes.object, [
selectors.currentSlide
], (currentSlide) => {
return images[currentSlide]
})

addSelector('imageCount', PropTypes.number, [], () => images.length)
}
selectors = ({ path, structure, constants, selectors }) => ({
currentImage: [
() => [PropTypes.object, selectors.currentSlide],
(currentSlide) => images[currentSlide]
],

imageCount: [
() => [PropTypes.number],
() => images.length
]
})
}

export default new SliderLogic().init()
59 changes: 28 additions & 31 deletions app/scenes/todos/logic.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,38 +154,35 @@ class TodosLogic extends Logic {
})

// SELECTORS (data from reducer + more)
selectors = ({ path, structure, constants, selectors, addSelector }) => {
addSelector('visibleTodos', PropTypes.array, [
selectors.visibilityFilter,
selectors.todos
], (visibilityFilter, todos) => do {
if (visibilityFilter === constants.SHOW_ALL) {
Object.values(todos)
} else if (visibilityFilter === constants.SHOW_ACTIVE) {
Object.values(todos).filter(todo => !todo.completed)
} else if (visibilityFilter === constants.SHOW_COMPLETED) {
Object.values(todos).filter(todo => todo.completed)
selectors = ({ path, structure, constants, selectors }) => ({
todoCount: [
() => [PropTypes.number, selectors.todos],
(todos) => Object.keys(todos).length
],

activeTodoCount: [
() => [PropTypes.number, selectors.todos],
(todos) => Object.values(todos).filter(todo => !todo.completed).length
],

completedTodoCount: [
() => [PropTypes.number, selectors.todos],
(todos) => Object.values(todos).filter(todo => todo.completed).length
],

visibleTodos: [
() => [PropTypes.array, selectors.visibilityFilter, selectors.todos],
(visibilityFilter, todos) => {
if (visibilityFilter === constants.SHOW_ALL) {
return Object.values(todos)
} else if (visibilityFilter === constants.SHOW_ACTIVE) {
return Object.values(todos).filter(todo => !todo.completed)
} else if (visibilityFilter === constants.SHOW_COMPLETED) {
return Object.values(todos).filter(todo => todo.completed)
}
}
})

addSelector('todoCount', PropTypes.number, [
selectors.todos
], (todos) => do {
Object.keys(todos).length
})

addSelector('activeTodoCount', PropTypes.number, [
selectors.todos
], (todos) => do {
Object.values(todos).filter(todo => !todo.completed).length
})

addSelector('completedTodoCount', PropTypes.number, [
selectors.todos
], (todos) => do {
Object.values(todos).filter(todo => todo.completed).length
})
}
]
})
}

export default new TodosLogic().init()
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"babel-runtime": "6.18.0",
"es6-promise": "4.0.5",
"kea": "0.13.0",
"kea": "0.14.0",
"mirror-creator": "1.1.0",
"react": "15.4.1",
"react-dom": "15.4.1",
Expand Down
6 changes: 3 additions & 3 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2501,9 +2501,9 @@ jsx-ast-utils@^1.3.4:
acorn-jsx "^3.0.1"
object-assign "^4.1.0"

kea@0.13.0:
version "0.13.0"
resolved "https://registry.yarnpkg.com/kea/-/kea-0.13.0.tgz#516e14630c5fb9ef5d4864ae7fb67eda7db4b849"
kea@0.14.0:
version "0.14.0"
resolved "https://registry.yarnpkg.com/kea/-/kea-0.14.0.tgz#a40a4145b3a1bf74246a3f308d721475936ae4c7"

kind-of@^3.0.2:
version "3.0.4"
Expand Down

0 comments on commit 241d30f

Please sign in to comment.