Skip to content
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

[FEAT]: Add selectors #98

Merged
merged 1 commit into from
Jul 30, 2018
Merged

[FEAT]: Add selectors #98

merged 1 commit into from
Jul 30, 2018

Conversation

josepot
Copy link
Contributor

@josepot josepot commented Jul 18, 2018

Hi and thanks for this library!

I'm migrating from react-router-redux and the only thing that I'm really missing is its selectors... Specially the createMatchSelector. So, I have taken the ones from react-router-redux and I have migrated them (with their corresponding tests) so that they can work in this library.

I would really appreciate it if you could add them to connected-react-router.

@josepot josepot force-pushed the feat/selectors branch 2 times, most recently from abd4990 to 463ed5c Compare July 18, 2018 15:05
src/selectors.js Outdated

const createSelectors = (structure) => {
const { getIn, toJS } = structure

Copy link
Owner

@supasate supasate Jul 29, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: No new line needed for variable declaration.

src/selectors.js Outdated
const getAction = state => toJS(getIn(state, ['router', 'action']))
const createMatchSelector = path => {
let lastPathname = null
let lastMatch = null
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: new line before return.

const match = matchPath(pathname, path)
if (!match || !lastMatch || match.url !== lastMatch.url) {
lastMatch = match
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: new line before return.

src/selectors.js Outdated

const getLocation = state => toJS(getIn(state, ['router', 'location']))
const getAction = state => toJS(getIn(state, ['router', 'action']))
const createMatchSelector = path => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some comments about the behavior of the matchSelector? So, it will be easier to understand for anyone who read the code. Ex. I need to read twice before I realized that lastPathname and lastMatch are inside a closure and their values are kept in memory, not re-initialized to null everytime.

})
})

describe("getLocation", () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: getAction

})

describe("getLocation", () => {
it("gets the location from the state", () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

expect(() => matchSelector(state)).not.toThrow()
})

it("does not update if the match is the same", () => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For all test cases, instead of comparing match1 with match2, I think it will be easier to understand if we compare each one against its expected value.

Copy link
Owner

@supasate supasate left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall is awesome! Can you address some nits?

@josepot
Copy link
Contributor Author

josepot commented Jul 29, 2018

Hi @supasate ! Thanks for the review, I rushed this a bit when I created the PR, sorry about that. I have addressed all your comments, please let me know if this look "mergeable" now or if you want me to make more changes.

@supasate
Copy link
Owner

LGTM. Thanks for your contribution!

@supasate supasate merged commit 2dfa00e into supasate:master Jul 30, 2018
@josepot josepot deleted the feat/selectors branch July 30, 2018 07:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants