Skip to content

fix: correct create vs update detection logic in syncedCrud#626

Open
scythewyvern wants to merge 1 commit intoLegendApp:mainfrom
scythewyvern:fix/crud-is-create-condition
Open

fix: correct create vs update detection logic in syncedCrud#626
scythewyvern wants to merge 1 commit intoLegendApp:mainfrom
scythewyvern:fix/crud-is-create-condition

Conversation

@scythewyvern
Copy link

Problem

When using syncedCrud with the list pattern and as: 'array' option, the create vs update detection logic was broken:

  • When adding a new item - update was called instead of create
  • When updating an existing item - create was called instead of update

Fix

// Fixed
const isCreate =
    !pendingCreates.has(item[fieldId]) &&
    (fieldCreatedAt
        ? !prev?.[fieldCreatedAt!]  // check server value
        : fieldUpdatedAt
            ? !item[fieldUpdatedAt] && !prev?.[fieldUpdatedAt!]
            : isNullOrUndefined(prev));

Key change: For fieldCreatedAt, we now check prev (server value) instead of item (local value). This is correct because when creating an item we don't have createdAt from the server, but we may add it locally.

Testing

The issue was reproducible with this config:

syncedCrud({
    as: 'array',
    list: getData,
    create: createEntity,
    update: updateEntity,
    
    fieldCreatedAt: 'createdAt',  // or fieldUpdatedAt
})

After the fix, create/update are called correctly.

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.

1 participant