Skip to content

Commit

Permalink
Handle initial loading
Browse files Browse the repository at this point in the history
Load default city in StartupComponent or redirect to add-city if there are none.

Removing the test cities from the app component since we don't need them any more.
  • Loading branch information
anthonyoconnor committed May 29, 2018
1 parent 8a36561 commit 8e430cb
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/app/app-routing.module.ts
Expand Up @@ -2,8 +2,11 @@ import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { CityComponent } from './city/city.component';
import { AddCityComponent } from './add-city/add-city.component';
import { AppComponent } from './app.component';
import { StartupComponent } from './startup.component';

const routes: Routes = [
{ path: '', component: StartupComponent },
{ path: 'city/:city', component: CityComponent },
{ path: 'add-city', component: AddCityComponent },
];
Expand Down
11 changes: 0 additions & 11 deletions src/app/app.component.html
@@ -1,14 +1,3 @@
<div class='container'>
<router-outlet></router-outlet>

<p>Some test cities:</p>
<div>
<a routerLink="/city/vancouver">Vancouver</a>
</div>
<div>
<a routerLink="/city/limerick">Limerick</a>
</div>
<div>
<a routerLink="/city/fakecity">Fake</a>
</div>
</div>
5 changes: 5 additions & 0 deletions src/app/app.component.ts
@@ -1,10 +1,15 @@
import { Component, OnInit } from '@angular/core';
import { WeatherService } from './weather.service';
import { CityStorageService } from './city-storage.service';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.less']
})
export class AppComponent {

constructor() {
}
}
4 changes: 3 additions & 1 deletion src/app/app.module.ts
Expand Up @@ -8,12 +8,14 @@ import { HttpClientModule } from '@angular/common/http';
import { CityComponent } from './city/city.component';
import { weatherServiceProvider } from './weather.service';
import { AddCityComponent } from './add-city/add-city.component';
import { StartupComponent } from './startup.component';

@NgModule({
declarations: [
AppComponent,
CityComponent,
AddCityComponent
AddCityComponent,
StartupComponent
],
imports: [
BrowserModule,
Expand Down
1 change: 0 additions & 1 deletion src/app/city/city.component.ts
Expand Up @@ -19,7 +19,6 @@ export class CityComponent implements OnInit {
}

ngOnInit() {

this.route.paramMap.subscribe(route => {
this.city = route.get('city');
this.reset();
Expand Down
20 changes: 20 additions & 0 deletions src/app/startup.component.ts
@@ -0,0 +1,20 @@
import { Component, OnInit } from '@angular/core';
import { WeatherService } from './weather.service';
import { CityStorageService } from './city-storage.service';
import { Router, ActivatedRoute } from '@angular/router';

@Component({
template: ``, // Nothing will show on this component.
})
export class StartupComponent {

constructor(private cityStorage: CityStorageService, private router: Router) {
if (this.cityStorage.cities.length === 0) {
this.router.navigate(['/add-city/']);
} else {
const defaultCity = this.cityStorage.cities[0];
console.log('going to default');
this.router.navigate(['/city/' + defaultCity]);
}
}
}

0 comments on commit 8e430cb

Please sign in to comment.