Fix header

This commit is contained in:
Rene Vergara 2021-10-21 11:22:48 -05:00
parent 4214dac887
commit 7e6cd11962
6 changed files with 78 additions and 66 deletions

View file

@ -12,7 +12,7 @@ import { MatDialogModule } from '@angular/material/dialog';
import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';
//import { PostCreateComponent } from './posts/post-create/post-create.component';
//import { HeaderComponent } from './header/header.component';
import { HeaderComponent } from './header/header.component';
//import { PostListComponent } from './posts/post-list/post-list.component';
//import { PostService } from './posts/posts.service';
import { ViewerComponent } from './viewer/viewer.component';
@ -24,7 +24,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
declarations: [
AppComponent,
//PostCreateComponent,
//HeaderComponent,
HeaderComponent,
ViewerComponent,
LoginComponent,
//NameDialogComponent,

View file

@ -1,19 +1,11 @@
<mat-toolbar color="primary">
<span>
<a routerLink = "/">
ZEC Block Height: {{height}}
ZEC Block Height: {{heightUpdate | async}}
</a>
</span>
<span class="spacer"></span>
<span>
testy
{{shortenZaddr()}}
</span>
<ul>
<li>
<a mat-button routerLink = "/view" routerLinkAcitve = "mat-accent">Viewer</a>
</li>
<li>
<a mat-button routerLink = "/create" routerLinkActive = "mat-accent">New Post</a>
</li>
</ul>
</mat-toolbar>

View file

@ -1,7 +1,7 @@
import {Component, OnInit, OnDestroy} from '@angular/core';
import {FullnodeService} from '../fullnode.service';
import { UserService } from '../user.service';
import {Subscription} from 'rxjs';
import {Subscription, Observable} from 'rxjs';
import {Owner} from '../owner.model';
@ -16,23 +16,26 @@ export class HeaderComponent implements OnInit, OnDestroy {
public height = 0;
private owner: Owner= {_id:'', address: 'none', name:''};
private session: string | null = '';
//private FullnodeSub: Subscription = new Subscription();
private UserSub: Subscription = new Subscription();
public heightUpdate: Observable<number>;
public ownerUpdate: Observable<Owner>;
constructor(
public fullnodeService: FullnodeService,
public userService: UserService
){
this.heightUpdate = fullnodeService.heightUpdate;
this.ownerUpdate = userService.ownerUpdate;
}
ngOnInit(){
this.fullnodeService.getHeight();
this.height = this.fullnodeService.heightUpdate;
this.ownerUpdate.subscribe((owner) => {
this.owner = owner;
});
console.log('Header owner', this.owner);
}
ngOnDestroy(){
//this.FullnodeSub.unsubscribe();
//this.UserSub.unsubscribe();
}
shortenZaddr() {

View file

@ -1,5 +1,5 @@
export interface Owner {
_id: string;
_id?: string;
address: string;
name: string;
}

View file

@ -7,15 +7,26 @@ import {Owner} from './owner.model';
@Injectable({providedIn: 'root'})
export class UserService{
private dataStore: { user: User} = { user: { address: '', session: '', blocktime: 0 }};
private dataStore: { user: User, owner: Owner} = {
user: {
address: '',
session: '',
blocktime: 0
},
owner: {
address: '',
name: ''
}
};
private uZaddr = '';
private oZaddr = '';
private uName = '';
private session: string | null = '';
private _uZaddrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.uZaddr);
private uNameUpdated = new Subject<string>();
private ownerUpdated = new Subject<Owner>();
private _ownerUpdated: BehaviorSubject<Owner> = new BehaviorSubject(this.dataStore.owner);
public readonly uZaddrUpdate: Observable<string> = this._uZaddrUpdated.asObservable();
public readonly ownerUpdate: Observable<Owner> = this._ownerUpdated.asObservable();
constructor(private http: HttpClient){
this.session = localStorage.getItem('s4z_token');
@ -44,22 +55,27 @@ export class UserService{
addUser(address: string, session: string, blocktime: number) {
const user: User={_id: '', address: address, session: session, blocktime: blocktime};
this.http.post<{message: string}>('http://localhost:3000/api/users', user).
subscribe((responseData) => {
let obs = this.http.post<{message: string}>('http://localhost:3000/api/users', user);
obs.subscribe((responseData) => {
console.log(responseData.message);
//this.getOwner(address);
//if (this.oZaddr == '') {
//this.addOwner(address);
//}
this.getOwner(address).subscribe((response) => {
console.log('addUser, checking owner', response);
});
});
return obs;
}
addOwner(address: string) {
const owner: Owner={_id: '', address: address, name: 'Zgo-'.concat(address.substring(0,5))};
this.http.post<{message: string}>('http://localhost:3000/api/addowner', {address: owner.address, name: owner.name}).
subscribe((responseData) => {
let obs = this.http.post<{message: string}>('http://localhost:3000/api/addowner', {address: owner.address, name: owner.name});
obs.subscribe((responseData) => {
console.log(responseData.message);
});
return obs;
}
addOwnerName(address: string, name: string) {
@ -69,50 +85,55 @@ export class UserService{
});
}
getUser(session: string) {
const params = new HttpParams().append('session', session);
console.log(`US: Searching for session ${session} `);
this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'}).
subscribe((UserDataResponse) => {
console.log(UserDataResponse.status);
if (UserDataResponse.status == 200){
this.uZaddr = UserDataResponse.body!.user[0].address;
console.log(`US: Found user, returning zaddr ${this.uZaddr}`);
this._uZaddrUpdated.next(this.uZaddr);
} else {
console.log('US: Did not find user');
this._uZaddrUpdated.next('');
}
});
}
getOwner(address: string) {
const ownParams = new HttpParams().append('address', address);
this.http.get<{message:string, owner: any}>('http://localhost:3000/api/getowner', {params: ownParams, observe: 'response'}).
subscribe((OwnerDataResponse) => {
let obs = this.http.get<{message:string, owner: any}>('http://localhost:3000/api/getowner', {params: ownParams, observe: 'response'});
obs.subscribe((OwnerDataResponse) => {
if (OwnerDataResponse.status == 200) {
this.ownerUpdated.next(OwnerDataResponse.body!.owner[0]);
this.dataStore.owner = OwnerDataResponse.body!.owner[0];
this._ownerUpdated.next(this.dataStore.owner);
} else {
console.log("No owner found, adding");
//this.addOwner(address);
console.log("No owner found");
this.addOwner(address);
}
});
return obs;
}
hasOwner() {
return this.uZaddr === this.oZaddr;
}
getName() {
if (this.session != null) {
this.getUser(this.session);
this._uZaddrUpdated.subscribe((addr: string) => {
console.log(` US: ${addr}`);
this.getOwner(addr);
});
}
}
//getUser(session: string) {
//const params = new HttpParams().append('session', session);
//console.log(`US: Searching for session ${session} `);
//this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'}).
//subscribe((UserDataResponse) => {
//console.log(UserDataResponse.status);
//if (UserDataResponse.status == 200){
//this.uZaddr = UserDataResponse.body!.user[0].address;
//console.log(`US: Found user, returning zaddr ${this.uZaddr}`);
//this._uZaddrUpdated.next(this.uZaddr);
//} else {
//console.log('US: Did not find user');
//this._uZaddrUpdated.next('');
//}
//});
//}
//getName() {
//if (this.session != null) {
//this.getUser(this.session);
//this._uZaddrUpdated.subscribe((addr: string) => {
//console.log(` US: ${addr}`);
//this.getOwner(addr);
//});
//}
//}
getNameUpdateListener() {
return this.uNameUpdated;
@ -122,8 +143,4 @@ export class UserService{
//return this.uZaddrUpdated;
//}
getOwnerUpdateListener() {
return this.ownerUpdated;
}
}

View file

@ -1,4 +1,4 @@
<!--<app-header></app-header>-->
<app-header></app-header>
<div align="center">
<h1>{{message}}</h1>
<br>