Merge pull request #190 from dbeaver/refactor/navigation-tree

fix(core-connections): throw error when required connection not established
This commit is contained in:
Alexey
2020-10-18 01:10:05 +03:00
committed by GitHub
5 changed files with 23 additions and 13 deletions
@@ -8,7 +8,7 @@
import { observer } from 'mobx-react';
import {
MouseEvent, KeyboardEvent, useCallback, PropsWithChildren
MouseEvent, useCallback, PropsWithChildren
} from 'react';
import styled, { use } from 'reshadow';
@@ -58,8 +58,8 @@ export const NavigationNode = observer(function NavigationNode({
);
const handleEnter = useCallback(
(event: KeyboardEvent<HTMLDivElement>) => {
switch (event.key) {
(event: React.KeyboardEvent<HTMLDivElement>) => {
switch ((event as unknown as KeyboardEvent).code) {
case KEY.ENTER:
controller.handleSelect(event.ctrlKey);
break;
@@ -89,7 +89,12 @@ export class NavigationTreeService {
const node = this.navNodeManagerService.getNode(navNodeId);
if (node?.objectFeatures.includes(EObjectFeature.dataSource)) {
await this.connectionAuthService.auth(NodeManagerUtils.connectionNodeIdToConnectionId(navNodeId));
const connection = await this.connectionAuthService.auth(
NodeManagerUtils.connectionNodeIdToConnectionId(navNodeId)
);
if (!connection.connected) {
throw new Error('Connection not established');
}
}
}
}
@@ -9,7 +9,7 @@
import { injectable } from '@cloudbeaver/core-di';
import { CommonDialogService } from '@cloudbeaver/core-dialogs';
import { ConnectionInfoResource } from './ConnectionInfoResource';
import { Connection, ConnectionInfoResource } from './ConnectionInfoResource';
import { DatabaseAuthDialog } from './DatabaseAuthDialog/DatabaseAuthDialog';
@injectable()
@@ -19,7 +19,7 @@ export class ConnectionAuthService {
private commonDialogService: CommonDialogService
) {}
async auth(connectionId: string) {
async auth(connectionId: string): Promise<Connection> {
let connection = this.connectionInfoResource.get(connectionId);
if (!connection?.connected) {
@@ -59,7 +59,7 @@ export const DatabaseAuthDialog = observer(function DatabaseAuthDialog({
payload,
options,
rejectDialog,
}: DialogComponentProps<string, null>) {
}: DialogComponentProps<string>) {
const connection = useConnectionInfo(payload);
const [focusedRef] = useFocus<HTMLFormElement>({ focusFirstChild: true });
const { driver } = useDBDriver(connection.connectionInfo?.driverId || '');
@@ -14,16 +14,21 @@ export interface DialogOptions {
persistent?: boolean;
}
export interface DialogComponentProps<TPayload, TResult> {
export interface DialogComponentProps<TPayload, TResult = DialogueStateResult> {
payload: TPayload;
options?: DialogOptions;
resolveDialog: (result: TResult | null) => void;
resolveDialog: (result: TResult) => void;
rejectDialog: () => void; // the dialog was closed by cancel button or backdrop click
className?: string;
}
export type DialogComponent<TPayload, TResult> = React.ElementType<DialogComponentProps<TPayload, TResult>>;
export enum DialogueStateResult {
Resolved,
Rejected
}
export interface DialogInternal {
component: DialogComponent<any, any>;
payload: any;
@@ -35,12 +40,12 @@ export interface DialogInternal {
export class CommonDialogService {
dialogs: DialogInternal[] = observable([]);
// note that if dialog is closed by user it will be resolved with null
// note that if dialog is closed by user it will be resolved with DialogueStateResult.Rejected
async open<TPayload, TResult>(
component: DialogComponent<TPayload, TResult>,
payload: TPayload,
options?: DialogOptions
): Promise<TResult | null> {
): Promise<TResult | DialogueStateResult> {
return new Promise<TResult>((resolve, reject) => {
const dialogInternal: DialogInternal = {
component,
@@ -53,12 +58,12 @@ export class CommonDialogService {
}
rejectDialog(dialog: DialogInternal): void {
dialog.resolve(null);
dialog.resolve(DialogueStateResult.Rejected);
this.removeDialog(dialog);
}
resolveDialog<TResult>(dialog: DialogInternal, result: TResult): void {
dialog.resolve(result);
dialog.resolve(result ?? DialogueStateResult.Resolved);
this.removeDialog(dialog);
}