11import { Subscription , firstValueFrom } from 'rxjs' ;
22
3+ import { HttpBackend , HttpClient , HttpErrorResponse } from '@angular/common/http' ;
34import { Component , OnInit } from '@angular/core' ;
45import { FlexModule } from '@angular/flex-layout' ;
56import { FormGroup , FormsModule , ReactiveFormsModule } from '@angular/forms' ;
@@ -11,9 +12,9 @@ import { ResponseWrapper } from '@models/response.model';
1112import { JsonAPISerializer } from '@services/api/serializer-service' ;
1213import { SERV , ValidationPatterns } from '@services/main.config' ;
1314import { GlobalService } from '@services/main.service' ;
14- import { RequestParamBuilder } from '@services/params/builder-implementation.service' ;
1515import { PreprocessorRoleService } from '@services/roles/binaries/preprocessor-role.service' ;
1616import { AlertService } from '@services/shared/alert.service' ;
17+ import { ConfigService } from '@services/shared/config.service' ;
1718
1819import {
1920 NewEditPreprocessorForm ,
@@ -40,11 +41,14 @@ export class NewEditPreprocessorComponent implements OnInit {
4041
4142 newEditPreprocessorForm : FormGroup < NewEditPreprocessorForm > ;
4243
43- loading : boolean ;
44+ loading = false ;
45+
46+ isLoading = true ;
47+
48+ private httpNoInterceptors : HttpClient ;
4449
4550 /**
4651 * Array to hold subscriptions for cleanup on component destruction.
47- * This prevents memory leaks by unsubscribing from observables when the component is destroyed.
4852 */
4953 subscriptions : Subscription [ ] = [ ] ;
5054
@@ -53,52 +57,71 @@ export class NewEditPreprocessorComponent implements OnInit {
5357 private gs : GlobalService ,
5458 private router : Router ,
5559 private alert : AlertService ,
60+ private cs : ConfigService ,
61+ httpBackend : HttpBackend ,
5662 protected roleService : PreprocessorRoleService
5763 ) {
5864 this . newEditPreprocessorForm = getNewEditPreprocessorForm ( ) ;
59- this . loading = false ;
65+ this . httpNoInterceptors = new HttpClient ( httpBackend ) ;
6066 }
6167
62- ngOnInit ( ) {
63- this . preprocessorId = parseInt ( this . route . snapshot . paramMap . get ( 'id' ) ) ;
64- if ( this . preprocessorId ) {
68+ async ngOnInit ( ) : Promise < void > {
69+ const idFromRoute = this . route . snapshot . paramMap . get ( 'id' ) ;
70+ const parsedId = idFromRoute !== null ? Number ( idFromRoute ) : NaN ;
71+
72+ if ( Number . isFinite ( parsedId ) ) {
73+ // Edit mode
6574 this . isEditMode = true ;
75+ this . preprocessorId = parsedId ;
6676 this . pageTitle = 'Edit Preprocessor' ;
6777 this . submitButtonText = 'Update' ;
6878
69- this . loadPreprocessor ( this . preprocessorId ) ;
79+ try {
80+ await this . loadPreprocessor ( this . preprocessorId ) ;
81+ } catch ( e : unknown ) {
82+ const status = e instanceof HttpErrorResponse ? e . status : undefined ;
83+
84+ if ( status === 403 ) {
85+ void this . router . navigateByUrl ( '/forbidden' ) ;
86+ return ;
87+ }
88+
89+ void this . router . navigateByUrl ( '/not-found' ) ;
90+ return ;
91+ }
7092 }
93+
94+ this . isLoading = false ;
7195 }
7296
7397 /**
7498 * Load preprocessor data from the server and patch the form with the data
7599 * @param preprocessorId ID of the preprocessor to load
76100 */
77- private loadPreprocessor ( preprocessorId : number ) {
78- const params = new RequestParamBuilder ( ) . create ( ) ;
79- this . subscriptions . push (
80- this . gs . get ( SERV . PREPROCESSORS , preprocessorId , params ) . subscribe ( ( response : ResponseWrapper ) => {
81- const preprocessor = new JsonAPISerializer ( ) . deserialize < JPreprocessor > ( {
82- data : response . data ,
83- included : response . included
84- } ) ;
85-
86- this . newEditPreprocessorForm . patchValue ( {
87- name : preprocessor . name ,
88- binaryName : preprocessor . binaryName ,
89- url : preprocessor . url ,
90- keyspaceCommand : preprocessor . keyspaceCommand ,
91- limitCommand : preprocessor . limitCommand ,
92- skipCommand : preprocessor . skipCommand
93- } ) ;
94- } )
95- ) ;
101+ private async loadPreprocessor ( preprocessorId : number ) : Promise < void > {
102+ const url = `${ this . cs . getEndpoint ( ) } ${ SERV . PREPROCESSORS . URL } /${ preprocessorId } ` ;
103+
104+ const response = await firstValueFrom < ResponseWrapper > ( this . httpNoInterceptors . get < ResponseWrapper > ( url ) ) ;
105+
106+ const preprocessor = new JsonAPISerializer ( ) . deserialize < JPreprocessor > ( {
107+ data : response . data ,
108+ included : response . included
109+ } ) ;
110+
111+ this . newEditPreprocessorForm . patchValue ( {
112+ name : preprocessor . name ,
113+ binaryName : preprocessor . binaryName ,
114+ url : preprocessor . url ,
115+ keyspaceCommand : preprocessor . keyspaceCommand ,
116+ limitCommand : preprocessor . limitCommand ,
117+ skipCommand : preprocessor . skipCommand
118+ } ) ;
96119 }
97120
98121 /**
99- * Create new preprocessor upon form submission and redirect to preprocessor page on success
122+ * Create / update preprocessor upon form submission and redirect on success
100123 */
101- async onSubmit ( ) {
124+ async onSubmit ( ) : Promise < void > {
102125 if ( this . newEditPreprocessorForm . invalid ) return ;
103126 this . loading = true ;
104127
@@ -111,7 +134,7 @@ export class NewEditPreprocessorComponent implements OnInit {
111134 skipCommand : this . newEditPreprocessorForm . value . skipCommand
112135 } ;
113136
114- if ( this . isEditMode ) {
137+ if ( this . isEditMode && this . preprocessorId !== null ) {
115138 try {
116139 this . subscriptions . push (
117140 this . gs . update ( SERV . PREPROCESSORS , this . preprocessorId , payload ) . subscribe ( ( ) => {
0 commit comments