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

fix: prevent duplicate ids when updating id of an element with the id of an existing element #2020

Conversation

samatar26
Copy link
Contributor

Fixes #2018

Think I'll probably have to update the sorted_state adapter too, but I'm unfamiliar with redux-toolkit and will need to take a closer look. Any pointers would be great 😛. Also let me know if it's too "expensive" to create a set from the array of ids and then changing it back to an array again.

@codesandbox-ci
Copy link

codesandbox-ci bot commented Feb 10, 2022

This pull request is automatically built and testable in CodeSandbox.

To see build info of the built libraries, click here or the icon next to each commit SHA.

Latest deployment of this branch, based on commit e86ed60:

Sandbox Source
Vanilla Configuration
Vanilla Typescript Configuration
rsk-github-issues-example Configuration
@examples-query-react/basic Configuration
@examples-query-react/advanced Configuration
@examples-action-listener/counter Configuration
beautiful-bash-0xj1xp Issue #2018

@netlify
Copy link

netlify bot commented Feb 10, 2022

Deploy Preview for redux-starter-kit-docs ready!

Name Link
🔨 Latest commit e86ed60
🔍 Latest deploy log https://app.netlify.com/sites/redux-starter-kit-docs/deploys/62c0a13c5c0e210009f0d545
😎 Deploy Preview https://deploy-preview-2020--redux-starter-kit-docs.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.

To edit notification comments on pull requests, go to your Netlify site settings.

@@ -158,7 +158,9 @@ export function createUnsortedStateAdapter<T>(
0

if (didMutateIds) {
state.ids = state.ids.map((id) => newKeys[id] || id)
state.ids = Array.from(
Copy link

@paolorossig paolorossig Feb 10, 2022

Choose a reason for hiding this comment

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

The state.ids iteration is necessary? What do you think about:
state.ids = Object.keys(state.entities) ?

Copy link
Collaborator

Choose a reason for hiding this comment

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

I think that could work, actually. Worth a shot?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated and all the tests are passing too

@@ -158,7 +158,9 @@ export function createUnsortedStateAdapter<T>(
0

if (didMutateIds) {
state.ids = state.ids.map((id) => newKeys[id] || id)
state.ids = Array.from(

Choose a reason for hiding this comment

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

I works well. Here's my suggestion:

Suggested change
state.ids = Array.from(
state.ids = Object.keys(state.entities)

@@ -158,7 +158,9 @@ export function createUnsortedStateAdapter<T>(
0

if (didMutateIds) {
state.ids = state.ids.map((id) => newKeys[id] || id)
state.ids = Array.from(
new Set(state.ids.map((id) => newKeys[id] || id))

Choose a reason for hiding this comment

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

Suggested change
new Set(state.ids.map((id) => newKeys[id] || id))

state.ids = state.ids.map((id) => newKeys[id] || id)
state.ids = Array.from(
new Set(state.ids.map((id) => newKeys[id] || id))
)

Choose a reason for hiding this comment

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

Suggested change
)

@markerikson markerikson force-pushed the fix/duplicate-ids-in-create-entity-adapter branch from a4e7fa3 to e86ed60 Compare July 2, 2022 19:49
@markerikson
Copy link
Collaborator

Just rebased this and updated the tests. Thanks!

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.

Duplicate Ids when updating the id of an element with the id of an existing element
3 participants