added friend request button on profile page

master
Max 5 years ago
parent 73f37bad44
commit 91ebff182a

@ -8,6 +8,14 @@
<div mat-card-avatar class="profile-picture"></div> <div mat-card-avatar class="profile-picture"></div>
<mat-card-title>{{user.username}}</mat-card-title> <mat-card-title>{{user.username}}</mat-card-title>
<mat-card-subtitle>{{user.handle}}</mat-card-subtitle> <mat-card-subtitle>{{user.handle}}</mat-card-subtitle>
<div class="icon-box">
<button mat-icon-button
class="request-button"
(click)="sendFriendRequest(user)"
[disabled]="!user.allowedToSendRequest">
<mat-icon>person_add</mat-icon>
</button>
</div>
</mat-card-header> </mat-card-header>
<mat-card-content> <mat-card-content>
<table id="profile-table"> <table id="profile-table">

@ -16,6 +16,12 @@
margin: 0 auto margin: 0 auto
width: 100% width: 100%
max-width: 690px max-width: 690px
.icon-box
text-align: right
width: 100%
.request-button
margin-top: 0.5em
margin-bottom: 0.5em
.mat-table .mat-table
width: 100% width: 100%

@ -7,6 +7,8 @@ import { Levellist } from 'src/app/models/levellist';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
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 { RequestService } from 'src/app/services/request/request.service';
import { DatasharingService } from '../../services/datasharing.service';
@Component({ @Component({
selector: 'app-profile', selector: 'app-profile',
@ -18,7 +20,8 @@ export class ProfileComponent implements OnInit {
actionlist: Actionlist = new Actionlist(); actionlist: Actionlist = new Actionlist();
levellist: Levellist = new Levellist(); levellist: Levellist = new Levellist();
user: User = new User(); private user: User = new User();
self: User;
id: string; id: string;
rankname: string; rankname: string;
profileNotFound = false; profileNotFound = false;
@ -26,7 +29,12 @@ export class ProfileComponent implements OnInit {
dataSource = new MatTableDataSource(this.actionlist.Actions); dataSource = new MatTableDataSource(this.actionlist.Actions);
displayedLevelColumns = ['level', 'name']; displayedLevelColumns = ['level', 'name'];
levelSource = this.levellist.levels; levelSource = this.levellist.levels;
constructor(private router: Router, private http: Http) { }
constructor(
private router: Router,
private http: Http,
private requestService: RequestService,
private data: DatasharingService) { }
@ViewChild(MatSort, {static: true}) sort: MatSort; @ViewChild(MatSort, {static: true}) sort: MatSort;
ngOnInit() { ngOnInit() {
@ -37,12 +45,14 @@ export class ProfileComponent implements OnInit {
const headers = new Headers(); const headers = new Headers();
headers.set('Content-Type', 'application/json'); headers.set('Content-Type', 'application/json');
return this.http.post(url, this.buildJson(this.id)) this.http.post(url, this.buildJson(this.id))
.subscribe(response => { .subscribe(response => {
console.log(response.text()); console.log(response.text());
this.updateUserInfo(response.json()); this.updateUserInfo(response.json());
} });
); this.data.currentUserInfo.subscribe(user => {
this.self = user;
});
} }
public updateUserInfo(response: any) { public updateUserInfo(response: any) {
@ -55,11 +65,17 @@ export class ProfileComponent implements OnInit {
this.user.points = response.data.getUser.points; this.user.points = response.data.getUser.points;
this.user.level = response.data.getUser.level; this.user.level = response.data.getUser.level;
this.rankname = this.levellist.getLevelName(this.user.level); this.rankname = this.levellist.getLevelName(this.user.level);
this.user.allowedToSendRequest = this.requestService.isAllowedToSendRequest(response.data.getUser.id, this.self);
} else { } else {
this.profileNotFound = true; this.profileNotFound = true;
} }
} }
public sendFriendRequest(user: User) {
user.allowedToSendRequest = false;
this.requestService.sendFriendRequest(user);
}
public buildJson(id: string): any { public buildJson(id: string): any {
const body = {query: `query($userId: ID) { const body = {query: `query($userId: ID) {
getUser(userId:$userId){ getUser(userId:$userId){

@ -57,24 +57,7 @@ export class SearchComponent implements OnInit {
.subscribe(response => { .subscribe(response => {
this.foundUsers = this.searchService.renderUsers(response.json()); this.foundUsers = this.searchService.renderUsers(response.json());
for (const foundUser of this.foundUsers) { for (const foundUser of this.foundUsers) {
if (!this.user.loggedIn) {foundUser.allowedToSendRequest = false; } else { foundUser.allowedToSendRequest = this.requestService.isAllowedToSendRequest(foundUser.userID, this.user);
for (const receiverID of this.user.sentRequestUserIDs) {
if (foundUser.userID === receiverID ||
foundUser.userID === this.user.userID) {
foundUser.allowedToSendRequest = false;
}
}
for (const friend of this.user.friends) {
if (foundUser.userID === friend.id) {
foundUser.allowedToSendRequest = false;
}
}
for (const sender of this.user.receivedRequests) {
if (foundUser.userID === sender.senderUserID) {
foundUser.allowedToSendRequest = false;
}
}
}
} }
this.loading = false; this.loading = false;
}); });
@ -86,11 +69,7 @@ export class SearchComponent implements OnInit {
public sendFriendRequest(user: User) { public sendFriendRequest(user: User) {
user.allowedToSendRequest = false; user.allowedToSendRequest = false;
const headers = new Headers(); this.requestService.sendFriendRequest(user);
headers.set('Content-Type', 'application/json');
this.http.post(environment.graphQLUrl, this.requestService.buildJsonRequest(user.userID, 'FRIENDREQUEST'))
.subscribe(response => {
});
} }
} }

@ -3,6 +3,7 @@ import {Headers, Http} from '@angular/http';
import {DatasharingService} from '../datasharing.service'; import {DatasharingService} from '../datasharing.service';
import {Router} from '@angular/router'; import {Router} from '@angular/router';
import {environment} from 'src/environments/environment'; import {environment} from 'src/environments/environment';
import { User } from 'src/app/models/user';
@Injectable({ @Injectable({
@ -13,6 +14,36 @@ export class RequestService {
constructor(private http: Http, private data: DatasharingService, private router: Router) { constructor(private http: Http, private data: DatasharingService, private router: Router) {
} }
public isAllowedToSendRequest(userID: number, self: User): boolean {
if (!self.loggedIn) { return false; } else {
for (const receiverID of self.sentRequestUserIDs) {
if (userID === receiverID ||
userID === self.userID) {
return false;
}
}
for (const friend of self.friends) {
if (userID === friend.id) {
return false;
}
}
for (const sender of self.receivedRequests) {
if (userID === sender.senderUserID) {
return false;
}
}
}
return true;
}
public sendFriendRequest(user: User) {
const headers = new Headers();
headers.set('Content-Type', 'application/json');
this.http.post(environment.graphQLUrl, this.buildJsonRequest(user.userID, 'FRIENDREQUEST'))
.subscribe(response => {
});
}
public buildJsonRequest(id_: number, type_: String): any { public buildJsonRequest(id_: number, type_: String): any {
const body = { const body = {
query: `mutation($id: ID!, $type: RequestType) { query: `mutation($id: ID!, $type: RequestType) {

Loading…
Cancel
Save