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 { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
//import { PostCreateComponent } from './posts/post-create/post-create.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 { PostListComponent } from './posts/post-list/post-list.component';
//import { PostService } from './posts/posts.service'; //import { PostService } from './posts/posts.service';
import { ViewerComponent } from './viewer/viewer.component'; import { ViewerComponent } from './viewer/viewer.component';
@ -24,7 +24,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
declarations: [ declarations: [
AppComponent, AppComponent,
//PostCreateComponent, //PostCreateComponent,
//HeaderComponent, HeaderComponent,
ViewerComponent, ViewerComponent,
LoginComponent, LoginComponent,
//NameDialogComponent, //NameDialogComponent,

View file

@ -1,19 +1,11 @@
<mat-toolbar color="primary"> <mat-toolbar color="primary">
<span> <span>
<a routerLink = "/"> <a routerLink = "/">
ZEC Block Height: {{height}} ZEC Block Height: {{heightUpdate | async}}
</a> </a>
</span> </span>
<span class="spacer"></span> <span class="spacer"></span>
<span> <span>
testy {{shortenZaddr()}}
</span> </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> </mat-toolbar>

View file

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

View file

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

View file

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

View file

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