Connect entity
This component allows you to connect an entity by a using a unique identifier, such as from a URL. It can be used in conjunction with SelectField, as SelectField does not have the capability to select default values.
ConnectEntity.tsx
During the static render, the component retrieves the entity you wish to connect. Subsequently, in the useEffect, the said entity gets linked to the specified field.
export interface ConnectEntityProps {entity: stringwhere: SugaredUniqueWhere | undefinedfield: stringchildren?: ReactNode}export const ConnectEntity = Component<ConnectEntityProps>(({ entity, where, field }) => {const currentEntity = useEntity()const getSubtree = useGetEntitySubTree()useEffect(() => {if (!where || currentEntity.getEntity(field).existsOnServer) {return}const entityToConnect = getSubtree({entity: {entityName: entity,where,},})if (entityToConnect) {currentEntity.connectEntityAtField(field, entityToConnect)}}, [entity, field, currentEntity, where, getSubtree])return null}, ({ entity, where, children, field }) => {if (!where) {return null}return <><HasOne field={field} /><EntitySubTree entity={{ entityName: entity, where }} children={children} /></>})
Usage.tsx
The usage is straightforward. You simply need to specify the field where you want to connect the entity and provide the unique identifier of the entity.
<ConnectEntity field={'site'} entity={'Site'} where={'(code = $site)'} />