Add levels are loaded from the backend now

master
Max 5 years ago
parent 8845d591e9
commit 0bb2fcf8d7

@ -20,7 +20,7 @@
<table mat-table [dataSource]="levelSource" class="mat-elevation-z8"> <table mat-table [dataSource]="levelSource" class="mat-elevation-z8">
<ng-container matColumnDef="level"> <ng-container matColumnDef="level">
<th mat-header-cell *matHeaderCellDef> level</th> <th mat-header-cell *matHeaderCellDef> level</th>
<td mat-cell *matCellDef="let level"> {{level.level}} </td> <td mat-cell *matCellDef="let level"> {{level.levelNumber}} </td>
</ng-container> </ng-container>
<ng-container matColumnDef="name"> <ng-container matColumnDef="name">
<th mat-header-cell *matHeaderCellDef> level name</th> <th mat-header-cell *matHeaderCellDef> level name</th>

@ -1,6 +1,6 @@
import {Component, OnInit, ViewChild} from '@angular/core'; import {Component, OnInit, ViewChild} from '@angular/core';
import {Activitylist} from 'src/app/models/activity'; import {Activitylist} from 'src/app/models/activity';
import {Levellist} from 'src/app/models/levellist'; import {LevelList} from 'src/app/models/levellist';
import {MatSort} from '@angular/material/sort'; import {MatSort} from '@angular/material/sort';
import {MatTableDataSource} from '@angular/material/table'; import {MatTableDataSource} from '@angular/material/table';
import {ActivityService} from 'src/app/services/activity/activity.service'; import {ActivityService} from 'src/app/services/activity/activity.service';
@ -12,12 +12,12 @@ import {ActivityService} from 'src/app/services/activity/activity.service';
}) })
export class AboutComponent implements OnInit { export class AboutComponent implements OnInit {
actionlist: Activitylist = new Activitylist(); actionlist: Activitylist = new Activitylist();
levellist: Levellist = new Levellist(); levellist: LevelList = new LevelList();
displayedColumns = ['points', 'name', 'description']; displayedColumns = ['points', 'name', 'description'];
dataSource = new MatTableDataSource(this.actionlist.Actions); dataSource = new MatTableDataSource(this.actionlist.Actions);
displayedLevelColumns = ['level', 'name']; displayedLevelColumns = ['level', 'name'];
levelSource = this.levellist.levels; levelSource = new MatTableDataSource(this.levellist.levels);
constructor(private activityService: ActivityService) { constructor(private activityService: ActivityService) {
} }
@ -31,6 +31,12 @@ export class AboutComponent implements OnInit {
this.dataSource = new MatTableDataSource(this.actionlist.Actions); this.dataSource = new MatTableDataSource(this.actionlist.Actions);
this.dataSource.sort = this.sort; this.dataSource.sort = this.sort;
}); });
this.activityService.getLevels();
this.activityService.levelList.subscribe(response => {
this.levellist = response;
this.levelSource = new MatTableDataSource(this.levellist.levels);
this.levelSource.sort = this.sort;
});
} }
} }

