diff --git a/src/app/app.component.html b/src/app/app.component.html index 7dd570e..caeea25 100644 --- a/src/app/app.component.html +++ b/src/app/app.component.html @@ -1 +1,2 @@ + diff --git a/src/app/app.component.ts b/src/app/app.component.ts index 776287c..42eed63 100644 --- a/src/app/app.component.ts +++ b/src/app/app.component.ts @@ -1,9 +1,10 @@ import { Component } from '@angular/core'; import { RouterOutlet } from '@angular/router'; +import {ToastContainerComponent} from '../components/toast-container/toast-container/toast-container.component'; @Component({ selector: 'app-root', - imports: [RouterOutlet], + imports: [RouterOutlet, ToastContainerComponent], templateUrl: './app.component.html', styleUrl: './app.component.css' }) diff --git a/src/components/create-modal/create-modal/create-modal.component.css b/src/components/create-modal/create-modal/create-modal.component.css new file mode 100644 index 0000000..e69de29 diff --git a/src/components/create-modal/create-modal/create-modal.component.html b/src/components/create-modal/create-modal/create-modal.component.html new file mode 100644 index 0000000..5a7d0cc --- /dev/null +++ b/src/components/create-modal/create-modal/create-modal.component.html @@ -0,0 +1,27 @@ + + diff --git a/src/components/create-modal/create-modal/create-modal.component.spec.ts b/src/components/create-modal/create-modal/create-modal.component.spec.ts new file mode 100644 index 0000000..c52da61 --- /dev/null +++ b/src/components/create-modal/create-modal/create-modal.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { CreateModalComponent } from './create-modal.component'; + +describe('CreateModalComponent', () => { + let component: CreateModalComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [CreateModalComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(CreateModalComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/components/create-modal/create-modal/create-modal.component.ts b/src/components/create-modal/create-modal/create-modal.component.ts new file mode 100644 index 0000000..24931c2 --- /dev/null +++ b/src/components/create-modal/create-modal/create-modal.component.ts @@ -0,0 +1,39 @@ +import {Component, inject} from '@angular/core'; +import {NgbActiveModal} from '@ng-bootstrap/ng-bootstrap'; +import {FormControl, FormGroup, ReactiveFormsModule, Validators} from '@angular/forms'; + +@Component({ + selector: 'app-create-modal', + imports: [ + ReactiveFormsModule + ], + templateUrl: './create-modal.component.html', + styleUrl: './create-modal.component.css' +}) +export class CreateModalComponent { + private activeModal = inject(NgbActiveModal) + protected newShowForm: FormGroup + + constructor() { + this.newShowForm = new FormGroup({ + title: new FormControl("", Validators.required), + year: new FormControl("", [Validators.required, Validators.min(1900)]), + seasons: new FormControl("", [Validators.required, Validators.min(1)]), + description: new FormControl("", Validators.required) + }) + } + + protected dismiss() { + this.activeModal.dismiss() + } + + protected formSubmitted(form: FormGroup) { + let show: {} = { + title: form.get("title")?.value, + year: form.get("year")?.value, + seasons: form.get("seasons")?.value, + description: form.get("description")?.value + } + console.log(show) + } +} diff --git a/src/components/toast-container/toast-container/toast-container.component.css b/src/components/toast-container/toast-container/toast-container.component.css new file mode 100644 index 0000000..4d1c95f --- /dev/null +++ b/src/components/toast-container/toast-container/toast-container.component.css @@ -0,0 +1,7 @@ +:host { + position: fixed; + top: 0; + right: 0; + margin: 0.5em; + z-index: 1200; +} diff --git a/src/components/toast-container/toast-container/toast-container.component.html b/src/components/toast-container/toast-container/toast-container.component.html new file mode 100644 index 0000000..35b3224 --- /dev/null +++ b/src/components/toast-container/toast-container/toast-container.component.html @@ -0,0 +1,7 @@ +@for (toast of toastService.toasts; track toast) { + {{ toast.body }} +} diff --git a/src/components/toast-container/toast-container/toast-container.component.spec.ts b/src/components/toast-container/toast-container/toast-container.component.spec.ts new file mode 100644 index 0000000..d052b36 --- /dev/null +++ b/src/components/toast-container/toast-container/toast-container.component.spec.ts @@ -0,0 +1,23 @@ +import { ComponentFixture, TestBed } from '@angular/core/testing'; + +import { ToastContainerComponent } from './toast-container.component'; + +describe('ToastContainerComponent', () => { + let component: ToastContainerComponent; + let fixture: ComponentFixture; + + beforeEach(async () => { + await TestBed.configureTestingModule({ + imports: [ToastContainerComponent] + }) + .compileComponents(); + + fixture = TestBed.createComponent(ToastContainerComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/src/components/toast-container/toast-container/toast-container.component.ts b/src/components/toast-container/toast-container/toast-container.component.ts new file mode 100644 index 0000000..c1eac7a --- /dev/null +++ b/src/components/toast-container/toast-container/toast-container.component.ts @@ -0,0 +1,15 @@ +import {Component, inject} from '@angular/core'; +import {ToastService} from '../../../services/toast/toast.service'; +import {NgbToast} from '@ng-bootstrap/ng-bootstrap'; + +@Component({ + selector: 'app-toast-container', + imports: [ + NgbToast + ], + templateUrl: './toast-container.component.html', + styleUrl: './toast-container.component.css' +}) +export class ToastContainerComponent { + toastService: ToastService = inject(ToastService); +} diff --git a/src/shows/shows-api.ts b/src/interfaces/show.ts similarity index 58% rename from src/shows/shows-api.ts rename to src/interfaces/show.ts index c3d81d9..3618e03 100644 --- a/src/shows/shows-api.ts +++ b/src/interfaces/show.ts @@ -1,9 +1,3 @@ -export interface ShowsApiResponse { - status: string - shows: Show[] - totalShows: number -} - export interface Show { _id: string title: string diff --git a/src/interfaces/shows-api-response.ts b/src/interfaces/shows-api-response.ts new file mode 100644 index 0000000..f0bf994 --- /dev/null +++ b/src/interfaces/shows-api-response.ts @@ -0,0 +1,7 @@ +import {Show} from './show'; + +export interface ShowsApiResponse { + status: string + shows: Show[] + totalShows: number +} diff --git a/src/interfaces/toast.ts b/src/interfaces/toast.ts new file mode 100644 index 0000000..cb029c4 --- /dev/null +++ b/src/interfaces/toast.ts @@ -0,0 +1,6 @@ +export interface Toast { + header?: string; + body: string; + delay?: number; + htmlClass?: string; +} diff --git a/src/pages/shows/shows.component.html b/src/pages/shows/shows.component.html index 5e41d99..cf63025 100644 --- a/src/pages/shows/shows.component.html +++ b/src/pages/shows/shows.component.html @@ -1 +1,19 @@ -

shows works!

+
+
+ @for (show of shows; track show._id) { +
+ +
+
{{ show.title }}
+

{{ show.year }}

+
+
+ } +
+
+
+ +
+
+
+ diff --git a/src/pages/shows/shows.component.ts b/src/pages/shows/shows.component.ts index acd48bd..c37ef3b 100644 --- a/src/pages/shows/shows.component.ts +++ b/src/pages/shows/shows.component.ts @@ -1,4 +1,11 @@ -import { Component } from '@angular/core'; +import {Component, inject} from '@angular/core'; +import {ShowsApiService} from '../../services/shows/shows-api.service'; +import {Show} from '../../interfaces/show'; +import {ShowsApiResponse} from '../../interfaces/shows-api-response'; +import {Toast} from '../../interfaces/toast'; +import {ToastService} from '../../services/toast/toast.service'; +import {NgbModal} from '@ng-bootstrap/ng-bootstrap'; +import {CreateModalComponent} from '../../components/create-modal/create-modal/create-modal.component'; @Component({ selector: 'app-shows', @@ -7,5 +14,31 @@ import { Component } from '@angular/core'; styleUrl: './shows.component.css' }) export class ShowsComponent { + private api: ShowsApiService = inject(ShowsApiService); + private toastService: ToastService = inject(ToastService); + private modalService: NgbModal = inject(NgbModal); + shows: Show[] = []; + + constructor() { + let loadToast: Toast = {body: "Loading shows..."}; + this.toastService.show(loadToast); + this.api.getShows().subscribe({ + next: (response: ShowsApiResponse) => { + this.shows = response.shows; + }, error: (err: any) => { + console.error("Error: ", err); + }, complete: () => { + let successToast: Toast = { + body: "Shows have been loaded!", + htmlClass: "bg-success text-light" + } + this.toastService.show(successToast); + } + }) + } + + createNewShow() { + this.modalService.open(CreateModalComponent) + } } diff --git a/src/shows/shows-api.service.spec.ts b/src/services/shows/shows-api.service.spec.ts similarity index 100% rename from src/shows/shows-api.service.spec.ts rename to src/services/shows/shows-api.service.spec.ts diff --git a/src/shows/shows-api.service.ts b/src/services/shows/shows-api.service.ts similarity index 77% rename from src/shows/shows-api.service.ts rename to src/services/shows/shows-api.service.ts index 9cba14c..78b3ad8 100644 --- a/src/shows/shows-api.service.ts +++ b/src/services/shows/shows-api.service.ts @@ -1,7 +1,7 @@ import {inject, Injectable} from '@angular/core'; import {HttpClient} from '@angular/common/http'; import {Observable} from 'rxjs'; -import {ShowsApiResponse} from './shows-api'; +import {ShowsApiResponse} from '../../interfaces/shows-api-response'; @Injectable({ providedIn: 'root' @@ -19,4 +19,8 @@ export class ShowsApiService { getShows(): Observable { return this.http.get(this.showsEndpoint) } + + sendShow(newShow: {}) { + return this.http.post(this.showsEndpoint, newShow) + } } diff --git a/src/services/toast/toast.service.spec.ts b/src/services/toast/toast.service.spec.ts new file mode 100644 index 0000000..e0413db --- /dev/null +++ b/src/services/toast/toast.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { ToastService } from './toast.service'; + +describe('ToastService', () => { + let service: ToastService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(ToastService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/services/toast/toast.service.ts b/src/services/toast/toast.service.ts new file mode 100644 index 0000000..d760383 --- /dev/null +++ b/src/services/toast/toast.service.ts @@ -0,0 +1,17 @@ +import { Injectable } from '@angular/core'; +import {Toast} from '../../interfaces/toast'; + +@Injectable({ + providedIn: 'root' +}) +export class ToastService { + toasts: Toast[] = []; + + show(toast: Toast) { + this.toasts.push(toast); + } + + remove(toast: Toast) { + this.toasts = this.toasts.filter(t => t != toast); + } +}