11// Copyright The Linux Foundation and each contributor to CommunityBridge.
22// SPDX-License-Identifier: MIT
33
4- import { Component , Input , EventEmitter , Output , ViewChild , TemplateRef , OnInit } from '@angular/core' ;
4+ import { Component , OnInit , Input , EventEmitter , Output } from '@angular/core' ;
55import { AlertService } from '../../services/alert.service' ;
66import { ClaContributorService } from 'src/app/core/services/cla-contributor.service' ;
77import { ProjectModel } from 'src/app/core/models/project' ;
8- import { UserFromSessionModel , UserFromTokenModel , UserModel } from 'src/app/core/models/user' ;
8+ import { UserModel } from 'src/app/core/models/user' ;
99import { StorageService } from '../../services/storage.service' ;
1010import { AppSettings } from 'src/app/config/app-settings' ;
11- import { NgbModal } from '@ng-bootstrap/ng-bootstrap' ;
12- import { Router } from '@angular/router' ;
1311
1412@Component ( {
1513 selector : 'app-project-title' ,
1614 templateUrl : './project-title.component.html' ,
1715 styleUrls : [ './project-title.component.scss' ]
1816} )
1917export class ProjectTitleComponent implements OnInit {
20- @ViewChild ( 'warningModal' ) warningModal : TemplateRef < any > ;
21-
2218 @Input ( ) projectId : string ;
2319 @Input ( ) userId : string ;
2420 @Output ( ) errorEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
2521 @Output ( ) successEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
26- @Output ( ) setUserIdEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
2722
28- message : string ;
29- redirectUrl : string ;
3023 project = new ProjectModel ( ) ;
31- userFromToken = new UserFromTokenModel ( ) ;
3224 user = new UserModel ( ) ;
33- userFromSession = new UserFromSessionModel ( ) ;
25+
3426 constructor (
3527 private alertService : AlertService ,
3628 private storageService : StorageService ,
3729 private claContributorService : ClaContributorService ,
38- private router : Router
39- ) {
40-
41- }
30+ ) { }
4231
4332 ngOnInit ( ) : void {
4433 const hasGerrit = JSON . parse ( this . storageService . getItem ( AppSettings . HAS_GERRIT ) ) ;
@@ -50,34 +39,47 @@ export class ProjectTitleComponent implements OnInit {
5039 }
5140
5241 validateGithubFlow ( ) {
53- console . log ( 'validateGithubFlow' + this . projectId )
54- if ( this . projectId ) {
55- this . getProject ( ) ;
56- if ( this . userId ) {
57- // get user from user id
58- this . getUser ( ) ;
42+ if ( this . projectId && this . userId ) {
43+ const localProjectId = JSON . parse ( this . storageService . getItem ( AppSettings . PROJECT_ID ) ) ;
44+ const localUserId = JSON . parse ( this . storageService . getItem ( AppSettings . USER_ID ) ) ;
45+ if ( localProjectId !== this . projectId ) {
46+ this . getProject ( ) ;
5947 } else {
60- // get user from token if user id is not present in url
61- this . getUserFromToken ( ) ;
48+ this . successEmitter . emit ( 'Project' ) ;
49+ this . project . project_name = JSON . parse ( this . storageService . getItem ( AppSettings . PROJECT_NAME ) ) ;
50+ }
51+
52+ if ( localUserId !== this . userId ) {
53+ this . getUser ( ) ;
6254 }
6355 } else {
6456 this . errorEmitter . emit ( true ) ;
65- this . alertService . error ( 'Invalid project id in URL' ) ;
57+ this . alertService . error ( 'Invalid project id and user id in URL' ) ;
6658 }
6759 }
6860
6961 getProject ( ) {
70- this . claContributorService . getProject ( this . projectId ) . subscribe (
71- ( response ) => {
72- this . project = response ;
73- this . storageService . setItem ( AppSettings . PROJECT_NAME , this . project . project_name ) ;
74- this . storageService . setItem ( AppSettings . PROJECT_ID , this . projectId ) ;
75- this . storageService . setItem ( AppSettings . PROJECT , this . project ) ;
76- this . successEmitter . emit ( 'Project' ) ;
77- }
78- ) ;
62+ if ( this . projectId ) {
63+ this . claContributorService . getProject ( this . projectId ) . subscribe (
64+ ( response ) => {
65+ this . project = response ;
66+ this . storageService . setItem ( AppSettings . PROJECT_NAME , this . project . project_name ) ;
67+ this . storageService . setItem ( AppSettings . PROJECT_ID , this . projectId ) ;
68+ this . storageService . setItem ( AppSettings . PROJECT , this . project ) ;
69+ this . successEmitter . emit ( 'Project' ) ;
70+ } ,
71+ ( exception ) => {
72+ this . errorEmitter . emit ( true ) ;
73+ this . claContributorService . handleError ( exception ) ;
74+ }
75+ ) ;
76+ } else {
77+ this . errorEmitter . emit ( true ) ;
78+ this . alertService . error ( 'Invalid project id in URL' ) ;
79+ }
7980 }
8081
82+
8183 getUser ( ) {
8284 if ( this . userId ) {
8385 this . claContributorService . getUser ( this . userId ) . subscribe (
@@ -97,50 +99,4 @@ export class ProjectTitleComponent implements OnInit {
9799 this . alertService . error ( 'Invalid user id in URL' ) ;
98100 }
99101 }
100-
101-
102- getUserFromToken ( ) {
103- this . claContributorService . getUserFromToken ( ) . subscribe (
104- ( response ) => {
105- console . log ( 'getUserFromToken response ==>' , response )
106- this . userFromToken = response ;
107- this . storageService . setItem ( AppSettings . USER_ID , this . userFromToken . userID ) ;
108- this . storageService . setItem ( AppSettings . USER , this . userFromToken ) ;
109- this . successEmitter . emit ( 'User' ) ;
110- this . setUserIdEmitter . emit ( this . userFromToken . userID ) ;
111- this . router . navigate ( [ '/cla/project/' + this . projectId + '/user/' + this . userFromToken . userID ] ) ;
112- } ,
113- ( ) => {
114- // If user is not found in token, get user from session
115- this . getUserFromSession ( ) ;
116- }
117- ) ;
118- }
119-
120- getUserFromSession ( ) {
121- this . claContributorService . getUserFromSession ( ) . subscribe (
122- ( response : any ) => {
123- console . log ( 'getUserFromSession response ==>' , response )
124- this . userFromSession = response ;
125- this . storageService . setItem ( AppSettings . USER_ID , this . userFromSession . user_id ) ;
126- this . storageService . setItem ( AppSettings . USER , {
127- dateCreated : this . userFromSession . date_created ,
128- dateModified : this . userFromSession . date_modified ,
129- emails : this . userFromSession . user_emails ,
130- lfEmail : this . userFromSession . lf_email ,
131- lfUsername : this . userFromSession . lf_username ,
132- userID : this . userFromSession . user_id ,
133- username : this . userFromSession . user_name ,
134- version : this . userFromSession . version
135- } ) ;
136- this . successEmitter . emit ( 'User' ) ;
137- } ,
138- ( exception ) => {
139- console . log ( 'getUserFromSession exception ==>' , exception )
140- this . errorEmitter . emit ( true ) ;
141- this . alertService . error ( 'An error occurred while retrieving the GitHub user from the session.' ) ;
142- this . claContributorService . handleError ( exception ) ;
143- }
144- ) ;
145- }
146- }
102+ }
0 commit comments