Skip to content

Commit 8c50fcc

Browse files
substitute native js with ndc-dynamic component
1 parent 21181e7 commit 8c50fcc

2 files changed

Lines changed: 29 additions & 58 deletions

File tree

frontend/src/app/components/connect-db/connect-db.component.html

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,12 @@ <h1 class="mat-h1 connectForm__fullLine">
114114
</div>
115115
}
116116

117-
<ng-container #credentialsFormContainer></ng-container>
117+
<ndc-dynamic
118+
[ndcDynamicComponent]="credentialsFormComponent"
119+
[ndcDynamicInputs]="credentialsFormInputs"
120+
[ndcDynamicOutputs]="credentialsFormOutputs"
121+
[ndcDynamicAttributes]="credentialsFormAttributes"
122+
></ndc-dynamic>
118123

119124
<div *ngIf="db.connectionType === 'agent'" class="connectForm__fullLine instruction">
120125

frontend/src/app/components/connect-db/connect-db.component.ts

Lines changed: 23 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { CdkCopyToClipboard } from '@angular/cdk/clipboard';
22
import { CommonModule } from '@angular/common';
3-
import { Component, ComponentRef, DoCheck, NgZone, OnDestroy, OnInit, Type, ViewChild, ViewContainerRef } from '@angular/core';
3+
import { Component, DoCheck, NgZone, OnInit, Type } from '@angular/core';
44
import { FormsModule, NgForm } from '@angular/forms';
55
import { MatButtonModule } from '@angular/material/button';
66
import { MatButtonToggleModule } from '@angular/material/button-toggle';
@@ -13,6 +13,7 @@ import { MatSlideToggleModule } from '@angular/material/slide-toggle';
1313
import { MatTooltipModule } from '@angular/material/tooltip';
1414
import { Title } from '@angular/platform-browser';
1515
import { Router, RouterModule } from '@angular/router';
16+
import { DynamicAttributesDirective, DynamicModule } from 'ng-dynamic-component';
1617
import { Angulartics2, Angulartics2Module } from 'angulartics2';
1718
import * as ipaddr from 'ipaddr.js';
1819
import posthog from 'posthog-js';
@@ -70,13 +71,13 @@ import { RedisCredentialsFormComponent } from './db-credentials-forms/redis-cred
7071
AlertComponent,
7172
Angulartics2Module,
7273
ConnectionStringValidatorDirective,
74+
DynamicModule,
75+
DynamicAttributesDirective,
7376
],
7477
})
75-
export class ConnectDBComponent implements OnInit, DoCheck, OnDestroy {
78+
export class ConnectDBComponent implements OnInit, DoCheck {
7679
protected posthog = posthog;
7780

78-
@ViewChild('credentialsFormContainer', { read: ViewContainerRef }) credentialsFormContainer: ViewContainerRef;
79-
8081
public isSaas = (environment as any).saas;
8182
public connectionID: string | null = null;
8283
public masterKey: string;
@@ -458,11 +459,7 @@ export class ConnectDBComponent implements OnInit, DoCheck, OnDestroy {
458459
}
459460

460461
ngDoCheck() {
461-
this._updateCredentialsForm();
462-
}
463-
464-
ngOnDestroy() {
465-
this._destroyCredentialsForm();
462+
this._updateCredentialsFormInputs();
466463
}
467464

468465
applyConnectionString() {
@@ -518,59 +515,28 @@ export class ConnectDBComponent implements OnInit, DoCheck, OnDestroy {
518515
return provider;
519516
}
520517

521-
private _credentialsFormRef: ComponentRef<BaseCredentialsFormComponent> | null = null;
522-
private _credentialsFormType: Type<BaseCredentialsFormComponent> | null = null;
523-
private _outputSubscriptions: { switchToAgent?: any; masterKeyChange?: any } = {};
524-
525-
private _updateCredentialsForm() {
526-
if (!this.credentialsFormContainer) {
527-
return;
528-
}
518+
public credentialsFormComponent: Type<BaseCredentialsFormComponent> | null = null;
519+
public credentialsFormInputs: Record<string, any> = {};
520+
public credentialsFormOutputs: Record<string, any> = {
521+
switchToAgent: () => this.switchToAgent(),
522+
masterKeyChange: (key: string) => this.handleMasterKeyChange(key),
523+
};
524+
public credentialsFormAttributes: Record<string, string> = { class: 'credentials-fieldset' };
529525

526+
private _updateCredentialsFormInputs() {
530527
const isConnectionStringMode = this.connectionInputMode === 'connectionString' && this.db.connectionType === 'direct' && !this.db.id;
531528
const targetType = (!isConnectionStringMode && this.db.connectionType === 'direct' && this.credentialsFormMap[this.db.type]) || null;
532529

533-
if (targetType !== this._credentialsFormType) {
534-
this._destroyCredentialsForm();
535-
536-
if (targetType) {
537-
this._credentialsFormRef = this.credentialsFormContainer.createComponent(targetType);
538-
this._credentialsFormType = targetType;
539-
540-
const instance = this._credentialsFormRef.instance;
541-
this._outputSubscriptions.switchToAgent = instance.switchToAgent.subscribe(() => this.switchToAgent());
542-
this._outputSubscriptions.masterKeyChange = instance.masterKeyChange.subscribe((key: string) =>
543-
this.handleMasterKeyChange(key),
544-
);
545-
}
546-
}
547-
548-
if (this._credentialsFormRef) {
549-
const instance = this._credentialsFormRef.instance;
550-
instance.connection = this.db;
551-
instance.submitting = this.submitting;
552-
instance.accessLevel = this.accessLevel;
553-
instance.masterKey = this.masterKey;
554-
instance.readonly = !!((this.accessLevel === 'readonly' || this.db.isTestConnection) && this.db.id);
555-
556-
const hostEl = this._credentialsFormRef.location.nativeElement as HTMLElement;
557-
hostEl.classList.add('credentials-fieldset');
558-
}
559-
}
560-
561-
private _destroyCredentialsForm() {
562-
this._outputSubscriptions.switchToAgent?.unsubscribe();
563-
this._outputSubscriptions.masterKeyChange?.unsubscribe();
564-
this._outputSubscriptions = {};
530+
this.credentialsFormComponent = targetType;
565531

566-
if (this._credentialsFormRef) {
567-
this._credentialsFormRef.destroy();
568-
this._credentialsFormRef = null;
569-
this._credentialsFormType = null;
570-
}
571-
572-
if (this.credentialsFormContainer) {
573-
this.credentialsFormContainer.clear();
532+
if (targetType) {
533+
this.credentialsFormInputs = {
534+
connection: this.db,
535+
submitting: this.submitting,
536+
accessLevel: this.accessLevel,
537+
masterKey: this.masterKey,
538+
readonly: !!((this.accessLevel === 'readonly' || this.db.isTestConnection) && this.db.id),
539+
};
574540
}
575541
}
576542
}

0 commit comments

Comments
 (0)