Skip to content

Commit 530d323

Browse files
authored
improvement(better demo): a more complete demo (#231)
1 parent 71f8e21 commit 530d323

20 files changed

Lines changed: 324 additions & 19 deletions

projects/sampleBlog/src/app/user/post/post.component.css

Whitespace-only changes.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<section *ngIf="post$ | async as post">
2+
<h4>{{ post.title }}</h4>
3+
4+
<p>{{ post.body }}</p>
5+
</section>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
2+
3+
import {PostComponent} from './post.component';
4+
5+
describe('PostComponent', () => {
6+
let component: PostComponent;
7+
let fixture: ComponentFixture<PostComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [PostComponent],
12+
}).compileComponents();
13+
}));
14+
15+
beforeEach(() => {
16+
fixture = TestBed.createComponent(PostComponent);
17+
component = fixture.componentInstance;
18+
fixture.detectChanges();
19+
});
20+
21+
it('should create', () => {
22+
expect(component).toBeTruthy();
23+
});
24+
});
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import {Component, OnInit} from '@angular/core';
2+
import {Observable, of} from 'rxjs';
3+
import {pluck, shareReplay, switchMap, catchError, tap, filter, map} from 'rxjs/operators';
4+
import {Post} from '../posts/posts.component';
5+
import {isScullyGenerated, TransferStateService} from '@scullyio/ng-lib';
6+
import {ActivatedRoute} from '@angular/router';
7+
import {HttpClient} from '@angular/common/http';
8+
9+
@Component({
10+
selector: 'app-post',
11+
templateUrl: './post.component.html',
12+
styleUrls: ['./post.component.css'],
13+
})
14+
export class PostComponent implements OnInit {
15+
postId$: Observable<number> = this.route.params.pipe(
16+
pluck('post'),
17+
filter(val => ![undefined, null].includes(val)),
18+
map(val => parseInt(val, 10)),
19+
shareReplay(1)
20+
);
21+
22+
apiPosts$ = this.postId$.pipe(
23+
switchMap(id =>
24+
this.http.get<Post>(`https://jsonplaceholder.typicode.com/posts/${id}`).pipe(
25+
catchError(() =>
26+
of({
27+
id,
28+
title: 'not found',
29+
} as Post)
30+
)
31+
)
32+
),
33+
shareReplay(1)
34+
);
35+
36+
// This is an example of using TransferState
37+
post$ = isScullyGenerated()
38+
? this.transferState.getState<Post>('post')
39+
: this.apiPosts$.pipe(tap(post => this.transferState.setState('post', post)));
40+
41+
constructor(
42+
private route: ActivatedRoute,
43+
private http: HttpClient,
44+
private transferState: TransferStateService
45+
) {
46+
console.log('post inits');
47+
}
48+
49+
ngOnInit() {}
50+
}

projects/sampleBlog/src/app/user/posts/posts.component.css

Whitespace-only changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<h4>User posts</h4>
2+
<section *ngIf="posts$ | async as posts">
3+
<ul>
4+
<li *ngFor="let post of posts">
5+
<a [routerLink]="['post', post.id]">{{ post.title }}</a>
6+
</li>
7+
</ul>
8+
</section>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import {async, ComponentFixture, TestBed} from '@angular/core/testing';
2+
3+
import {PostsComponent} from './posts.component';
4+
5+
describe('PostsComponent', () => {
6+
let component: PostsComponent;
7+
let fixture: ComponentFixture<PostsComponent>;
8+
9+
beforeEach(async(() => {
10+
TestBed.configureTestingModule({
11+
declarations: [PostsComponent],
12+
}).compileComponents();
13+
}));
14+
15+
beforeEach(() => {
16+
fixture = TestBed.createComponent(PostsComponent);
17+
component = fixture.componentInstance;
18+
fixture.detectChanges();
19+
});
20+
21+
it('should create', () => {
22+
expect(component).toBeTruthy();
23+
});
24+
});
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import {HttpClient} from '@angular/common/http';
2+
import {Component, OnInit} from '@angular/core';
3+
import {ActivatedRoute} from '@angular/router';
4+
import {isScullyGenerated, TransferStateService} from '@scullyio/ng-lib';
5+
import {Observable, of} from 'rxjs';
6+
import {catchError, filter, map, pluck, shareReplay, switchMap, tap} from 'rxjs/operators';
7+
8+
export interface Post {
9+
userId: number;
10+
id: number;
11+
title: string;
12+
body: string;
13+
}
14+
15+
@Component({
16+
selector: 'app-posts',
17+
templateUrl: './posts.component.html',
18+
styleUrls: ['./posts.component.css'],
19+
})
20+
export class PostsComponent implements OnInit {
21+
userId$: Observable<number> = this.route.params.pipe(
22+
pluck('userId'),
23+
filter(val => ![undefined, null].includes(val)),
24+
map(val => parseInt(val, 10)),
25+
shareReplay(1)
26+
);
27+
28+
apiPosts$ = this.userId$.pipe(
29+
switchMap(id =>
30+
this.http.get<Post[]>(`https://jsonplaceholder.typicode.com/posts?userId=${id}`).pipe(
31+
catchError(() =>
32+
of({
33+
id,
34+
title: 'not found',
35+
} as Post)
36+
)
37+
)
38+
),
39+
shareReplay(1)
40+
);
41+
42+
// This is an example of using TransferState
43+
posts$ = isScullyGenerated()
44+
? this.transferState.getState('posts')
45+
: this.apiPosts$.pipe(tap(posts => this.transferState.setState('posts', posts)));
46+
47+
constructor(
48+
private route: ActivatedRoute,
49+
private http: HttpClient,
50+
private transferState: TransferStateService
51+
) {}
52+
53+
ngOnInit() {}
54+
}

