@@ -74,7 +74,7 @@ knowledge of the CeCILL-C license and that you accept its terms.
7474import { CdkTextareaAutosize } from '@angular/cdk/text-field' ;
7575import { Component , OnDestroy , OnInit , ViewChild } from '@angular/core' ;
7676import { ActivatedRoute , Router } from '@angular/router' ;
77- import { finalize , Subscription } from 'rxjs' ;
77+ import { finalize , map , Subscription , switchMap } from 'rxjs' ;
7878import { FileService } from '../core/services/file.service' ;
7979import { ToggleSidenavService } from '../core/services/toggle-sidenav.service' ;
8080import { FileNode , FileNodeInsertAttributeParams , FileNodeInsertParams } from '../models/file-node' ;
@@ -108,6 +108,7 @@ export class MainComponent implements OnInit, OnDestroy {
108108 uploadedProfileSelected : ProfileDescription ;
109109
110110 private _routeParamsSubscription : Subscription ;
111+ private _profileLoadingSubscription : Subscription ;
111112
112113 constructor (
113114 public fileService : FileService ,
@@ -118,9 +119,6 @@ export class MainComponent implements OnInit, OnDestroy {
118119 private loaderService : NgxUiLoaderService ,
119120 private router : Router ,
120121 ) {
121- this . uploadedProfileResponse = this . router . getCurrentNavigation ( ) . extras . state as ProfileResponse ;
122- this . uploadedProfileSelected = this . router . getCurrentNavigation ( ) . extras . state as ProfileDescription ;
123-
124122 this . sideNavService . isOpened . subscribe ( ( status ) => {
125123 this . opened = status ;
126124 } ) ;
@@ -133,29 +131,20 @@ export class MainComponent implements OnInit, OnDestroy {
133131 this . fileService . currentTreeLoaded = false ;
134132 this . _routeParamsSubscription = this . route . params . subscribe ( ( params ) => {
135133 const profileId = params . id ;
134+
136135 // If a profileId has been defined, it is retrieved from backend
137136 if ( profileId !== undefined ) {
138- if ( this . uploadedProfileSelected === undefined ) {
139- this . router . navigate ( [ '/pastis/tenant/1' ] , { skipLocationChange : false } ) ;
140- } else {
141- this . fileService . getProfileAndUpdateTree ( this . uploadedProfileSelected ) ;
142- }
137+ this . loadProfileById ( profileId ) ;
143138 } else {
144- // Otherwise we must have an user uploaded profile
145- this . uploadedProfileResponse . id = null ;
146-
147- this . loaderService . start ( ) ;
148- this . profileService
149- . getMetaModel ( this . uploadedProfileResponse . sedaVersion )
150- . pipe (
151- tap ( ( metaModel ) => {
152- this . sedaService . setMetaModel ( metaModel ) ;
153- this . fileService . linkFileNodeToSedaData ( null , [ this . uploadedProfileResponse . profile ] ) ;
154- this . fileService . updateTreeWithProfile ( this . uploadedProfileResponse ) ;
155- } ) ,
156- finalize ( ( ) => this . loaderService . stop ( ) ) ,
157- )
158- . subscribe ( ) ;
139+ // Check for query params to create a new profile
140+ this . route . queryParams . subscribe ( ( queryParams ) => {
141+ if ( queryParams [ 'type' ] && queryParams [ 'version' ] ) {
142+ this . createNewProfile ( queryParams [ 'type' ] , queryParams [ 'version' ] ) ;
143+ } else {
144+ // No valid params, redirect to list
145+ this . router . navigate ( [ '/' ] , { skipLocationChange : false } ) ;
146+ }
147+ } ) ;
159148 }
160149 } ) ;
161150 this . opened = true ;
@@ -193,6 +182,55 @@ export class MainComponent implements OnInit, OnDestroy {
193182 if ( this . _routeParamsSubscription != null ) {
194183 this . _routeParamsSubscription . unsubscribe ( ) ;
195184 }
185+ if ( this . _profileLoadingSubscription != null ) {
186+ this . _profileLoadingSubscription . unsubscribe ( ) ;
187+ }
196188 if ( this . pendingSub ) this . pendingSub . unsubscribe ( ) ;
197189 }
190+
191+ private loadProfileById ( profileId : string ) {
192+ // Unsubscribe from previous profile loading to avoid multiple concurrent requests
193+ if ( this . _profileLoadingSubscription != null ) {
194+ this . _profileLoadingSubscription . unsubscribe ( ) ;
195+ }
196+
197+ // Subscribe to profiles list
198+ this . _profileLoadingSubscription = this . profileService . retrievedProfiles . subscribe ( ( profiles ) => {
199+ if ( ! profiles || profiles . length === 0 ) {
200+ // Profiles not loaded yet, refresh the list
201+ this . profileService . refreshListProfiles ( ) ;
202+ return ;
203+ }
204+
205+ // Find the profile in the list
206+ const profileDescription = profiles . find ( ( p ) => p . id === profileId ) ;
207+ if ( profileDescription ) {
208+ this . fileService . getProfileAndUpdateTree ( profileDescription ) ;
209+ } else {
210+ this . router . navigate ( [ '/' ] , { skipLocationChange : false } ) ;
211+ }
212+ } ) ;
213+ }
214+
215+ private createNewProfile ( profileType : string , profileVersion : string ) {
216+ this . loaderService . start ( ) ;
217+ this . profileService
218+ . createProfile ( '/pastis/profile' , profileType as any , profileVersion as any )
219+ . pipe (
220+ tap ( ( profileResponse ) => {
221+ this . uploadedProfileResponse = profileResponse ;
222+ this . uploadedProfileResponse . id = null ;
223+ } ) ,
224+ switchMap ( ( profileResponse ) =>
225+ this . profileService . getMetaModel ( profileResponse . sedaVersion ) . pipe ( map ( ( metaModel ) => ( { profileResponse, metaModel } ) ) ) ,
226+ ) ,
227+ tap ( ( { profileResponse, metaModel } ) => {
228+ this . sedaService . setMetaModel ( metaModel ) ;
229+ this . fileService . linkFileNodeToSedaData ( null , [ profileResponse . profile ] ) ;
230+ this . fileService . updateTreeWithProfile ( profileResponse ) ;
231+ } ) ,
232+ finalize ( ( ) => this . loaderService . stop ( ) ) ,
233+ )
234+ . subscribe ( ) ;
235+ }
198236}
0 commit comments