From baefb2f06fe3dcc9344d93e3ec2f55f46de7262b Mon Sep 17 00:00:00 2001 From: Konstantin Vasilev Date: Fri, 9 Sep 2022 13:13:54 +0300 Subject: [PATCH] CB-2535 feat(plugin-connections): Add project selector to connection form --- .../src/ConnectionForm/Options/Options.tsx | 41 +++---------- .../ConnectionForm/Options/ProjectSelect.tsx | 60 +++++++++++++++++++ 2 files changed, 67 insertions(+), 34 deletions(-) create mode 100644 webapp/packages/plugin-connections/src/ConnectionForm/Options/ProjectSelect.tsx diff --git a/webapp/packages/plugin-connections/src/ConnectionForm/Options/Options.tsx b/webapp/packages/plugin-connections/src/ConnectionForm/Options/Options.tsx index 1e152733e9..9bbfe58150 100644 --- a/webapp/packages/plugin-connections/src/ConnectionForm/Options/Options.tsx +++ b/webapp/packages/plugin-connections/src/ConnectionForm/Options/Options.tsx @@ -34,9 +34,9 @@ import { import { DatabaseAuthModelsResource, DBDriverResource, isLocalConnection } from '@cloudbeaver/core-connections'; import { useService } from '@cloudbeaver/core-di'; import { useTranslate } from '@cloudbeaver/core-localization'; -import { Project, ProjectsResource, PROJECT_GLOBAL_ID } from '@cloudbeaver/core-projects'; +import { PROJECT_GLOBAL_ID } from '@cloudbeaver/core-projects'; import { usePermission } from '@cloudbeaver/core-root'; -import { CachedMapAllKey, CachedMapEmptyKey, DriverConfigurationType, resourceKeyList } from '@cloudbeaver/core-sdk'; +import { CachedMapEmptyKey, DriverConfigurationType, resourceKeyList } from '@cloudbeaver/core-sdk'; import { useStyles } from '@cloudbeaver/core-theming'; import type { TabContainerPanelComponent } from '@cloudbeaver/core-ui'; import { useAuthenticationAction } from '@cloudbeaver/core-ui'; @@ -44,6 +44,7 @@ import { useAuthenticationAction } from '@cloudbeaver/core-ui'; import { ConnectionFormService } from '../ConnectionFormService'; import type { IConnectionFormProps } from '../IConnectionFormProps'; import { ParametersForm } from './ParametersForm'; +import { ProjectSelect } from './ProjectSelect'; import { useOptions } from './useOptions'; const styles = css` @@ -116,22 +117,6 @@ export const Options: TabContainerPanelComponent = observe optionsHook.setAuthModel(model); }, []); - const projectsLoader = useMapResource(Options, ProjectsResource, CachedMapAllKey); - const projects = (projectsLoader.data as Project[]).filter(p => { - if (!adminPermission && p.id === PROJECT_GLOBAL_ID) { - return false; - } - - return true; - }); - - function handleProjectSelect(projectId: string) { - const project = projectsLoader.resource.get(projectId); - - if (project?.canCreateConnections) { - state.setProject(projectId); - } - } const driverMap = useMapResource( Options, @@ -310,24 +295,12 @@ export const Options: TabContainerPanelComponent = observe > {translate('connections_connection_name')} - project.id} - valueSelector={project => project.name} - titleSelector={project => project.description} - isDisabled={project => !project.canCreateConnections} - searchable={projects.length > 10} - readOnly={readonly || edit || projects.length < 2} + readOnly={readonly || edit} disabled={disabled} - loading={projectsLoader.isLoading()} - tiny - fill - onSelect={handleProjectSelect} - > - {translate('connections_connection_project')} - + onChange={projectId => state.setProject(projectId)} + /> {!config.template && ( void; + readOnly?: boolean; + disabled?: boolean; +} + +export function ProjectSelect(props: Props) { + const translate = useTranslate(); + + const projectsLoader = useMapResource(ProjectSelect, ProjectsResource, CachedMapAllKey); + const projects = projectsLoader.data as Project[]; + + function handleProjectSelect(projectId: string) { + const project = projectsLoader.resource.get(projectId); + + if (project?.canCreateConnections) { + props.onChange(projectId); + } + } + + const possibleOptions = projects.filter(project => project.canCreateConnections); + + return ( + project.id} + valueSelector={project => project.name} + titleSelector={project => project.description} + isDisabled={project => !project.canCreateConnections} + readOnly={props.readOnly || possibleOptions.length <= 1} + searchable={projects.length > 10} + disabled={props.disabled} + loading={projectsLoader.isLoading()} + tiny + fill + onSelect={handleProjectSelect} + > + {translate('connections_connection_project')} + + ); +} \ No newline at end of file