projects/sampleBlog/src/app/user/user-routing.module.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,21 @@ import {NgModule} from '@angular/core';
22
import {Routes, RouterModule} from '@angular/router';
33

44
import {UserComponent} from './user.component';
5+
import {UsersComponent} from './users/users.component';
6+
import {PostsComponent} from './posts/posts.component';
7+
import {PostComponent} from './post/post.component';
58

69
const routes: Routes = [
7-
{path: '', component: UserComponent},
8-
{path: ':userId', component: UserComponent},
9-
{path: ':userId/:friendCode', component: UserComponent},
10-
{path: ':userId/:posts/:comments', component: UserComponent},
10+
{path: '', component: UsersComponent},
11+
{
12+
path: ':userId',
13+
component: UserComponent,
14+
children: [
15+
{path: '', component: PostsComponent, pathMatch: 'full'},
16+
{path: 'friend/:friendCode', component: UserComponent},
17+
{path: 'post/:post', component: PostComponent},
18+
],
19+
},
1120
];
1221

1322
@NgModule({
Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,54 @@
1-
<section>user works!</section>
2-
<section>
3-
<h4>User Data ({{ userId$ | async }})</h4>
4-
<pre>
5-
<code>{{user$ | async | json}}</code>
6-
</pre>
1+
<style>
2+
#user {
3+
display: grid;
4+
grid-template-columns: 10rem 1fr;
5+
grid-template-rows: repeat(4, 1rem);
6+
}
7+
8+
#user .label {
9+
text-align: right;
10+
}
11+
12+
#user .label::after {
13+
content: ':';
14+
}
15+
16+
strong {
17+
display: inline-block;
18+
padding: 5px;
19+
border-radius: 5px;
20+
background-color: #afafaf;
21+
margin: 10px;
22+
}
23+
#sub {
24+
padding: 20px;
25+
}
26+
</style>
27+
<section *ngIf="user$ | async as user; else loading">
28+
<h4>User Data</h4>
29+
30+
<section id="user">
31+
<label>Id</label>
32+
<p>{{ user.id }}</p>
33+
<label>Name</label>
34+
<p>{{ user.name }}</p>
35+
<label>email</label>
36+
<p>{{ user.email }}</p>
37+
<label>company</label>
38+
<p>{{ user.company.name }}</p>
39+
</section>
40+
41+
<strong>{{ user.company.catchPhrase }}</strong>
742
</section>
43+
<ng-template #loading>
44+
<section>
45+
Loading ...
46+
</section>
47+
</ng-template>
848
<a [routerLink]="['/user/' + (prev$ | async)]">Previous ({{ prev$ | async }})</a>
949
<a [routerLink]="['/user/' + (next$ | async)]">Next ({{ next$ | async }})</a>
50+
51+
<hr />
52+
<section id="sub">
53+
<router-outlet></router-outlet>
54+
</section>

0 commit comments

Comments
 (0)