fix(plugin-multi-tenant): correctly set doc default value on load (#11018)

When navigating from the list view, with no tenant selected, the
document would load and set the hidden tenant field to the first tenant
option.

This was caused by incorrect logic inside the TenantField useEffect that
sets the value on the field upon load.
This commit is contained in:
Jarrod Flesch
2025-02-06 11:24:06 -05:00
committed by GitHub
parent 5f58daffd0
commit f25acb801c

View File

@@ -24,15 +24,21 @@ export const TenantField = (args: Props) => {
const hasSetValueRef = React.useRef(false) const hasSetValueRef = React.useRef(false)
React.useEffect(() => { React.useEffect(() => {
if (!hasSetValueRef.current && value) { if (!hasSetValueRef.current) {
// set value on load // set value on load
setTenant({ id: value, refresh: unique }) if (value && value !== selectedTenantID) {
setTenant({ id: value, refresh: unique })
} else {
// in the document view, the tenant field should always have a value
const defaultValue =
!selectedTenantID || selectedTenantID === SELECT_ALL
? options[0]?.value
: selectedTenantID
setTenant({ id: defaultValue, refresh: unique })
}
hasSetValueRef.current = true hasSetValueRef.current = true
} else if (selectedTenantID && selectedTenantID === SELECT_ALL && options?.[0]?.value) { } else if ((!value || value !== selectedTenantID) && selectedTenantID !== SELECT_ALL) {
// in the document view, the tenant field should always have a value // Update the field on the document value when the tenant is changed
setTenant({ id: options[0].value, refresh: unique })
} else if ((!value || value !== selectedTenantID) && selectedTenantID) {
// Update the field value when the tenant is changed
setValue(selectedTenantID) setValue(selectedTenantID)
} }
}, [value, selectedTenantID, setTenant, setValue, options, unique]) }, [value, selectedTenantID, setTenant, setValue, options, unique])