Move file upload handling to self service

master
trivernis 5 years ago
parent f4a456ace4
commit 168c737665

@ -8,7 +8,7 @@ import { ProfileService } from 'src/app/services/profile/profile.service';
import { HttpClient } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { environment } from 'src/environments/environment'; import { environment } from 'src/environments/environment';
import {MatSnackBar} from '@angular/material/snack-bar'; import {MatSnackBar} from '@angular/material/snack-bar';
import { reduce } from 'rxjs/operators'; import {SelfService} from '../../services/selfservice/self.service';
@Component({ @Component({
selector: 'app-profile', selector: 'app-profile',
@ -33,7 +33,8 @@ export class ProfileComponent implements OnInit {
private router: Router, private router: Router,
private requestService: RequestService, private requestService: RequestService,
private data: DatasharingService, private data: DatasharingService,
private profileService: ProfileService) { private profileService: ProfileService,
private selfService: SelfService) {
router.events.forEach((event) => { router.events.forEach((event) => {
// check if the user is on the profile page (of userY) and routes to the page of userY (e.g. his own page) // check if the user is on the profile page (of userY) and routes to the page of userY (e.g. his own page)
if (event instanceof NavigationEnd) { if (event instanceof NavigationEnd) {
@ -69,16 +70,11 @@ export class ProfileComponent implements OnInit {
user.allowedToSendRequest = false; user.allowedToSendRequest = false;
this.requestService.sendFriendRequest(user); this.requestService.sendFriendRequest(user);
} }
onFileInput(event) {
console.log(event.target.files[0]);
const formData: any = new FormData();
formData.append('profilePicture', event.target.files[0]);
this.http.post(environment.greenvironmentUrl + '/upload', formData).subscribe( onFileInput(event) {
(response: any) => { this.selfService.changeProfilePicture(event.target.files[0]).subscribe((response) => {
this.userProfile.profilePicture = environment.greenvironmentUrl + response.fileName; this.userProfile.profilePicture = environment.greenvironmentUrl + response.fileName;
}, }, (error) => {
(error) => {
this._snackBar.open('failed to upload picture', 'okay', { this._snackBar.open('failed to upload picture', 'okay', {
duration: 3000 duration: 3000
}); });

@ -0,0 +1,5 @@
export interface IFileUploadResult {
success: boolean;
fileName?: string;
error?: string;
}

@ -8,6 +8,7 @@ import {FriendInfo} from 'src/app/models/friendinfo';
import {HttpClient} from '@angular/common/http'; import {HttpClient} from '@angular/common/http';
import {tap} from 'rxjs/operators'; import {tap} from 'rxjs/operators';
import {BaseService} from '../base.service'; import {BaseService} from '../base.service';
import {IFileUploadResult} from '../../models/interfaces/IFileUploadResult';
const getSelfGraphqlQuery = `{ const getSelfGraphqlQuery = `{
getSelf{ getSelf{
@ -65,17 +66,18 @@ export class SelfService extends BaseService {
return this.http.post(url, SelfService.buildGetSelfBody(), {headers: this.headers}) return this.http.post(url, SelfService.buildGetSelfBody(), {headers: this.headers})
.pipe(tap(response => { .pipe(tap(response => {
this.stillLoggedIn();
this.updateUserInfo(response); this.updateUserInfo(response);
}, error => {
this.notLoggedIn();
})); }));
} }
public stillLoggedIn() { /**
} * Uploads a file as a profile picture
* @param file
public notLoggedIn() { */
public changeProfilePicture(file: any) {
const formData: any = new FormData();
formData.append('profilePicture', file);
return this.http.post<IFileUploadResult>(environment.greenvironmentUrl + '/upload', formData);
} }
/** /**
@ -87,26 +89,4 @@ export class SelfService extends BaseService {
user.assignFromResponse(response.data.getSelf); user.assignFromResponse(response.data.getSelf);
this.data.changeUserInfo(user); this.data.changeUserInfo(user);
} }
public fakeLogin() {
const user: User = new User();
let friendRequest: FriendRequest = new FriendRequest();
user.loggedIn = true;
user.userID = 1;
user.username = 'Rapier';
user.handle = 'rapier123';
user.email = 'r@r.com';
user.points = 100;
user.level = 3;
user.friends.push(new FriendInfo(1, 'Freund77', 4, 'lalala'));
friendRequest = new FriendRequest();
friendRequest.id = 10;
friendRequest.senderUserID = 99;
friendRequest.senderUsername = 'Löwe';
friendRequest.senderHandle = 'loewe123';
user.receivedRequests.push(friendRequest);
this.data.changeUserInfo(user);
}
} }

Loading…
Cancel
Save