11// Copyright The Linux Foundation and each contributor to CommunityBridge.
22// SPDX-License-Identifier: MIT
33
4- import { Component , OnInit , Input , EventEmitter , Output } from '@angular/core' ;
4+ import { Component , OnInit , Input , EventEmitter , Output , ViewChild , TemplateRef } 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 { UserModel } from 'src/app/core/models/user' ;
8+ import { UserFromSessionModel , UserFromTokenModel } 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' ;
1112
1213@Component ( {
1314 selector : 'app-project-title' ,
1415 templateUrl : './project-title.component.html' ,
1516 styleUrls : [ './project-title.component.scss' ]
1617} )
1718export class ProjectTitleComponent implements OnInit {
19+ @ViewChild ( 'warningModal' ) warningModal : TemplateRef < any > ;
20+
1821 @Input ( ) projectId : string ;
1922 @Input ( ) userId : string ;
2023 @Output ( ) errorEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
2124 @Output ( ) successEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
25+ @Output ( ) setUserIdEmitter : EventEmitter < any > = new EventEmitter < any > ( ) ;
2226
27+ message : string ;
28+ redirectUrl : string ;
2329 project = new ProjectModel ( ) ;
24- user = new UserModel ( ) ;
25-
30+ user = new UserFromTokenModel ( ) ;
31+ userFromSession = new UserFromSessionModel ( ) ;
2632 constructor (
2733 private alertService : AlertService ,
2834 private storageService : StorageService ,
2935 private claContributorService : ClaContributorService ,
36+ private modalService : NgbModal
3037 ) { }
3138
3239 ngOnInit ( ) : void {
@@ -39,68 +46,74 @@ export class ProjectTitleComponent implements OnInit {
3946 }
4047
4148 validateGithubFlow ( ) {
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 ) {
49+ if ( this . projectId ) {
4650 this . getProject ( ) ;
47- } else {
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 ) {
5351 this . getUser ( ) ;
54- }
5552 } else {
5653 this . errorEmitter . emit ( true ) ;
57- this . alertService . error ( 'Invalid project id and user id in URL' ) ;
54+ this . alertService . error ( 'Invalid project id in URL' ) ;
5855 }
5956 }
6057
6158 getProject ( ) {
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- }
59+ console . log ( this . projectId )
60+ this . claContributorService . getProject ( this . projectId ) . subscribe (
61+ ( response ) => {
62+ this . project = response ;
63+ this . storageService . setItem ( AppSettings . PROJECT_NAME , this . project . project_name ) ;
64+ this . storageService . setItem ( AppSettings . PROJECT_ID , this . projectId ) ;
65+ this . storageService . setItem ( AppSettings . PROJECT , this . project ) ;
66+ this . successEmitter . emit ( 'Project' ) ;
67+ } ,
68+ ( exception ) => {
69+ this . errorEmitter . emit ( true ) ;
70+ this . claContributorService . handleError ( exception ) ;
71+ }
72+ ) ;
8073 }
8174
8275
8376 getUser ( ) {
84- if ( this . userId ) {
85- this . claContributorService . getUser ( this . userId ) . subscribe (
77+ this . claContributorService . getUserFromToken ( ) . subscribe (
8678 ( response ) => {
79+ console . log ( 'getUserFromToken response ==>' , response )
8780 this . user = response ;
88- this . storageService . setItem ( AppSettings . USER_ID , this . userId ) ;
81+ this . storageService . setItem ( AppSettings . USER_ID , this . user . userID ) ;
8982 this . storageService . setItem ( AppSettings . USER , this . user ) ;
9083 this . successEmitter . emit ( 'User' ) ;
84+ this . setUserIdEmitter . emit ( this . user . userID ) ;
9185 } ,
92- ( exception ) => {
93- this . errorEmitter . emit ( true ) ;
94- this . claContributorService . handleError ( exception ) ;
86+ ( ) => {
87+ // If user is not found in token, get user from session
88+ this . getUserFromSession ( ) ;
9589 }
9690 ) ;
97- } else {
98- this . errorEmitter . emit ( true ) ;
99- this . alertService . error ( 'Invalid user id in URL' ) ;
100- }
10191 }
10292
103-
104-
105-
93+ getUserFromSession ( ) {
94+ this . claContributorService . getUserFromSession ( ) . subscribe (
95+ ( response : any ) => {
96+ console . log ( 'getUserFromSession response ==>' , response )
97+ this . userFromSession = response ;
98+ this . storageService . setItem ( AppSettings . USER_ID , this . userFromSession . user_id ) ;
99+ this . storageService . setItem ( AppSettings . USER , {
100+ dateCreated : this . userFromSession . date_created ,
101+ dateModified : this . userFromSession . date_modified ,
102+ emails : this . userFromSession . user_emails ,
103+ lfEmail : this . userFromSession . lf_email ,
104+ lfUsername : this . userFromSession . lf_username ,
105+ userID : this . userFromSession . user_id ,
106+ username : this . userFromSession . user_name ,
107+ version : this . userFromSession . version
108+ } ) ;
109+ this . successEmitter . emit ( 'User' ) ;
110+ } ,
111+ ( exception ) => {
112+ console . log ( 'getUserFromSession exception ==>' , exception )
113+ this . errorEmitter . emit ( true ) ;
114+ this . alertService . error ( 'An error occurred while retrieving the GitHub user from the session.' ) ;
115+ this . claContributorService . handleError ( exception ) ;
116+ }
117+ ) ;
118+ }
106119}
0 commit comments