174. Delete user back end call (#24 Section 25: User Page - Delete User)

This commit is contained in:
Art
2021-09-21 21:20:30 +03:00
parent 6a4f3498c7
commit cf3474f32e
3 changed files with 19 additions and 3 deletions

View File

@ -84,7 +84,7 @@
<td class="">
<div class="btn-group">
<button class="btn btn-outline-info" (click)="onEditUser(appUser)"><i class="fas fa-edit"></i></button>
<button class="btn btn-outline-danger"><i class="fas fa-trash"></i></button>
<button class="btn btn-outline-danger" (click)="onDeleteUser(appUser)"><i class="fas fa-trash"></i></button>
</div>
</td>
</tr>

View File

@ -6,6 +6,7 @@ import {NotificationService} from "../../service/notification.service";
import {NotificationType} from "../../notification/notification-type";
import {HttpErrorResponse} from "@angular/common/http";
import {NgForm} from "@angular/forms";
import {CustomHttpResponse} from "../../dto/custom-http-response";
@Component({
selector: 'app-user',
@ -153,4 +154,19 @@ export class UserComponent implements OnInit, OnDestroy {
);
this.subscriptions.push(subscription);
}
onDeleteUser(user: User) {
const subscription = this.userService.deleteUser(user.userId)
.subscribe(
(response: CustomHttpResponse) => {
this.getUsers(false);
this.invalidateVariables();
this.notificationService.notify(NotificationType.SUCCESS, response.message);
},
(errorResponse: HttpErrorResponse) => {
this.sendErrorNotification(errorResponse.error.message);
}
);
this.subscriptions.push(subscription);
}
}

View File

@ -46,9 +46,9 @@ export class UserService {
});
}
public deleteUser(userId: string): Observable<CustomHttpResponse | HttpErrorResponse> {
public deleteUser(userId: string): Observable<CustomHttpResponse> {
return this.httpClient
.delete<CustomHttpResponse | HttpErrorResponse>(`${this.host}/user/${userId}`);
.delete<CustomHttpResponse>(`${this.host}/user/${userId}`);
}
public addUsersToLocalStorage(users: User[]) {