182. Update profile image back end call (#26)

This commit is contained in:
Art
2021-09-22 14:26:50 +03:00
parent 77e40c594f
commit aeeb66b833
3 changed files with 25 additions and 6 deletions

View File

@ -4,7 +4,7 @@ import {User} from "../../model/user";
import {UserService} from "../../service/user.service";
import {NotificationService} from "../../service/notification.service";
import {NotificationType} from "../../notification/notification-type";
import {HttpErrorResponse} from "@angular/common/http";
import {HttpErrorResponse, HttpEvent} from "@angular/common/http";
import {NgForm} from "@angular/forms";
import {CustomHttpResponse} from "../../dto/custom-http-response";
import {AuthenticationService} from "../../service/authentication.service";
@ -234,6 +234,23 @@ export class UserComponent implements OnInit, OnDestroy {
}
public onUpdateProfileImage(): void {
if (!this.profileImage) return;
this.refreshing = true;
const formData = new FormData();
formData.append("profileImage", this.profileImage);
let user = this.authenticationService.getUserFromLocalStorage();
let subscription = this.userService.updateProfileImage(user.username, formData)
.subscribe(
(event: HttpEvent<any>) => {
this.notificationService.notify(NotificationType.SUCCESS, `Profile image updated successfully: ${event}`);
this.refreshing = false;
this.getUsers(false);
},
(errorResponse: HttpErrorResponse) => {
this.sendErrorNotification(errorResponse.error.message);
this.refreshing = false;
}
);
this.subscriptions.push(subscription);
}
}

View File

@ -1,6 +1,6 @@
import {Injectable} from '@angular/core';
import {environment} from "../../environments/environment";
import {HttpClient, HttpErrorResponse, HttpEvent} from "@angular/common/http";
import {HttpClient, HttpEvent} from "@angular/common/http";
import {Observable} from "rxjs";
import {User} from "../model/user";
import {CustomHttpResponse} from "../dto/custom-http-response";
@ -37,9 +37,9 @@ export class UserService {
.post<CustomHttpResponse>(`${this.host}/user/resetPassword/${email}`, null);
}
public updateProfileImage(username: string, formData: FormData): Observable<HttpEvent<User | HttpErrorResponse>> {
public updateProfileImage(username: string, formData: FormData): Observable<HttpEvent<User>> {
return this.httpClient
.put<User | HttpErrorResponse>(`${this.host}/user/${username}/profileImage`, formData,
.put<User>(`${this.host}/user/${username}/profileImage`, formData,
{
reportProgress: true,
observe: 'events'