payload icon indicating copy to clipboard operation
payload copied to clipboard

Admin UI doesn't update values changed in hooks

Open suphon-t opened this issue 1 year ago • 6 comments

Describe the Bug

When saving a document, the admin UI doesn't update the form state with new values from the server. This causes it to be stale if there's a beforeChange field hook that changes the value.

This only happens in the edit page. In the create page, the changed field gets updated correctly. I found this issue while migrating my project from Payload 2 that depended on the fields being updated.

Previously, I thought that #9573 would fix this, but it seems that PR only merges the error states and not values, and only does so when autosave is disabled. If this is intended, could it be made an option to also update server values as well, or allow overriding the form submit behavior?

Currently I'm reloading the whole page on save, but that's very slow so it's not ideal for me.

Link to the code that reproduces this issue

https://github.com/suphon-t/payload/blob/reproduce/admin-ui-stale-fields/test/_community/e2e.spec.ts

Reproduction Steps

In these steps, I have a posts collection with title and slug field. The slug field has a beforeChange hook that takes the value from title, and replaces spaces with -.

Working: When saving from the create page, the form gets updated with server values

  1. Go to the create post page
  2. Fill the post title, and click publish
  3. The slug field gets a value computed from the title

https://github.com/user-attachments/assets/a1d3644b-6312-4831-bf3e-aae5821b4862

Broken: When saving from the edit page, the form doesn't get updated with the new values from the server

  1. Create a post with a title and go to the edit page
  2. Change the post title to something else, and save
  3. The slug field stays the same
  4. Refresh the page
  5. The slug field is changed to the new value

https://github.com/user-attachments/assets/fccb86a8-8863-4be2-a4ad-8241f5dd6118

Old behavior in Payload 2

Using the same steps for the edit page, the values is updated after saving in Payload 2.

https://github.com/user-attachments/assets/3537b82c-0998-48dc-a3c0-73a2c24263e6

https://github.com/suphon-t/payload/blob/reproduce/admin-ui-stale-fields-2.x/test/_community/e2e.spec.ts

Which area(s) are affected? (Select all that apply)

area: ui

Environment Info

Binaries:
  Node: 20.10.0
  npm: 10.2.3
  Yarn: 4.0.2
  pnpm: 9.14.4
Relevant Packages:
  payload: 3.2.2
  next: 15.0.3
  @payloadcms/db-postgres: 3.2.2
  @payloadcms/email-nodemailer: 3.2.2
  @payloadcms/graphql: 3.2.2
  @payloadcms/next/utilities: 3.2.2
  @payloadcms/payload-cloud: 3.2.2
  @payloadcms/plugin-cloud-storage: 3.2.2
  @payloadcms/plugin-seo: 3.2.2
  @payloadcms/richtext-lexical: 3.2.2
  @payloadcms/storage-s3: 3.2.2
  @payloadcms/translations: 3.2.2
  @payloadcms/ui/shared: 3.2.2
  react: 19.0.0-rc-66855b96-20241106
  react-dom: 19.0.0-rc-66855b96-20241106
Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 24.2.0: Sun Nov  3 20:57:51 PST 2024; root:xnu-11215.60.405~54/RELEASE_ARM64_T8112
  Available memory (MB): 16384
  Available CPU cores: 8

suphon-t avatar Dec 03 '24 10:12 suphon-t

I'm also getting this issue with a simple beforeChange hook that tries to clear a field value depending on a checkbox state

HQ92 avatar Dec 09 '24 13:12 HQ92

Still a problem in 3.5.0 - with Field and Collection hooks, specifically beforeValidate and beforeChange. A manual refresh is always needed.

PeakVisionMediaKLG avatar Dec 09 '24 22:12 PeakVisionMediaKLG

Yes, I also have the exact same problem.

devdenmak avatar Dec 10 '24 15:12 devdenmak

I have the same problem as well.

bebejane avatar Dec 14 '24 09:12 bebejane

This issue is still relevant with 3.8.0

andershermansen avatar Dec 18 '24 08:12 andershermansen

+1

PP-Tom avatar Dec 19 '24 11:12 PP-Tom

Also having this issue

plumduffer avatar Dec 22 '24 22:12 plumduffer

When using autosave:false, sometimes it doesn't refresh as well. When combined with array field, things get even worse and it removes fields from the UI randomly.

zabatonni avatar Jan 02 '25 14:01 zabatonni

Hi all, this will be fixed in the next release!

jmikrut avatar Jan 03 '25 19:01 jmikrut

This issue has been automatically locked. Please open a new issue if this issue persists with any additional detail.

github-actions[bot] avatar Jan 05 '25 04:01 github-actions[bot]