From 26283130072c4446d6357bcafb471fa8828457bc Mon Sep 17 00:00:00 2001 From: Patrick Mueller Date: Tue, 27 Dec 2022 13:49:16 +0100 Subject: [PATCH] Add delete capabilities --- src/app/app.module.ts | 2 +- src/app/components/event/event.component.html | 7 +++++ src/app/components/event/event.component.ts | 31 ++++++++++++++++--- .../events-table/events-table.component.html | 3 +- .../events-table/events-table.component.ts | 8 +++++ src/app/services/api.service.ts | 24 ++++++++++++-- 6 files changed, 67 insertions(+), 8 deletions(-) diff --git a/src/app/app.module.ts b/src/app/app.module.ts index b503b20..4a65db0 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -1,4 +1,4 @@ -import {NgModule} from '@angular/core'; +import {ComponentRef, NgModule} from '@angular/core'; import {BrowserModule} from '@angular/platform-browser'; import {AppComponent} from './app.component'; diff --git a/src/app/components/event/event.component.html b/src/app/components/event/event.component.html index 830134f..8da14cf 100644 --- a/src/app/components/event/event.component.html +++ b/src/app/components/event/event.component.html @@ -29,6 +29,10 @@

Start Date must not be after end date!

Required fields are missing!

+

Error creating event. Please try again.

+ + + @@ -61,4 +65,7 @@ + + + diff --git a/src/app/components/event/event.component.ts b/src/app/components/event/event.component.ts index 88e2497..c72530c 100644 --- a/src/app/components/event/event.component.ts +++ b/src/app/components/event/event.component.ts @@ -1,4 +1,4 @@ -import {Component, Input, OnInit} from '@angular/core'; +import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core'; import {Event} from '../../models/event'; import {ApiService} from '../../services/api.service'; @@ -10,7 +10,6 @@ import {ApiService} from '../../services/api.service'; export class EventComponent implements OnInit { @Input() event: Event | undefined; - @Input() editActive: boolean = false; newStartDate: string | undefined; newStartTime: string | undefined; @@ -18,8 +17,12 @@ export class EventComponent implements OnInit { newEndTime: string | undefined; showDateError: boolean = false; requiredFieldsMissing: boolean = false; + showCreateError: boolean = false; + @Output() deleteEvent = new EventEmitter(); - constructor(private api: ApiService) { + constructor( + private api: ApiService + ) { } ngOnInit(): void { @@ -53,7 +56,14 @@ export class EventComponent implements OnInit { if(this.event.eventId === undefined) { this.api.createEvent(this.event).subscribe((res: any) => { console.log(res); - this.event!.eventId = res.eventId; + + if(res.eventId) { + this.event!.eventId = res.eventId; + } else { + this.showCreateError = true; + return; + } + }); } else { // Update existing event @@ -126,4 +136,17 @@ export class EventComponent implements OnInit { isNullOrBlank(str: string | null): boolean { return str === null || str === undefined || str.trim() === ''; } + + triggerDelete() { + let deleteConfirmed = window.confirm('Are you sure?'); + + if(deleteConfirmed && this.event) { + this.api.deleteEvent(this.event).subscribe((res: any) => { + console.log(res); + if(res.message) { + this.deleteEvent.next(this.event!.eventId); + } + }); + } + } } diff --git a/src/app/components/events-table/events-table.component.html b/src/app/components/events-table/events-table.component.html index dc15afc..bf1f133 100644 --- a/src/app/components/events-table/events-table.component.html +++ b/src/app/components/events-table/events-table.component.html @@ -9,7 +9,8 @@ Created by URL Edit + Delete - + diff --git a/src/app/components/events-table/events-table.component.ts b/src/app/components/events-table/events-table.component.ts index 3cc1313..02c9e79 100644 --- a/src/app/components/events-table/events-table.component.ts +++ b/src/app/components/events-table/events-table.component.ts @@ -26,4 +26,12 @@ export class EventsTableComponent implements OnInit { createdBy: UtilsService.getNameFromLocalStorage() } as Event); } + + deleteEvent(id: number) { + this.events.forEach(event => { + if(event.eventId === id) { + this.events.splice(this.events.indexOf(event), 1); + } + }) + } } diff --git a/src/app/services/api.service.ts b/src/app/services/api.service.ts index 5e95643..a28fb9d 100644 --- a/src/app/services/api.service.ts +++ b/src/app/services/api.service.ts @@ -39,7 +39,7 @@ export class ApiService { return this.http.put(this.apiUrl + updateEvent.eventId, updateEvent); } catch (exception) { - console.log('Error fetching events from API'); + console.log('Error updating event'); } return new Observable(); } @@ -54,7 +54,27 @@ export class ApiService { return this.http.post(this.apiUrl, createEvent); } catch (exception) { - console.log('Error fetching events from API'); + console.log('Error creating event'); + } + return new Observable(); + } + + deleteEvent(event: Event): Observable { + try { + let password = UtilsService.getPasswordFromLocalStorage(); + + let deleteEvent: any = event; + + deleteEvent.password = password; + + return this.http.delete(this.apiUrl + deleteEvent.eventId, { + headers: { + 'Content-Type': 'application/json' + }, + body: deleteEvent + }); + } catch (exception) { + console.log('Error deleting event'); } return new Observable(); }