1414
1515import { SelectionModel } from '@angular/cdk/collections' ;
1616import { HttpErrorResponse , HttpStatusCode } from '@angular/common/http' ;
17- import { Component , ViewChild , effect } from '@angular/core' ;
17+ import { Component , ViewChild , effect , Inject } from '@angular/core' ;
1818import { MatPaginator , PageEvent } from '@angular/material/paginator' ;
1919import { MatSnackBar } from '@angular/material/snack-bar' ;
2020import { MatTableDataSource } from '@angular/material/table' ;
21- import { Subject , debounceTime } from 'rxjs' ;
21+ import { Subject , debounceTime , take , filter } from 'rxjs' ;
2222import { RegistrarService } from '../registrar/registrar.service' ;
2323import { Domain , DomainListService } from './domainList.service' ;
2424import { RegistryLockComponent } from './registryLock.component' ;
2525import { RegistryLockService } from './registryLock.service' ;
26+ import {
27+ MAT_DIALOG_DATA ,
28+ MatDialog ,
29+ MatDialogRef ,
30+ } from '@angular/material/dialog' ;
31+
32+ interface DomainResponse {
33+ message : string ;
34+ responseCode : string ;
35+ }
36+
37+ interface DomainData {
38+ [ domain : string ] : DomainResponse ;
39+ }
40+
41+ @Component ( {
42+ selector : 'app-response-dialog' ,
43+ template : `
44+ <h2 mat-dialog-title>{{ data.title }}</h2>
45+ <mat-dialog-content [innerHTML]="data.content" />
46+ <mat-dialog-actions>
47+ <button mat-button (click)="onClose()">Close</button>
48+ </mat-dialog-actions>
49+ ` ,
50+ } )
51+ export class ResponseDialogComponent {
52+ constructor (
53+ public dialogRef : MatDialogRef < ReasonDialogComponent > ,
54+ @Inject ( MAT_DIALOG_DATA )
55+ public data : { title : string ; content : string }
56+ ) { }
57+
58+ onClose ( ) : void {
59+ this . dialogRef . close ( ) ;
60+ }
61+ }
62+
63+ @Component ( {
64+ selector : 'app-reason-dialog' ,
65+ template : `
66+ <h2 mat-dialog-title>
67+ Please provide a reason for {{ data.operation }} the domain(s):
68+ </h2>
69+ <mat-dialog-content>
70+ <mat-form-field appearance="outline" style="width:100%">
71+ <textarea matInput [(ngModel)]="reason" rows="4"></textarea>
72+ </mat-form-field>
73+ </mat-dialog-content>
74+ <mat-dialog-actions>
75+ <button mat-button (click)="onCancel()">Cancel</button>
76+ <button mat-button color="warn" (click)="onDelete()" [disabled]="!reason">
77+ Delete
78+ </button>
79+ </mat-dialog-actions>
80+ ` ,
81+ } )
82+ export class ReasonDialogComponent {
83+ reason : string = '' ;
84+
85+ constructor (
86+ public dialogRef : MatDialogRef < ReasonDialogComponent > ,
87+ @Inject ( MAT_DIALOG_DATA )
88+ public data : { operation : 'deleting' | 'suspending' }
89+ ) { }
90+
91+ onDelete ( ) : void {
92+ this . dialogRef . close ( this . reason ) ;
93+ }
94+
95+ onCancel ( ) : void {
96+ this . dialogRef . close ( ) ;
97+ }
98+ }
2699
27100@Component ( {
28101 selector : 'app-domain-list' ,
@@ -55,13 +128,18 @@ export class DomainListComponent {
55128 resultsPerPage = 50 ;
56129 totalResults ?: number = 0 ;
57130
131+ reason : string = '' ;
132+
133+ operationResult : DomainData | undefined ;
134+
58135 @ViewChild ( MatPaginator , { static : true } ) paginator ! : MatPaginator ;
59136
60137 constructor (
61138 protected domainListService : DomainListService ,
62139 protected registrarService : RegistrarService ,
63140 protected registryLockService : RegistryLockService ,
64- private _snackBar : MatSnackBar
141+ private _snackBar : MatSnackBar ,
142+ private dialog : MatDialog
65143 ) {
66144 effect ( ( ) => {
67145 this . pageNumber = 0 ;
@@ -138,6 +216,7 @@ export class DomainListComponent {
138216 onPageChange ( event : PageEvent ) {
139217 this . pageNumber = event . pageIndex ;
140218 this . resultsPerPage = event . pageSize ;
219+ this . selection . clear ( ) ;
141220 this . reloadData ( ) ;
142221 }
143222
@@ -156,7 +235,9 @@ export class DomainListComponent {
156235 if ( ! row ) {
157236 return `${ this . isAllSelected ? 'deselect' : 'select' } all` ;
158237 }
159- return `${ this . selection . isSelected ( row ) ? 'deselect' : 'select' } row ${ row . domainName + 1 } ` ;
238+ return `${ this . selection . isSelected ( row ) ? 'deselect' : 'select' } row ${
239+ row . domainName
240+ } `;
160241 }
161242
162243 private isChecked ( ) : ( ( o1 : Domain , o2 : Domain ) => boolean ) | undefined {
@@ -168,4 +249,60 @@ export class DomainListComponent {
168249 return this . isAllSelected || o1 . domainName === o2 . domainName ;
169250 } ;
170251 }
252+
253+ getOperationMessage ( domain : string ) {
254+ if ( this . operationResult && this . operationResult [ domain ] )
255+ return this . operationResult [ domain ] . message ;
256+ return '' ;
257+ }
258+
259+ sendDeleteRequest ( reason : string ) {
260+ this . isLoading = true ;
261+ this . domainListService
262+ . deleteDomains (
263+ this . selection . selected ,
264+ reason ,
265+ this . registrarService . registrarId ( )
266+ )
267+ . pipe ( take ( 1 ) )
268+ . subscribe ( {
269+ next : ( result : DomainData ) => {
270+ this . isLoading = false ;
271+ const successCount = Object . keys ( result ) . filter ( ( domainName ) =>
272+ result [ domainName ] . responseCode . toString ( ) . startsWith ( '1' )
273+ ) . length ;
274+ const failureCount = Object . keys ( result ) . length - successCount ;
275+ this . dialog . open ( ResponseDialogComponent , {
276+ data : {
277+ title : 'Domain Deletion Results' ,
278+ content : `Successfully deleted - ${ successCount } domain(s)<br/>Failed to delete - ${ failureCount } domain(s)<br/>${
279+ failureCount
280+ ? 'Some domains could not be deleted due to ongoing processes or server errors. '
281+ : ''
282+ } Please check the table for more information.`,
283+ } ,
284+ } ) ;
285+ this . selection . clear ( ) ;
286+ this . operationResult = result ;
287+ this . reloadData ( ) ;
288+ } ,
289+ error : ( err : HttpErrorResponse ) =>
290+ this . _snackBar . open ( err . error || err . message ) ,
291+ } ) ;
292+ }
293+ deleteSelectedDomains ( ) {
294+ const dialogRef = this . dialog . open ( ReasonDialogComponent , {
295+ data : {
296+ operation : 'deleting' ,
297+ } ,
298+ } ) ;
299+
300+ dialogRef
301+ . afterClosed ( )
302+ . pipe (
303+ take ( 1 ) ,
304+ filter ( ( reason ) => ! ! reason )
305+ )
306+ . subscribe ( this . sendDeleteRequest . bind ( this ) ) ;
307+ }
171308}
0 commit comments