@payloadcms/plugin-multi-tenant
Version:
Multi Tenant plugin for Payload
74 lines (73 loc) • 2.38 kB
JavaScript
'use client';
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { RelationshipField, useField } from '@payloadcms/ui';
import React from 'react';
import { useTenantSelection } from '../../providers/TenantSelectionProvider/index.client.js';
import './index.scss';
const baseClass = 'tenantField';
export const TenantField = (args)=>{
const { debug, path, unique } = args;
const { setValue, value } = useField({
path
});
const { options, selectedTenantID, setPreventRefreshOnChange, setTenant } = useTenantSelection();
const hasSetValueRef = React.useRef(false);
React.useEffect(()=>{
if (!hasSetValueRef.current) {
// set value on load
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 || options[0]?.value;
setTenant({
id: defaultValue,
refresh: unique
});
}
hasSetValueRef.current = true;
} else if (!value || value !== selectedTenantID) {
// Update the field on the document value when the tenant is changed
setValue(selectedTenantID);
}
}, [
value,
selectedTenantID,
setTenant,
setValue,
options,
unique
]);
React.useEffect(()=>{
if (!unique) {
setPreventRefreshOnChange(true);
}
return ()=>{
setPreventRefreshOnChange(false);
};
}, [
unique,
setPreventRefreshOnChange
]);
if (debug) {
return /*#__PURE__*/ _jsxs("div", {
className: baseClass,
children: [
/*#__PURE__*/ _jsx("div", {
className: `${baseClass}__wrapper`,
children: /*#__PURE__*/ _jsx(RelationshipField, {
...args
})
}),
/*#__PURE__*/ _jsx("div", {
className: `${baseClass}__hr`
})
]
});
}
return null;
};
//# sourceMappingURL=index.client.js.map