From b0bbf2b8e7d101cd2f76cf1f66f47d313fcb0449 Mon Sep 17 00:00:00 2001 From: jherel Date: Thu, 20 Apr 2017 13:52:16 +0200 Subject: [PATCH] Add pagination in manage-resources, and delete resource --- .../manage-resources.component.css | 3 + .../manage-resources.component.html | 35 +++++-- .../manage-resources.component.ts | 93 +++++++++++++++++-- .../manage-users/manage-users.component.html | 1 + frontend/src/app/service/resource.service.ts | 32 ++++++- 5 files changed, 147 insertions(+), 17 deletions(-) diff --git a/frontend/src/app/component/admin/manage-resources/manage-resources.component.css b/frontend/src/app/component/admin/manage-resources/manage-resources.component.css index e69de29..fb5336f 100644 --- a/frontend/src/app/component/admin/manage-resources/manage-resources.component.css +++ b/frontend/src/app/component/admin/manage-resources/manage-resources.component.css @@ -0,0 +1,3 @@ +table { + background-color: white !important; +} diff --git a/frontend/src/app/component/admin/manage-resources/manage-resources.component.html b/frontend/src/app/component/admin/manage-resources/manage-resources.component.html index 5d04ab7..79bba4e 100644 --- a/frontend/src/app/component/admin/manage-resources/manage-resources.component.html +++ b/frontend/src/app/component/admin/manage-resources/manage-resources.component.html @@ -1,9 +1,20 @@ +
+ ¡Eliminado! {{message}} +
+
+ Algo ha ido mal... {{message}} +
+

Administración de recursos Administra los libros o las revistas

+
- +
@@ -29,15 +40,25 @@

Administración de recursos Administra los libros o las revistas - - - +

Título
- - - +
+ +
+ + +
+
diff --git a/frontend/src/app/component/admin/manage-resources/manage-resources.component.ts b/frontend/src/app/component/admin/manage-resources/manage-resources.component.ts index c0de606..2c1eec5 100644 --- a/frontend/src/app/component/admin/manage-resources/manage-resources.component.ts +++ b/frontend/src/app/component/admin/manage-resources/manage-resources.component.ts @@ -1,5 +1,9 @@ import { Component, OnInit } from '@angular/core'; +import { Router } from '@angular/router'; +import { Resource } from 'app/model/resource.model'; + +import { SessionService } from 'app/service/session.service'; import { ResourceService } from 'app/service/resource.service'; @Component({ @@ -8,17 +12,94 @@ import { ResourceService } from 'app/service/resource.service'; }) export class ManageResourcesComponent implements OnInit { - private resources: Object[] = []; + private resources: Resource[] = []; + errorMessage: boolean; + message: String; + showNextPage: boolean; + showPreviousPage: boolean; + successMessage: boolean; + resourcesPage: number; - constructor(private service: ResourceService) { + constructor(private router: Router, + private sessionService: SessionService, + private ResourceService: ResourceService) { + this.successMessage = false; + this.errorMessage = false; + this.resourcesPage = 0; + this.showNextPage = false; + this.showPreviousPage = false; } ngOnInit() { - this.service.getAllResources().subscribe( - resources => { - this.resources = resources; + if (!this.sessionService.checkCredentials()) { + this.router.navigate(["/login"]); + } else { + this.getResources(); + this.checkNextPage(); + this.checkPreviousPage(); + } + } + + getResources() { + this.ResourceService.getPageResources(this.resourcesPage).subscribe( + resources => this.resources = resources, + error => console.log("Fail trying to get current page of resources.") + ); + } + + nextPage() { + this.showNextPage = false; + this.showPreviousPage = false; + this.resourcesPage++; + this.getResources(); + this.checkNextPage(); + this.showPreviousPage = true; + } + + previousPage() { + this.showNextPage = false; + this.showPreviousPage = false; + this.resourcesPage--; + this.getResources(); + this.checkPreviousPage(); + this.showNextPage = true; + } + + checkNextPage() { + this.ResourceService.getPageResources(this.resourcesPage + 1).subscribe( + resources => this.showNextPage = (Object.keys(resources).length === 0) ? false : true + ); + } + + checkPreviousPage() { + if (this.resourcesPage > 0) { + this.ResourceService.getPageResources(this.resourcesPage - 1).subscribe( + resources => this.showPreviousPage = (Object.keys(resources).length === 0) ? false : true + ); + } else { + this.showPreviousPage = false; + } + } + + deleteResource(id: number) { + this.ResourceService.deleteUser(id).subscribe( + response => { + this.successMessage = true; + this.errorMessage = false; + this.message = 'Recurso eliminado correctamente.'; + this.resourcesPage = 0; + console.log('Resource successfully deleted.'); + this.getResources(); + this.checkNextPage(); + this.checkPreviousPage(); }, - error => console.log(error) + error => { + this.successMessage = false; + this.errorMessage = true; + this.message = 'No se ha podido eliminar el recurso.' + console.log('Fail trying to delete selected resource.'); + } ); } + } diff --git a/frontend/src/app/component/admin/manage-users/manage-users.component.html b/frontend/src/app/component/admin/manage-users/manage-users.component.html index ba44a0c..4f4f571 100644 --- a/frontend/src/app/component/admin/manage-users/manage-users.component.html +++ b/frontend/src/app/component/admin/manage-users/manage-users.component.html @@ -46,6 +46,7 @@

+
diff --git a/frontend/src/app/service/resource.service.ts b/frontend/src/app/service/resource.service.ts index 4281503..446442e 100644 --- a/frontend/src/app/service/resource.service.ts +++ b/frontend/src/app/service/resource.service.ts @@ -1,15 +1,21 @@ -import {Injectable} from '@angular/core'; -import {Http, Response} from '@angular/http'; -import {Observable} from 'rxjs/Observable'; +import { Injectable } from '@angular/core'; +import { Headers, Http, Response } from '@angular/http'; +import { Observable } from 'rxjs/Observable'; import 'rxjs/Rx'; -import {RESOURCES_URL} from "../util"; +import { RESOURCES_URL } from "../util"; @Injectable() export class ResourceService { + authCreds: string; + constructor(private http: Http) { } + setAuthHeaders(authCreds: string) { + this.authCreds = authCreds; + } + getResource(id: number) { return this.http.get(RESOURCES_URL + '/' + id) .map(response => response.json()) @@ -23,9 +29,27 @@ export class ResourceService { .catch(error => Observable.throw('Server error')); } + getPageResources(page?: number) { + let url = (page) ? RESOURCES_URL + '?page=' + page : RESOURCES_URL; + return this.http.get(url) + .map(response => response.json().content) + .catch(error => Observable.throw('Server error')); + } + searchResources(name: string, page: number) { return this.http.get(RESOURCES_URL + '?name=' + name + '&page=' + page) .map(response => response.json().content) .catch(error => Observable.throw('Server error')); } + + deleteUser(id: number) { + this.authCreds = localStorage.getItem("creds"); + let headers: Headers = new Headers(); + headers.append('Content-Type', 'application/json'); + headers.append('X-Requested-With', 'XMLHttpRequest'); + headers.append('Authorization', 'Basic ' + this.authCreds); + return this.http.delete(RESOURCES_URL + '/' + id, { headers: headers }) + .map(response => response.json()) + .catch(error => Observable.throw('Server error')); + } }