Skip to content

Commit

Permalink
feat: use the new single-tweet component to fetch and display a singl…
Browse files Browse the repository at this point in the history
…e tweet
  • Loading branch information
jessepinho committed Apr 23, 2017
1 parent a72a9f0 commit 88f5cee
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const routes: Routes = [
},
{
path: 'tweet/:id',
component: TimelineComponent,
component: SingleTweetComponent,
},
],
},
Expand Down
4 changes: 1 addition & 3 deletions src/app/single-tweet/single-tweet.component.html
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
<p>
single-tweet works!
</p>
<app-tweet [tweet]="tweet" [loading]="loading"></app-tweet>
3 changes: 2 additions & 1 deletion src/app/single-tweet/single-tweet.component.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { async, ComponentFixture, TestBed } from '@angular/core/testing';

import { AppModule } from '../app.module';
import { SingleTweetComponent } from './single-tweet.component';

describe('SingleTweetComponent', () => {
Expand All @@ -8,7 +9,7 @@ describe('SingleTweetComponent', () => {

beforeEach(async(() => {
TestBed.configureTestingModule({
declarations: [ SingleTweetComponent ]
imports: [AppModule],
})
.compileComponents();
}));
Expand Down
32 changes: 29 additions & 3 deletions src/app/single-tweet/single-tweet.component.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,41 @@
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';

import { TwitterService } from '../twitter.service';

@Component({
selector: 'app-single-tweet',
templateUrl: './single-tweet.component.html',
styleUrls: ['./single-tweet.component.css']
})
export class SingleTweetComponent implements OnInit {
export class SingleTweetComponent implements OnInit, OnDestroy {
private paramsSubscription: Subscription;
private loading: boolean = false;
private tweet: Tweet;

constructor() { }
constructor(
private route: ActivatedRoute,
private service: TwitterService,
) {}

ngOnInit() {
this.paramsSubscription = this.route.params.subscribe(({ id }) => this.loadTweet(id));
}

ngOnDestroy() {
this.paramsSubscription.unsubscribe();
}

private loadTweet(id: string): void {
this.loading = true;

this.service
.getTweet(id)
.subscribe(
tweet => this.tweet = tweet,
() => {},
() => this.loading = false,
);
}
}

0 comments on commit 88f5cee

Please sign in to comment.