diff --git a/src/app/app.module.ts b/src/app/app.module.ts index fa3d75f7..61711dc3 100644 --- a/src/app/app.module.ts +++ b/src/app/app.module.ts @@ -67,7 +67,13 @@ const MATERIAL_MODULES = [ ReactiveFormsModule, ...MATERIAL_MODULES, RouterModule.forRoot([ - { path: 'listings', children: [{ path: ':listingId', component: ListingComponent }] }, + { + path: 'listings', + children: [ + { path: ':listingId', component: ListingComponent }, + { path: 'new', component: ListingComponent, pathMatch: 'full' }, + ], + }, { path: '**', pathMatch: 'full', redirectTo: '/' }, ]), NgxSubFormModule, diff --git a/src/app/main/listing/listing.component.html b/src/app/main/listing/listing.component.html index c74bf651..6a84045e 100644 --- a/src/app/main/listing/listing.component.html +++ b/src/app/main/listing/listing.component.html @@ -6,21 +6,22 @@
Photo of {{ listingForm.value?.listing?.title }}
- - - - {{ listingType.value }} - - - -
+ + + + {{ listingType.value }} + + + +
params.get('listingId')), takeUntil(this.onDestroy$), - switchMap(listingId => this.listingService.getOneListing(listingId)), + switchMap(listingId => { + if (listingId === 'new') { + return of(null); + } + return this.listingService.getOneListing(listingId); + }), tap(listing => { this.listingForm.get('listing').patchValue(listing); - this.selectListingType.patchValue(listing.listingType); + this.listingForm.get('listingType').patchValue(listing ? listing.listingType : null); }), ) .subscribe(); diff --git a/src/app/main/listings/listings.component.html b/src/app/main/listings/listings.component.html index 04830d04..0887235b 100644 --- a/src/app/main/listings/listings.component.html +++ b/src/app/main/listings/listings.component.html @@ -39,3 +39,5 @@

{{ listing.title }} ({{ listing.listingType }}) £{{ listing.price

+ + Create new diff --git a/src/app/services/listing.service.ts b/src/app/services/listing.service.ts index 2fa0a37c..6987202d 100644 --- a/src/app/services/listing.service.ts +++ b/src/app/services/listing.service.ts @@ -2,7 +2,7 @@ import { Injectable } from '@angular/core'; import { BehaviorSubject, Observable } from 'rxjs'; import { OneListing } from '../interfaces/listing.interface'; import { hardCodedListings } from './listings.data'; -import { map } from 'rxjs/operators'; +import { map, tap } from 'rxjs/operators'; @Injectable({ providedIn: 'root', @@ -31,6 +31,11 @@ export class ListingService { public getOneListing(id: string): Observable { return this.listings$.pipe( map(listings => listings.find(s => s.id === id)), + tap(listing => { + if (!listing) { + throw new Error('not found'); + } + }), map(this.listingDeepCopy), ); }