UNPKG

@payloadcms/plugin-multi-tenant

Version:
81 lines (80 loc) 2.67 kB
'use client'; import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime"; import { RelationshipField, useField, useFormModified } 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, unique } = args; const { setValue, value } = useField(); const modified = useFormModified(); const { options, selectedTenantID, setEntityType: setEntityType, setModified, 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 || value === selectedTenantID); } }, [ value, selectedTenantID, setTenant, setValue, options, unique ]); React.useEffect(()=>{ setEntityType(unique ? 'global' : 'document'); return ()=>{ setEntityType(undefined); }; }, [ unique, setEntityType ]); React.useEffect(()=>{ // sync form modified state with the tenant selection provider context setModified(modified); return ()=>{ setModified(false); }; }, [ modified, setModified ]); 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