added getLevelName()-function, WIP: added SelfService to check if logged in

master
Max 5 years ago
parent 0d96caa5b0
commit e152148161

@ -1,6 +1,6 @@
<h1>Greenvironment</h1>
<button id="tab-home" routerLink="">Home</button>
<button id="tab-profile" routerLink="/profile" *ngIf="loggedIn">Profile</button>
<button id="tab-profile" routerLink={{profileUrl}} *ngIf="loggedIn">Profile</button>
<button id="tab-rank" *ngIf="loggedIn">Rank</button>
<button id="tab-about" routerLink="/about">About</button>
<button id="tab-imprint" routerLink="/imprint">Imprint</button>

@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { LoginComponent } from '../login/login.component';
import { DatasharingService } from '../../services/datasharing.service';
import { SelfService } from '../../services/selfservice/self.service';
@Component({
selector: 'app-scaffold',
@ -10,13 +11,23 @@ import { DatasharingService } from '../../services/datasharing.service';
export class AppScaffoldComponent implements OnInit {
loggedIn: boolean = false;
constructor(private data: DatasharingService) { }
userId: number;
profileUrl: string;
constructor(private data: DatasharingService,private selfservice: SelfService) { }
ngOnInit() {
this.data.currentUserInfo.subscribe(user => {
this.loggedIn = user.loggedIn;
console.log('he´s comming through!');
this.userId = user.userID;
this.profileUrl = '/profile/' + this.userId;
console.log(user.loggedIn);
})
if(this.loggedIn != true){
console.log('user is not logged in');
this.selfservice.checkIfLoggedIn;
};
console.log('loggedIn is ' + this.loggedIn)
}
}

@ -1,5 +1,4 @@
<div id="content">
<app-scaffold id="headerbar"></app-scaffold>
<home-chatmanager id="chatcontainer"></home-chatmanager>
<home-feed id="feedcontainer"></home-feed>
<home-social id="socialcontainer"></home-social>

@ -2,30 +2,23 @@
@import '../../../styles/vars.sass'
#content
grid-template: 7.5% 92.5% / 25% 50% 25%
grid-template: 100%/ 25% 50% 25%
display: grid
min-height: 100vh
max-height: 100vh
#headerbar
@include gridPosition(1, 2, 1, 4)
display: grid
grid-template: 100% /30% 10% 10% 10% 10% 10% 15% 5%
background-color: $cHeadPrimaryBackground
color: $cHeadFontColor
#chatcontainer
@include gridPosition(2, 3, 1, 2)
@include gridPosition(1, 3, 1, 2)
background-color: $cBoxBodyBackground
#feedcontainer
@include gridPosition(2, 3, 2, 3)
@include gridPosition(1, 3, 2, 3)
display: grid
grid-template: 10% 90% /100%
background-color: $cSecondaryBackground
#socialcontainer
@include gridPosition(2, 3, 3, 4)
@include gridPosition(1, 3, 3, 4)
display: grid
grid-template: 50% 50% /100%
background-color: $cBoxBodyBackground

@ -20,7 +20,7 @@
<td>{{user.points}}</td>
<tr>
<td>level: </td>
<td>{{user.level}}</td>
<td>{{user.level}} ({{rankname}})</td>
</tr>
</table>
<h2>What´s the level?</h2>

@ -15,6 +15,7 @@ export class ProfileComponent implements OnInit {
levellist: Levellist = new Levellist();
user: User = new User()
id : string
rankname: string;
profileNotFound : boolean = false;
constructor(private router: Router,private http: Http) { }
@ -43,6 +44,7 @@ export class ProfileComponent implements OnInit {
this.user.handle = response.data.getUser.handle;
this.user.points = response.data.getUser.points;
this.user.level = response.data.getUser.level;
this.rankname = this.levellist.getLevelName(this.user.level);
this.user.friendIDs = response.data.getUser.friends;
} else{
this.profileNotFound = true;

@ -8,4 +8,18 @@ export class Levellist{
{level: 5, name: "Champion of the Earth", points: 500 },
{level: 6, name: "Intergallactic Superhero", points: 600 },
];
getLevelName(lev:number): any{
var name: string = 'not defined';
console.log(lev);
this.levels.forEach(rank => {
console.log(rank.level);
if(lev == rank.level){
console.log('found matching level');
name = rank.name;
}
});
return name
}
}

@ -0,0 +1,12 @@
import { TestBed } from '@angular/core/testing';
import { SelfService } from './self.service';
describe('Selfservice', () => {
beforeEach(() => TestBed.configureTestingModule({}));
it('should be created', () => {
const service: SelfService = TestBed.get(SelfService);
expect(service).toBeTruthy();
});
});

@ -0,0 +1,69 @@
import { Injectable, EventEmitter, Output } from '@angular/core';
import {Http, URLSearchParams, Headers} from '@angular/http';
import { Login } from '../../models/login';
import { User } from 'src/app/models/user';
import { DatasharingService } from '../datasharing.service';
import { userInfo } from 'os';
import {Router} from '@angular/router';
@Injectable({
providedIn: 'root'
})
export class SelfService {
constructor(private http: Http, private data: DatasharingService,private router: Router) { }
public checkIfLoggedIn() {
console.log('check if logged in...');
//let url = './graphql'
let url = 'https://greenvironment.net/graphql'
let headers = new Headers();
headers.set('Content-Type', 'application/json');
return this.http.post(url, this.buildJson())
.subscribe(response => {
console.log(response.text());
this.stillLoggedIn();
this.updateUserInfo(response.json())
}, error => {
this.notLoggedIn()
console.log(error.text())
}
);
}
public stillLoggedIn(){
console.log('user was logged in');
}
public notLoggedIn(){
console.log('user was not logged in');
//do routing
this.router.navigateByUrl('/login');
}
public updateUserInfo(response : any){
const user: User = new User();
user.loggedIn = true;
user.userID = response.data.login.id;
user.username = response.data.login.name;
user.handle = response.data.login.handle;
user.email = response.data.login.email;
user.points = response.data.login.points;
user.level = response.data.login.level;
user.friendIDs = response.data.login.friends;
user.groupIDs = response.data.login.groups;
user.chatIDs = response.data.login.chats;
user.requestIDs = response.data.login.requests;
this.data.changeUserInfo(user)
}
public buildJson(): any {
const body = {query: `query() {
getSelf(){id, name, handle, points, level, friends{id}, groups{id},chats{id}}
}`, variables: {
}};
return body;
}
}//add ,receivedRequests{id} later
Loading…
Cancel
Save