-
Notifications
You must be signed in to change notification settings - Fork 379
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Composable selectors #743
Open
shorja
wants to merge
6
commits into
GriddleGriddle:master
Choose a base branch
from
shorja:m-composable-selectors
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Composable selectors #743
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
89ecac2
Composable selectors
5fca14d
Refactored the selector composing function into a utils class, added …
a04c699
Refactor and add story
8db33e1
Merge branch 'master' into m-composable-selectors
8f9c74c
add dataLoadingSelector to composedSelectors
6da06fa
Complete example story
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,153 @@ | ||
import Immutable from 'immutable'; | ||
import { createSelector } from 'reselect'; | ||
import _ from 'lodash'; | ||
import MAX_SAFE_INTEGER from 'max-safe-integer' | ||
import { griddleCreateSelector } from '../utils/selectorUtils'; | ||
|
||
export const dataLoadingSelector = griddleCreateSelector( | ||
"dataSelector", | ||
data => !data | ||
); | ||
|
||
export const hasPreviousSelector = griddleCreateSelector( | ||
"currentPageSelector", | ||
(currentPage) => (currentPage > 1) | ||
); | ||
|
||
export const maxPageSelector = griddleCreateSelector( | ||
"pageSizeSelector", | ||
"recordCountSelector", | ||
(pageSize, recordCount) => { | ||
const calc = recordCount / pageSize; | ||
const result = calc > Math.floor(calc) ? Math.floor(calc) + 1 : Math.floor(calc); | ||
return _.isFinite(result) ? result : 1; | ||
} | ||
); | ||
|
||
export const hasNextSelector = griddleCreateSelector( | ||
"currentPageSelector", | ||
"maxPageSelector", | ||
(currentPage, maxPage) => { | ||
return currentPage < maxPage; | ||
} | ||
); | ||
|
||
export const allColumnsSelector = griddleCreateSelector( | ||
"dataSelector", | ||
"renderPropertiesSelector", | ||
(data, renderProperties) => { | ||
const dataColumns = !data || data.size === 0 ? | ||
[] : | ||
data.get(0).keySeq().toJSON(); | ||
|
||
const columnPropertyColumns = (renderProperties && renderProperties.size > 0) ? | ||
// TODO: Make this not so ugly | ||
Object.keys(renderProperties.get('columnProperties').toJSON()) : | ||
[]; | ||
|
||
return _.union(dataColumns, columnPropertyColumns); | ||
} | ||
); | ||
|
||
export const sortedColumnPropertiesSelector = griddleCreateSelector( | ||
"renderPropertiesSelector", | ||
(renderProperties) => ( | ||
renderProperties && renderProperties.get('columnProperties') && renderProperties.get('columnProperties').size !== 0 ? | ||
renderProperties.get('columnProperties') | ||
.sortBy(col => (col && col.get('order'))||MAX_SAFE_INTEGER) : | ||
null | ||
) | ||
); | ||
|
||
export const metaDataColumnsSelector = griddleCreateSelector( | ||
"sortedColumnPropertiesSelector", | ||
(sortedColumnProperties) => ( | ||
sortedColumnProperties ? sortedColumnProperties | ||
.filter(c => c.get('isMetadata')) | ||
.keySeq() | ||
.toJSON() : | ||
[] | ||
) | ||
); | ||
|
||
export const visibleColumnsSelector = griddleCreateSelector( | ||
"sortedColumnPropertiesSelector", | ||
"allColumnsSelector", | ||
(sortedColumnProperties, allColumns) => ( | ||
sortedColumnProperties ? sortedColumnProperties | ||
.filter(c => { | ||
const isVisible = c.get('visible') || c.get('visible') === undefined; | ||
const isMetadata = c.get('isMetadata'); | ||
return isVisible && !isMetadata; | ||
}) | ||
.keySeq() | ||
.toJSON() : | ||
allColumns | ||
) | ||
); | ||
|
||
export const visibleColumnPropertiesSelector = griddleCreateSelector( | ||
"visibleColumnsSelector", | ||
"renderPropertiesSelector", | ||
(visibleColumns=[], renderProperties) => ( | ||
visibleColumns.map(c => { | ||
const columnProperty = renderProperties.getIn(['columnProperties', c]); | ||
return (columnProperty && columnProperty.toJSON()) || { id: c } | ||
}) | ||
) | ||
); | ||
|
||
export const hiddenColumnsSelector = griddleCreateSelector( | ||
"visibleColumnsSelector", | ||
"allColumnsSelector", | ||
"metaDataColumnsSelector", | ||
(visibleColumns, allColumns, metaDataColumns) => { | ||
const removeColumns = [...visibleColumns, ...metaDataColumns]; | ||
|
||
return allColumns.filter(c => removeColumns.indexOf(c) === -1); | ||
} | ||
); | ||
|
||
export const hiddenColumnPropertiesSelector = griddleCreateSelector( | ||
"hiddenColumnsSelector", | ||
"renderPropertiesSelector", | ||
(hiddenColumns=[], renderProperties) => ( | ||
hiddenColumns.map(c => { | ||
const columnProperty = renderProperties.getIn(['columnProperties', c]); | ||
|
||
return (columnProperty && columnProperty.toJSON()) || { id: c } | ||
}) | ||
) | ||
); | ||
|
||
export const columnIdsSelector = griddleCreateSelector( | ||
"renderPropertiesSelector", | ||
"visibleColumnsSelector", | ||
(renderProperties, visibleColumns) => { | ||
const offset = 1000; | ||
// TODO: Make this better -- This is pretty inefficient | ||
return visibleColumns | ||
.map((k, index) => ({ | ||
id: renderProperties.getIn(['columnProperties', k, 'id']) || k, | ||
order: renderProperties.getIn(['columnProperties', k, 'order']) || offset + index | ||
})) | ||
.sort((first, second) => first.order - second.order) | ||
.map(item => item.id); | ||
} | ||
); | ||
|
||
export const columnTitlesSelector = griddleCreateSelector( | ||
"columnIdsSelector", | ||
"renderPropertiesSelector", | ||
(columnIds, renderProperties) => columnIds.map(k => renderProperties.getIn(['columnProperties', k, 'title']) || k) | ||
); | ||
|
||
export const visibleRowIdsSelector = griddleCreateSelector( | ||
"dataSelector", | ||
currentPageData => currentPageData ? currentPageData.map(c => c.get('griddleKey')) : new Immutable.List() | ||
); | ||
|
||
export const visibleRowCountSelector = griddleCreateSelector( | ||
"visibleRowIdsSelector", | ||
(visibleRowIds) => visibleRowIds.size | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Before we dig into reviewing this code, can you pull your whole new block into
./utils/selectorUtils.js
to minimize the churn here? Something like:That should make it a lot easier to test your selector composition logic in isolation.
(Please squash this change, to avoid merge conflicts in this file.)