Sync tenant selector dropdown after tenant save#970
Conversation
|
Preview deployment: https://fix-tenant-selector-refresh.preview.avy-fx.org |
busbyk
left a comment
There was a problem hiding this comment.
My immediate reaction was that this should be done in an afterChange hook but we really don't want to do a full page reload just to get the tenant selector to update so I like this solution. I couldn't think of any situations where this client component wouldn't fire when we'd want it to so 👍
Everything looks good except the failing test 🤘
There was a problem hiding this comment.
I think the renaming of dvac isn't being correctly cleaned up somehow.
I'm getting the following test failure when running e2e tests locally. This only happens when running right after a pnpm seed. If I run this test, get the failure, then run again, it succeeds. So run a fresh seed first.
pnpm seed
pnpm dev
pnpm test:e2e __tests__/e2e/admin/tenant-selector
admin/tenant-selector/tenant-cookie-edge-cases.e2e.spec.ts:86
Description
Automatically refresh the tenant selector dropdown when a tenant is created or updated, eliminating the need to manually refresh the page.
Payload's official multi-tenant plugin has the same limitation.
Why this approach
The
syncTenants()function already exists inTenantSelectionProviderClientand handles re-fetching the tenant list from the API. The challenge was triggering it at the right time after a save.Considered alternatives:
TenantFieldComponentalready exists but runs during form interaction, not after saveafterChangehooks run on the server and can't reach the client-side React contextbeforeDocumentControlsis Payload's built-in slot for injecting invisible components into the document edit view. The component usesuseDocumentInfo().lastUpdateTimewhich Payload updates after every successful save, making it a reliable signal without coupling to save button internals.Related Issues
Fixes part of automating onboarding.md
Key Changes
SyncTenantsOnSaveclient component that watcheslastUpdateTimefrom Payload'suseDocumentInfoand callssyncTenants()when it changesbeforeDocumentControlson the Tenants collectionHow to test
Screenshots / Demo video
Migration Explanation
No migration needed — this is a UI-only change.