From e35564735f969a6e03e41634b4dcee93c234e7c2 Mon Sep 17 00:00:00 2001 From: Toast Date: Thu, 6 Feb 2025 10:46:04 +0100 Subject: [PATCH] Components/create-edit-modal: finish implementing --- .../create-edit-modal.component.ts | 66 ++++++++++++++----- 1 file changed, 50 insertions(+), 16 deletions(-) diff --git a/src/components/create-modal/create-edit-modal/create-edit-modal.component.ts b/src/components/create-modal/create-edit-modal/create-edit-modal.component.ts index 6a7b561..4428e32 100644 --- a/src/components/create-modal/create-edit-modal/create-edit-modal.component.ts +++ b/src/components/create-modal/create-edit-modal/create-edit-modal.component.ts @@ -59,22 +59,56 @@ export class CreateEditModalComponent { //TODO: Allow user to specify genres genres: [] } - this.showsService.sendShow(show).subscribe({ - next: (response: ShowsApiCreation) => { - show._id = response.newId - }, error: err => { - this.toastService.show({ - body: "Could not add show!", - htmlClass: "bg-danger text-light" - }) - console.error(err) - }, complete: () => { - this.toastService.show({ - body: "Show added successfully!", - htmlClass: "bg-success text-light" - }) - this.activeModal.close(show) + + + if (!this.editMode) { + this.showsService.sendShow(show).subscribe({ + next: (response: ShowsApiCreation) => { + show._id = response.newId + }, error: err => { + this.showToast(true, "created") + console.error(err) + }, complete: () => { + this.showToast(false, "created") + this.activeModal.close(show) + } + }) + } else { + // The API requires the show to also have an ID, so + // I add it here + show._id = this.show?._id + this.showsService.updateShow(show).subscribe({ + next: (response: ShowsApiDeletionEdit) => { + // Do nothing + }, error: err => { + this.showToast(true, "edited") + console.error(err) + }, complete: () => { + this.showToast(false, "edited") + this.activeModal.close(show) + } + }) + } + } + + private showToast(error: boolean, action: "edited" | "created") { + let htmlClass: string = "bg-success text-light" + let body: string; + if (error) { + htmlClass = htmlClass.replace("success", "danger") + let temp: string + switch (action){ + case "created": + temp = "created" + break + case "edited": + temp = "edit" + break } - }) + body = `Could not ${temp} show!` + } else { + body = `Show ${action} successfully!` + } + this.toastService.show({body,htmlClass}) } }