@ -4,7 +4,6 @@ import {DatasharingService} from '../../services/datasharing.service';
import {RequestService} from '../../services/request/request.service'; import {RequestService} from '../../services/request/request.service';
import {SettingsService} from '../../services/settings/settings.service'; import {SettingsService} from '../../services/settings/settings.service';
import {environment} from 'src/environments/environment'; import {environment} from 'src/environments/environment';
import {Levellist} from 'src/app/models/levellist';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
import {User} from 'src/app/models/user'; import {User} from 'src/app/models/user';
import {OverlayContainer} from '@angular/cdk/overlay'; import {OverlayContainer} from '@angular/cdk/overlay';
@ -33,7 +32,6 @@ export class MainNavigationComponent implements OnInit {
userId: number; userId: number;
username: string; username: string;
user: User; user: User;
levellist: Levellist = new Levellist();
level: string; level: string;
points: number; points: number;
profileUrl = '/profile/1'; profileUrl = '/profile/1';

@ -1,7 +1,7 @@
import {Component, OnInit} from '@angular/core'; import {Component, OnInit} from '@angular/core';
import {NavigationEnd, Router} from '@angular/router'; import {NavigationEnd, Router} from '@angular/router';
import {User} from 'src/app/models/user'; import {User} from 'src/app/models/user';
import {Levellist} from 'src/app/models/levellist'; import {LevelList} from 'src/app/models/levellist';
import {RequestService} from 'src/app/services/request/request.service'; import {RequestService} from 'src/app/services/request/request.service';
import {DatasharingService} from '../../services/datasharing.service'; import {DatasharingService} from '../../services/datasharing.service';
import {ProfileService} from 'src/app/services/profile/profile.service'; import {ProfileService} from 'src/app/services/profile/profile.service';
@ -18,7 +18,7 @@ import {Lightbox} from 'ngx-lightbox';
styleUrls: ['./profile.component.sass'] styleUrls: ['./profile.component.sass']
}) })
export class ProfileComponent implements OnInit { export class ProfileComponent implements OnInit {
levellist: Levellist = new Levellist(); levellist: LevelList = new LevelList();
ownProfile = false; ownProfile = false;
userProfile: User = new User(); userProfile: User = new User();
self: User; self: User;

@ -1,5 +1,3 @@
import {Levellist} from 'src/app/models/levellist';
export class FriendInfo { export class FriendInfo {
id: number; id: number;
name: string; name: string;

@ -1,23 +1,27 @@
export class Levellist { export class Level {
levels: { level: number, name: string, points: number }[] = [ id: number;
{level: 0, name: 'Green Horn', points: 0}, name: string;
{level: 1, name: 'Good Willed', points: 100}, levelNumber: number;
{level: 2, name: 'Helper', points: 200}, points: number;
{level: 3, name: 'World Saver', points: 300},
{level: 4, name: 'Hero of the Green Country', points: 400},
{level: 5, name: 'Champion of the Earth', points: 500},
{level: 6, name: 'Intergallactic Superhero', points: 600},
];
getLevelName(level: number): any { constructor(id: number, name: string, levelNumber: number, points: number) {
let name = 'not defined'; this.id = id;
this.name = name;
this.levelNumber = levelNumber;
this.points = points;
}
}
export class LevelList {
levels: Level[] = [];
getLevelName(level: number): string {
let name = 'not defined';
for (const rank of this.levels) { for (const rank of this.levels) {
if (level === rank.level) { if (level === rank.levelNumber) {
name = rank.name; name = rank.name;
} }
} }
return name; return name;
} }
} }

@ -1,18 +1,40 @@
import {Injectable} from '@angular/core'; import {Injectable} from '@angular/core';
import {BehaviorSubject} from 'rxjs'; import {BehaviorSubject} from 'rxjs';
import {Activity, Activitylist} from 'src/app/models/activity'; import {Activity, Activitylist} from 'src/app/models/activity';
import {Level, LevelList} from 'src/app/models/levellist';
import {environment} from 'src/environments/environment'; import {environment} from 'src/environments/environment';
import {Http} from '@angular/http'; import {HttpClient, HttpErrorResponse} from '@angular/common/http';
import {BaseService} from '../base.service';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class ActivityService { export class ActivityService extends BaseService {
public activitylist = new BehaviorSubject<Activitylist>(new Activitylist()); public activitylist = new BehaviorSubject<Activitylist>(new Activitylist());
public levelList = new BehaviorSubject<LevelList>(new LevelList());
constructor(private http: Http) { constructor(http: HttpClient) {
super(http);
}
private static buildGetActivityBody(): any {
const body = {
query: `query{getActivities{
id name description points
}}`, variables: {}
};
return body;
}
private static buildGetLevelsBody(): any {
const body = {
query: `query{getLevels{
id name levelNumber points
}}`, variables: {}
};
return body;
} }
changeUserInfo(pActivitylist: Activitylist) { changeUserInfo(pActivitylist: Activitylist) {
@ -21,22 +43,24 @@ export class ActivityService {
public getActivities() { public getActivities() {
if (this.activitylist.getValue().Actions.length < 1) { if (this.activitylist.getValue().Actions.length < 1) {
const headers = new Headers(); this.http.post(environment.graphQLUrl, ActivityService.buildGetActivityBody(), {headers: this.headers})
headers.set('Content-Type', 'application/json'); .pipe(this.retryRated())
this.http.post(environment.graphQLUrl, this.buildJson()).subscribe(result => { .subscribe(result => {
// push onto subject // push onto subject
this.activitylist.next(this.renderActivity(result.json())); this.activitylist.next(this.renderActivity(result));
}); });
} }
} }
public buildJson(): any { public getLevels() {
const body = { if (this.activitylist.getValue().Actions.length < 1) {
query: `query{getActivities{ this.http.post(environment.graphQLUrl, ActivityService.buildGetLevelsBody(), {headers: this.headers})
id name description points .pipe(this.retryRated())
}}`, variables: {} .subscribe(result => {
}; // push onto subject
return body; this.levelList.next(this.renderLevels(result));
});
}
} }
public renderActivity(response: any): Activitylist { public renderActivity(response: any): Activitylist {
@ -50,5 +74,17 @@ export class ActivityService {
} }
return activitylist; return activitylist;
} }
public renderLevels(response: any): LevelList {
const levelList = new LevelList();
for (const level of response.data.getLevels) {
levelList.levels.push(new Level(
level.id,
level.name,
level.levelNumber,
level.points));
}
return levelList;
}
} }

Loading…
Cancel
Save