@@ -75,7 +75,7 @@ import { CdkTextareaAutosize } from '@angular/cdk/text-field';
7575import { Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ;
7676import { ActivatedRoute , Router } from '@angular/router' ;
7777import { ToastContainerDirective , ToastrService } from 'ngx-toastr' ;
78- import { finalize , Subscription } from 'rxjs' ;
78+ import { finalize , map , Subscription , switchMap } from 'rxjs' ;
7979import { FileService } from '../core/services/file.service' ;
8080import { ToggleSidenavService } from '../core/services/toggle-sidenav.service' ;
8181import { FileNode , FileNodeInsertAttributeParams , FileNodeInsertParams } from '../models/file-node' ;
@@ -110,6 +110,7 @@ export class MainComponent implements OnInit, OnDestroy {
110110 uploadedProfileSelected : ProfileDescription ;
111111
112112 private _routeParamsSubscription : Subscription ;
113+ private _profileLoadingSubscription : Subscription ;
113114
114115 constructor (
115116 public fileService : FileService ,
@@ -121,9 +122,6 @@ export class MainComponent implements OnInit, OnDestroy {
121122 private loaderService : NgxUiLoaderService ,
122123 private router : Router ,
123124 ) {
124- this . uploadedProfileResponse = this . router . getCurrentNavigation ( ) . extras . state as ProfileResponse ;
125- this . uploadedProfileSelected = this . router . getCurrentNavigation ( ) . extras . state as ProfileDescription ;
126-
127125 this . sideNavService . isOpened . subscribe ( ( status ) => {
128126 this . opened = status ;
129127 } ) ;
@@ -137,29 +135,20 @@ export class MainComponent implements OnInit, OnDestroy {
137135 this . toastrService . overlayContainer = this . toastContainer ;
138136 this . _routeParamsSubscription = this . route . params . subscribe ( ( params ) => {
139137 const profileId = params . id ;
138+
140139 // If a profileId has been defined, it is retrieved from backend
141140 if ( profileId !== undefined ) {
142- if ( this . uploadedProfileSelected === undefined ) {
143- this . router . navigate ( [ '/pastis/tenant/1' ] , { skipLocationChange : false } ) ;
144- } else {
145- this . fileService . getProfileAndUpdateTree ( this . uploadedProfileSelected ) ;
146- }
141+ this . loadProfileById ( profileId ) ;
147142 } else {
148- // Otherwise we must have an user uploaded profile
149- this . uploadedProfileResponse . id = null ;
150-
151- this . loaderService . start ( ) ;
152- this . profileService
153- . getMetaModel ( this . uploadedProfileResponse . sedaVersion )
154- . pipe (
155- tap ( ( metaModel ) => {
156- this . sedaService . setMetaModel ( metaModel ) ;
157- this . fileService . linkFileNodeToSedaData ( null , [ this . uploadedProfileResponse . profile ] ) ;
158- this . fileService . updateTreeWithProfile ( this . uploadedProfileResponse ) ;
159- } ) ,
160- finalize ( ( ) => this . loaderService . stop ( ) ) ,
161- )
162- . subscribe ( ) ;
143+ // Check for query params to create a new profile
144+ this . route . queryParams . subscribe ( ( queryParams ) => {
145+ if ( queryParams [ 'type' ] && queryParams [ 'version' ] ) {
146+ this . createNewProfile ( queryParams [ 'type' ] , queryParams [ 'version' ] ) ;
147+ } else {
148+ // No valid params, redirect to list
149+ this . router . navigate ( [ '/' ] , { skipLocationChange : false } ) ;
150+ }
151+ } ) ;
163152 }
164153 } ) ;
165154 this . opened = true ;
@@ -197,6 +186,55 @@ export class MainComponent implements OnInit, OnDestroy {
197186 if ( this . _routeParamsSubscription != null ) {
198187 this . _routeParamsSubscription . unsubscribe ( ) ;
199188 }
189+ if ( this . _profileLoadingSubscription != null ) {
190+ this . _profileLoadingSubscription . unsubscribe ( ) ;
191+ }
200192 if ( this . pendingSub ) this . pendingSub . unsubscribe ( ) ;
201193 }
194+
195+ private loadProfileById ( profileId : string ) {
196+ // Unsubscribe from previous profile loading to avoid multiple concurrent requests
197+ if ( this . _profileLoadingSubscription != null ) {
198+ this . _profileLoadingSubscription . unsubscribe ( ) ;
199+ }
200+
201+ // Subscribe to profiles list
202+ this . _profileLoadingSubscription = this . profileService . retrievedProfiles . subscribe ( ( profiles ) => {
203+ if ( ! profiles || profiles . length === 0 ) {
204+ // Profiles not loaded yet, refresh the list
205+ this . profileService . refreshListProfiles ( ) ;
206+ return ;
207+ }
208+
209+ // Find the profile in the list
210+ const profileDescription = profiles . find ( ( p ) => p . id === profileId ) ;
211+ if ( profileDescription ) {
212+ this . fileService . getProfileAndUpdateTree ( profileDescription ) ;
213+ } else {
214+ this . router . navigate ( [ '/' ] , { skipLocationChange : false } ) ;
215+ }
216+ } ) ;
217+ }
218+
219+ private createNewProfile ( profileType : string , profileVersion : string ) {
220+ this . loaderService . start ( ) ;
221+ this . profileService
222+ . createProfile ( '/pastis/profile' , profileType as any , profileVersion as any )
223+ . pipe (
224+ tap ( ( profileResponse ) => {
225+ this . uploadedProfileResponse = profileResponse ;
226+ this . uploadedProfileResponse . id = null ;
227+ } ) ,
228+ switchMap ( ( profileResponse ) =>
229+ this . profileService . getMetaModel ( profileResponse . sedaVersion ) . pipe ( map ( ( metaModel ) => ( { profileResponse, metaModel } ) ) ) ,
230+ ) ,
231+ tap ( ( { profileResponse, metaModel } ) => {
232+ this . sedaService . setMetaModel ( metaModel ) ;
233+ this . fileService . linkFileNodeToSedaData ( null , [ profileResponse . profile ] ) ;
234+ this . fileService . updateTreeWithProfile ( profileResponse ) ;
235+ } ) ,
236+ finalize ( ( ) => this . loaderService . stop ( ) ) ,
237+ )
238+ . subscribe ( ) ;
239+ }
202240}
0 commit comments