From 7cd563c430674f0a26966f95a58e9c8f268f9dd6 Mon Sep 17 00:00:00 2001 From: "diego.martinez" Date: Sat, 14 Dec 2024 19:36:08 +0100 Subject: [PATCH 1/3] created visit-cancel as user --- src/app/app-routing.module.ts | 1 + .../visit-cancel/visit-cancel.component.css | 0 .../visit-cancel/visit-cancel.component.html | 16 ++++ .../visit-cancel.component.spec.ts | 23 +++++ .../visit-cancel/visit-cancel.component.ts | 90 +++++++++++++++++++ .../visit-status/visit-status.component.css | 6 +- 6 files changed, 135 insertions(+), 1 deletion(-) create mode 100644 src/app/visit/visit-cancel/visit-cancel.component.css create mode 100644 src/app/visit/visit-cancel/visit-cancel.component.html create mode 100644 src/app/visit/visit-cancel/visit-cancel.component.spec.ts create mode 100644 src/app/visit/visit-cancel/visit-cancel.component.ts diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 7d70c31..1881778 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -33,6 +33,7 @@ const routes: Routes = [ { path: 'apartment/:id/update', component: ApartmentUpdateComponent}, { path: 'apartment/:id/delete', component: ApartmentDeleteComponent}, { path: 'visit/:id/status', component: VisitStatusComponent}, + { path: 'visit/:id/cancel', component:VisitCancelComponent}, { path: 'rooms', component: RoomListComponent}, { path: '404', component: NotFoundComponent}, { path: '', redirectTo: 'about', pathMatch: 'full'} diff --git a/src/app/visit/visit-cancel/visit-cancel.component.css b/src/app/visit/visit-cancel/visit-cancel.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/app/visit/visit-cancel/visit-cancel.component.html b/src/app/visit/visit-cancel/visit-cancel.component.html new file mode 100644 index 0000000..8a5b365 --- /dev/null +++ b/src/app/visit/visit-cancel/visit-cancel.component.html @@ -0,0 +1,16 @@ +@if (errorFetchMsg){ + +} +@else { + + +@if(visit.status !== "CANCELLED"){ +
+ +
+} +} diff --git a/src/app/visit/visit-cancel/visit-cancel.component.spec.ts b/src/app/visit/visit-cancel/visit-cancel.component.spec.ts new file mode 100644 index 0000000..7ccd109 --- /dev/null +++ b/src/app/visit/visit-cancel/visit-cancel.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { VisitCancelComponent } from './visit-cancel.component'; + +describe('VisitCancelComponent', () => { + let component: VisitCancelComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [VisitCancelComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(VisitCancelComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/app/visit/visit-cancel/visit-cancel.component.ts b/src/app/visit/visit-cancel/visit-cancel.component.ts new file mode 100644 index 0000000..1f0f6c5 --- /dev/null +++ b/src/app/visit/visit-cancel/visit-cancel.component.ts @@ -0,0 +1,90 @@ +import { Component, OnInit} from '@angular/core'; +import {Visit} from '../visit'; +import {User} from '../../login-basic/user'; +import {ActivatedRoute, Router} from '@angular/router'; +import {VisitService} from '../visit.service'; +import {ErrorMessageService} from '../../error-handler/error-message.service'; +import {AuthenticationBasicService} from '../../login-basic/authentication-basic.service'; +import {catchError, of} from 'rxjs'; +import {NgClass} from '@angular/common'; +import {VisitStatusComponent} from '../visit-status/visit-status.component'; + +@Component({ + selector: 'app-visit-cancel', + standalone: true, + imports: [ + NgClass, VisitStatusComponent ], + templateUrl: './visit-cancel.component.html', + styleUrl: './visit-cancel.component.css' +}) +export class VisitCancelComponent implements OnInit{ + public visit: Visit = new Visit() + public user: User = new User(); + public visitId: string = ''; + public errorFetchMsg: string = ''; + public isAuthorized: boolean = false; + + + + constructor( + private router: Router, + private visitService: VisitService = new VisitService(), + private activatedRoute: ActivatedRoute, + private errorMessageService: ErrorMessageService, + private authenticationService: AuthenticationBasicService + ) {} + + + ngOnInit(): void { + this.visit = new Visit() + + this.visitId = this.activatedRoute.snapshot.paramMap.get('id') || ''; + this.user = this.authenticationService.getCurrentUser(); + this.isAuthorized = this.isAuthorised(); + + + if (!this.isAuthorized) { + this.onUnauthorised(); + return; + } + + + this.visitService + .getResource(this.visitId) + .pipe( + catchError((error) => { + this.errorFetchMsg = error.message; + return of(null); + }) + ) + .subscribe((_visit) => { + if (_visit) { + this.visit = _visit; + this.visit.id = this.visit.getIdFromLinks(); + + } + }); + + } + + private isAuthorised(): boolean { + return this.user.getRoles().includes('user'); + } + + onUnauthorised(): void { + this.errorMessageService.showErrorMessage('You are not authorized'); + this.router.navigate(['/apartments']); + } + + onCancel(): void{ + if (!this.isAuthorized) { + this.onUnauthorised(); + return; + } + this.visit.status = "CANCELLED" + this.visitService.updateResource(this.visit) + .subscribe(() => { + window.location.reload(); + }) + } +} diff --git a/src/app/visit/visit-status/visit-status.component.css b/src/app/visit/visit-status/visit-status.component.css index 877f53e..ef8336a 100644 --- a/src/app/visit/visit-status/visit-status.component.css +++ b/src/app/visit/visit-status/visit-status.component.css @@ -57,7 +57,7 @@ h1 { margin-right: 8px; } -/* Colores según el estado */ +/* Colores según el estado */ .status.pending::before { background-color: #f1c40f; /* Amarillo */ } @@ -66,6 +66,10 @@ h1 { background-color: #e74c3c; /* Rojo */ } +.status.cancelled::before { + background-color: #e74c3c; /* Rojo */ +} + .status.accepted::before { background-color: #2ecc71; /* Verde */ } From 195b3e5a1b86e77a3d5fe0eed2e6ee1f2d404cbb Mon Sep 17 00:00:00 2001 From: GiovanniMaerean Date: Sat, 14 Dec 2024 19:41:35 +0100 Subject: [PATCH 2/3] Import error --- src/app/app-routing.module.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 1881778..78f85bb 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -17,6 +17,7 @@ import { ApartmentUpdateComponent } from './apartment/apartment-update/apartment import { ApartmentDeleteComponent } from './apartment/apartment-delete/apartment-delete.component'; import { VisitStatusComponent } from './visit/visit-status/visit-status.component'; import { RoomListComponent } from './room/room-list/room-list.component'; +import {VisitCancelComponent} from './visit/visit-cancel/visit-cancel.component'; const routes: Routes = [ { path: 'users/create', component: UserRegisterComponent}, From 247f40c2d224cef36072ae33019b5ff022f6e1d8 Mon Sep 17 00:00:00 2001 From: GiovanniMaerean Date: Sat, 14 Dec 2024 20:15:12 +0100 Subject: [PATCH 3/3] onUnauthorised navigates to about instead of apartments --- src/app/visit/visit-cancel/visit-cancel.component.ts | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/app/visit/visit-cancel/visit-cancel.component.ts b/src/app/visit/visit-cancel/visit-cancel.component.ts index 1f0f6c5..1b76fca 100644 --- a/src/app/visit/visit-cancel/visit-cancel.component.ts +++ b/src/app/visit/visit-cancel/visit-cancel.component.ts @@ -6,14 +6,13 @@ import {VisitService} from '../visit.service'; import {ErrorMessageService} from '../../error-handler/error-message.service'; import {AuthenticationBasicService} from '../../login-basic/authentication-basic.service'; import {catchError, of} from 'rxjs'; -import {NgClass} from '@angular/common'; import {VisitStatusComponent} from '../visit-status/visit-status.component'; @Component({ selector: 'app-visit-cancel', standalone: true, imports: [ - NgClass, VisitStatusComponent ], + VisitStatusComponent ], templateUrl: './visit-cancel.component.html', styleUrl: './visit-cancel.component.css' }) @@ -73,7 +72,7 @@ export class VisitCancelComponent implements OnInit{ onUnauthorised(): void { this.errorMessageService.showErrorMessage('You are not authorized'); - this.router.navigate(['/apartments']); + this.router.navigate(['/about']); } onCancel(): void{