Commit c8cde83c authored by Jie Yuan's avatar Jie Yuan

add profile dialog comp

parent 3786f17f
<h2 mat-dialog-title><B>Hi, {{user}}</B></h2>
<div mat-dialog-content >
<mat-list *ngIf="hasToken">
<p mat-line> <B> Your Id: &nbsp; </B>{{apitoken.id}} </p>
<p mat-line matTooltip="{{this.apitoken.token}}" ><B> Your Token: &nbsp; </B> {{shortToken}}
<button (click)="copyToken(this.apitoken.token)" value="click to copy" mat-raised-button >
<mat-icon>format_align_justify</mat-icon>
</button>
<button (click)="onDeleteClick()" mat-raised-button >
<mat-icon>delete</mat-icon>
</button>
</p>
<p mat-line> <B> Validate till: &nbsp; </B> {{eDate}} </p>
</mat-list>
<button *ngIf = "hasToken === false" mat-button (click)="onCreationClick()">Create an API</button>
</div>
<div mat-dialog-actions>
<button mat-button (click)="onNoClick()">OK</button>
</div>
import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { ProfileDialogComponent } from './profile-dialog.component';
describe('ProfileDialogComponent', () => {
let component: ProfileDialogComponent;
let fixture: ComponentFixture<ProfileDialogComponent>;
beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ ProfileDialogComponent ]
})
.compileComponents();
}));
beforeEach(() => {
fixture = TestBed.createComponent(ProfileDialogComponent);
component = fixture.componentInstance;
fixture.detectChanges();
});
it('should create', () => {
expect(component).toBeTruthy();
});
});
import { Component, OnInit , Inject } from '@angular/core';
import {MatDialog, MatDialogRef, MAT_DIALOG_DATA, throwMatDialogContentAlreadyAttachedError} from '@angular/material/dialog';
import {HttpClient} from '@angular/common/http';
import {HttpHeaders} from '@angular/common/http';
import {TableComponent} from '../table/table.component';
import {SharedService} from '../../shared-service';
import { TokenInfo } from 'src/app/model/tokenInfo';
import { resolve } from 'url';
@Component({
selector: 'app-profile-dialog',
templateUrl: './profile-dialog.component.html',
styleUrls: ['./profile-dialog.component.css']
})
export class ProfileDialogComponent implements OnInit {
user: string = "";
hasToken: boolean;
apitoken: TokenInfo;
eDate: Date;
shortToken: string;
constructor ( public dialogRef: MatDialogRef<ProfileDialogComponent>,
public sharedService: SharedService,
private httpClient: HttpClient)
{
const tmp = sessionStorage.getItem('currentUser');
console.log("opened the dialog by " + tmp);
this.user = tmp;
}
onNoClick(): void {
this.dialogRef.close();
}
ngOnInit() {
this.getApiToken()
.then(
(atoken: TokenInfo) => {
this.apitoken = atoken;
if ( this.apitoken === undefined || this.apitoken === null) {
this.hasToken = false;
} else {
this.hasToken = true;
this.shortToken=this.apitoken.token.slice(0,20) + " ... ...";
this.eDate = new Date(this.apitoken.validationDate);
console.log(new Date(this.apitoken.validationDate));
}
}
).catch(
error => {
console.log('No token');
console.log(error);
}
);
// console.log(this.apitoken);
// this.dialogRef.updatePosition({ top: `200px`, right: `400px`});
}
getApiToken() {
console.log('Fetching api token');
const promise = new Promise(
(resolve, reject) => {
/** Gets the sites from the source specified (GOCDB or DPMT) **/
const getUrl = this.sharedService.apiTokenURL;
if ( sessionStorage.getItem('authHeader') !== '') {
const headers = new HttpHeaders( sessionStorage.getItem('authHeader') ? {
'Authorization' : sessionStorage.getItem('authHeader'),
'Content-Type' : 'application/json'
} : {}
);
this.httpClient.get<TokenInfo>(getUrl , {headers : headers })
.subscribe(
(tokeninfo: TokenInfo) => {
// this.apitoken = tokeninfo;
resolve(tokeninfo);
//return new TokenInfo(tokeninfo.id,tokeninfo.token,tokeninfo.validationDate);
// this.apitoken = tokeninfo;
//resolve(tokeninfo);
//console.log(this.token);
},
(error) => {
reject(error);
}
);
}
});
return promise;
}
postApiToken() {
console.log('Creating api token');
const promise = new Promise(
(resolve, reject) => {
/** Gets the sites from the source specified (GOCDB or DPMT) **/
const Url = this.sharedService.apiTokenURL;
if ( sessionStorage.getItem('authHeader') !== '') {
const headers = new HttpHeaders( sessionStorage.getItem('authHeader') ? {
'Authorization' : sessionStorage.getItem('authHeader'),
'Content-Type' : 'application/json'
} : {}
);
//console.log("post header");
//console.log(headers);
this.httpClient.post<TokenInfo>(Url , null ,{headers : headers })
.subscribe(
(tokeninfo: TokenInfo) => {
// this.apitoken = tokeninfo;
resolve(tokeninfo);
// return new TokenInfo(tokeninfo.id,tokeninfo.token,tokeninfo.validationDate);
// this.apitoken = tokeninfo;
//resolve(tokeninfo);
//console.log(this.token);
},
(error) => {
reject(error);
}
);
}
});
return promise;
}
deleteApiToken() {
console.log('deleting api token');
const promise = new Promise(
(resolve, reject) => {
/** Gets the sites from the source specified (GOCDB or DPMT) **/
const Url = this.sharedService.apiTokenURL;
if ( sessionStorage.getItem('authHeader') !== '') {
const headers = new HttpHeaders( sessionStorage.getItem('authHeader') ? {
'Authorization' : sessionStorage.getItem('authHeader'),
'Content-Type' : 'application/json'
} : {}
);
console.log("post header");
console.log(headers);
this.httpClient.delete(Url ,{headers : headers })
.subscribe(
(info: string) => {
// this.apitoken = tokeninfo;
resolve(info);
//return new TokenInfo(tokeninfo.id,tokeninfo.token,tokeninfo.validationDate);
// this.apitoken = tokeninfo;
//resolve(tokeninfo);
//console.log(this.token);
},
(error) => {
reject(error);
}
);
}
});
return promise;
}
copyToken(val: string){
let selBox = document.createElement('textarea');
selBox.style.position = 'fixed';
selBox.style.left = '0';
selBox.style.top = '0';
selBox.style.opacity = '0';
selBox.value = val;
document.body.appendChild(selBox);
selBox.focus();
selBox.select();
document.execCommand('copy');
document.body.removeChild(selBox);
}
onCreationClick(){
console.log("on creation clicked");
this.postApiToken()
.then(
(atoken: TokenInfo) => {
this.apitoken = atoken;
if ( this.apitoken === undefined || this.apitoken === null) {
this.hasToken = false;
} else {
this.hasToken = true;
this.shortToken=this.apitoken.token.slice(0,20) + " ... ...";
this.eDate = new Date(this.apitoken.validationDate);
console.log(new Date(this.apitoken.validationDate));
}
}
).catch(
error => {
console.log('No token created');
console.log(error);
}
);
}
onDeleteClick(){
console.log("on creation clicked");
this.deleteApiToken()
.then(
(info: string) => {
this.hasToken = false;
this.shortToken = "";
this.apitoken = null
}
).catch(
error => {
console.log('No token deleted');
console.log(error);
}
);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment