From f5328cd57ab806e425825b7aa17ddb6973aab990 Mon Sep 17 00:00:00 2001 From: Max Date: Sun, 26 Jan 2020 21:18:04 +0100 Subject: [PATCH] Add delete event functionallity --- src/app/components/group/group.component.html | 4 +++ src/app/components/group/group.component.ts | 4 +++ src/app/models/event.ts | 4 ++- src/app/models/interfaces/IEvent.ts | 2 ++ src/app/services/group/group.service.ts | 30 ++++++++++++++++--- 5 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/app/components/group/group.component.html b/src/app/components/group/group.component.html index 64677ef..bb4c11a 100644 --- a/src/app/components/group/group.component.html +++ b/src/app/components/group/group.component.html @@ -110,6 +110,10 @@ matTooltipShowDelay="500" (click)="leaveEvent(event)" [disabled]="!event.joined"> event_busy + diff --git a/src/app/components/group/group.component.ts b/src/app/components/group/group.component.ts index 2d7cb5f..fa81f61 100644 --- a/src/app/components/group/group.component.ts +++ b/src/app/components/group/group.component.ts @@ -174,6 +174,10 @@ export class GroupComponent implements OnInit { }); } + public deleteEvent(event: Event) { + this.groupService.deleteEvent(event.id).subscribe(); + } + public showUserProfile(user: User) { this.router.navigate(['profile/' + user.userID]); } diff --git a/src/app/models/event.ts b/src/app/models/event.ts index a01d2f7..e65111e 100644 --- a/src/app/models/event.ts +++ b/src/app/models/event.ts @@ -1,10 +1,11 @@ -import {IEvent} from './interfaces/IEvent'; +import { IEvent } from './interfaces/IEvent'; export class Event { id: string; name: string; date: string; joined: boolean; + deletable: boolean; public assignFromResponse(eventDataResponse: IEvent) { this.id = eventDataResponse.id; @@ -12,6 +13,7 @@ export class Event { const temp = new Date(Number(eventDataResponse.dueDate)); this.date = temp.toLocaleString('en-GB'); this.joined = eventDataResponse.joined; + this.deletable = eventDataResponse.deletable; return this; } diff --git a/src/app/models/interfaces/IEvent.ts b/src/app/models/interfaces/IEvent.ts index cdf7fa0..49f1c2a 100644 --- a/src/app/models/interfaces/IEvent.ts +++ b/src/app/models/interfaces/IEvent.ts @@ -7,4 +7,6 @@ export interface IEvent { dueDate: string; joined: boolean; + + deletable: boolean; } diff --git a/src/app/services/group/group.service.ts b/src/app/services/group/group.service.ts index 7527fe6..3f1c935 100644 --- a/src/app/services/group/group.service.ts +++ b/src/app/services/group/group.service.ts @@ -20,7 +20,7 @@ const getGroupGraphqlQuery = `query($groupId: ID!) { creator{id name handle} admins{id name handle} members{id name handle profilePicture} - events{id name dueDate joined} + events{id name dueDate joined deletable} } }`; @@ -146,8 +146,6 @@ export class GroupService extends BaseService { } public leaveEvent(eventId: string) { - const headers = new Headers(); - headers.set('Content-Type', 'application/json'); const body = { query: `mutation($eventId: ID!) { leaveEvent(eventId: $eventId) { @@ -157,7 +155,31 @@ export class GroupService extends BaseService { eventId: eventId } }; - return this.postGraphql(body); + return this.postGraphql(body) + .pipe(this.retryRated()); + } + + public deleteEvent(eventId: string) { + const body = { + query: `mutation($eventId: ID!) { + deleteEvent(eventId: $eventId) { + joined + } + }`, variables: { + eventId + } + }; + return this.postGraphql(body) + .pipe(this.retryRated()) + .pipe(tap(response => { + const group = this.group.getValue(); + for (let i = 0; i < group.events.length; i++) { + if (group.events[i].id === eventId) { + group.events.splice(i, 1); + } + } + this.group.next(group); + })); } public changeProfilePicture(file: any, id: number) {