Skip to content

Commit 5eaf815

Browse files
Show Component:
* fetch data in store * use store in show component
1 parent 245875c commit 5eaf815

File tree

17 files changed

+176
-136
lines changed

17 files changed

+176
-136
lines changed
Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
import {Component, Input} from '@angular/core';
22
import {NgForOf} from "@angular/common";
33
import {RouterLink} from "@angular/router";
4-
import {Hero} from "../../../interface/hero.model";
5-
import {List} from "../../../interface/list";
6-
import {selectorHeroesLoading} from "../../../store/selector/hero.selectors";
7-
import {Store} from "@ngrx/store";
4+
import {List} from "../../../interface/list.model";
85

96
@Component({
107
selector: 'app-table',
@@ -17,9 +14,4 @@ import {Store} from "@ngrx/store";
1714
})
1815
export class TableComponent {
1916
@Input() items: List | undefined
20-
21-
isLoading = this.store.select(selectorHeroesLoading)
22-
23-
constructor(private store: Store<{ heroes: List }>) {
24-
}
2517
}

src/app/components/foo/list/list.component.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import {Component, OnInit} from '@angular/core';
22
import {RouterLink} from "@angular/router";
33
import {AsyncPipe, NgFor, NgIf} from "@angular/common";
44
import {TableComponent} from "../../common/table/table.component";
5-
import {Observable} from "rxjs";
6-
import {List} from "../../../interface/list";
7-
import {select, Store} from "@ngrx/store";
5+
import {List} from "../../../interface/list.model";
6+
import {Store} from "@ngrx/store";
87
import {HeroesActions} from "../../../store/action/heroes.actions";
98
import {HeroService} from "../../../service/hero.service";
10-
import {heroesReducer} from "../../../store/reducer/heroes.reducer";
119
import {selectorHeroesLoading} from "../../../store/selector/hero.selectors";
1210

1311
@Component({
@@ -25,16 +23,16 @@ import {selectorHeroesLoading} from "../../../store/selector/hero.selectors";
2523
export class ListComponent implements OnInit {
2624
heroes$ = this.store.select('heroes')
2725
isLoading = this.store.select(selectorHeroesLoading)
26+
2827
constructor(private store: Store<{ heroes: List }>, private heroService: HeroService) {
29-
this.heroes$ = store.select('heroes')
3028
}
3129

3230
ngOnInit() {
3331
this.heroService
3432
.getHeroes()
3533
.subscribe(
3634
(items) =>
37-
this.store.dispatch(HeroesActions.getHeroes({items, isLoading: false}))
35+
this.store.dispatch(HeroesActions.getHeroes({items: items['hydra:member']}))
3836
)
3937
}
4038
}
Lines changed: 45 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,51 @@
11
<div class="container mx-auto px-4 max-w-2xl mt-4">
2-
<!--<div class="flex items-center justify-between">
3-
<a [routerLink]="['/heroes']"
4-
class="text-blue-600 hover:text-blue-800">
5-
Back to list
6-
</a>
7-
<div>
8-
<a [routerLink]="['/heroes/edit', item?.id]"
9-
class="px-6 py-2 mr-2 bg-green-600 text-white text-xs rounded shadow-md hover:bg-green-700">
10-
Edit
2+
<p *ngIf="(isLoading | async) else elseData">Loading ... </p>
3+
<ng-template #elseData>
4+
<div class="flex items-center justify-between">
5+
<a [routerLink]="['/heroes']"
6+
class="text-blue-600 hover:text-blue-800">
7+
Back to list
118
</a>
12-
<button
13-
(click)="delete()"
14-
class="px-6 py-2 bg-red-600 text-white text-xs rounded shadow-md hover:bg-red-700">
15-
Delete
16-
</button>
9+
<div>
10+
<a [routerLink]="['/heroes/edit', (item|async)?.id]"
11+
class="px-6 py-2 mr-2 bg-green-600 text-white text-xs rounded shadow-md hover:bg-green-700">
12+
Edit
13+
</a>
14+
<button
15+
class="px-6 py-2 bg-red-600 text-white text-xs rounded shadow-md hover:bg-red-700">
16+
Delete
17+
</button>
18+
</div>
1719
</div>
18-
</div>
1920

20-
<h1 class="text-3xl my-4">Show {{ item?.["@id"] }}</h1>
21+
<h1 class="text-3xl my-4">Show {{ (item|async)?.["@id"] }}</h1>
2122

22-
<div *ngIf="item" class="overflow-x-auto">
23-
<table class="min-w-full">
24-
<thead class="border-b">
25-
<tr>
26-
<th scope="col" class="text-sm font-medium px-6 py-4 text-left">
27-
Field
28-
</th>
29-
<th scope="col" class="text-sm font-medium px-6 py-4 text-left">
30-
Value
31-
</th>
32-
</tr>
33-
</thead>
34-
<tbody>
35-
<tr class="border-b">
36-
<th
37-
class="text-sm font-medium px-6 py-4 text-left capitalize"
38-
scope="row"
39-
>
40-
name
41-
</th>
42-
<td class="px-6 py-4 whitespace-nowrap text-sm">
43-
{{item.name}}
44-
</td>
45-
</tbody>
46-
</table>
47-
</div>-->
48-
coucou
23+
<div class="overflow-x-auto">
24+
<table class="min-w-full">
25+
<thead class="border-b">
26+
<tr>
27+
<th scope="col" class="text-sm font-medium px-6 py-4 text-left">
28+
Field
29+
</th>
30+
<th scope="col" class="text-sm font-medium px-6 py-4 text-left">
31+
Value
32+
</th>
33+
</tr>
34+
</thead>
35+
<tbody>
36+
<tr *ngIf="(item | async)"class="border-b">
37+
<th
38+
class="text-sm font-medium px-6 py-4 text-left capitalize"
39+
scope="row"
40+
>
41+
name
42+
</th>
43+
<td class="px-6 py-4 whitespace-nowrap text-sm">
44+
{{ (item|async)?.name }}
45+
</td>
46+
</tr>
47+
</tbody>
48+
</table>
49+
</div>
50+
</ng-template>
4951
</div>
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
import {Component} from '@angular/core';
2-
import { RouterLink} from "@angular/router";
3-
import {CommonModule, NgIf} from "@angular/common";
1+
import {Component, OnInit} from '@angular/core';
2+
import {ActivatedRoute, RouterLink} from "@angular/router";
3+
import {CommonModule, NgIf} from "@angular/common";
44
import {Store} from "@ngrx/store";
55
import {HeroService} from "../../../service/hero.service";
6+
import {Show} from "../../../interface/show.model";
7+
import {selectorShowItem, selectorShowLoading} from "../../../store/selector/show.selectors";
8+
import {HeroesActions} from "../../../store/action/heroes.actions";
69

710
@Component({
811
selector: 'app-show',
@@ -14,28 +17,22 @@ import {HeroService} from "../../../service/hero.service";
1417
],
1518
templateUrl: './show.component.html',
1619
})
17-
export class ShowComponent {
20+
export class ShowComponent implements OnInit {
21+
isLoading = this.store.select(selectorShowLoading)
22+
item = this.store.select(selectorShowItem)
1823

19-
constructor( private store: Store, private heroService: HeroService) {}
20-
ngOnInit() {
21-
/* this.heroService
22-
.getHeroes()
23-
.subscribe(
24-
(heroes) => this.store.dispatch(HeroesActions.getHeroes({ heroes }))
25-
)*/
24+
constructor(
25+
private store: Store<{ show: Show }>,
26+
private heroService: HeroService,
27+
private router: ActivatedRoute
28+
) {
2629
}
2730

28-
/*delete() {
29-
if (window.confirm(`Are you sure you want to delete this ${this.item?.["@id"]}`)) {
30-
this.deleteItemApi()
31-
.then(() => this.location.back())
32-
}
31+
ngOnInit() {
32+
const id = this.router.snapshot.paramMap.get('id')
33+
this.heroService
34+
.getHero(id)
35+
.subscribe((item) =>
36+
this.store.dispatch(HeroesActions.getHero({item})))
3337
}
34-
35-
async deleteItemApi() {
36-
const idRoute = this.router.snapshot.paramMap.get('id')
37-
await fetchApi(`/heroes/${idRoute}`, {
38-
method: 'DELETE'
39-
})
40-
}*/
4138
}

src/app/interface/api.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@ export interface Api {
44
"@context": String,
55
"@id": String,
66
"@type": String,
7+
}
8+
9+
export interface ApiList extends Api {
710
"hydra:totalItems": Number,
811
"hydra:member": Hero[]
912
}
13+
14+
export interface ApiShow extends Api {
15+
id: Number,
16+
name: String
17+
}

src/app/interface/book.model.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.

src/app/interface/hero.model.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
export interface Hero {
2-
"@context": String,
3-
"@id": String,
4-
"@type": String,
1+
import {Api} from "./api";
2+
3+
export interface Hero extends Api {
54
id: Number,
65
name: String
76
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import {Hero} from "./hero.model";
22

33
export interface List {
4-
isLoading?: Boolean,
4+
isLoading: Boolean | undefined | null,
55
items?: Hero[],
66
error?: String,
77
}

src/app/interface/show.model.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import {Hero} from "./hero.model";
2+
3+
export interface Show {
4+
isLoading?: Boolean,
5+
item?: Hero,
6+
error?: String,
7+
}

src/app/service/books.service.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/app/service/hero.service.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,34 @@
11
import {HttpClient} from "@angular/common/http";
2-
import {map} from "rxjs/operators";
3-
import {Hero} from "../interface/hero.model";
42
import {Observable} from "rxjs";
53
import {Injectable} from "@angular/core";
6-
import {List} from "../interface/list";
7-
import {Api} from "../interface/api";
4+
import {List} from "../interface/list.model";
5+
import {ApiList, ApiShow} from "../interface/api";
86
import {Store} from "@ngrx/store";
97
import {HeroesActions} from "../store/action/heroes.actions";
8+
import {Show} from "../interface/show.model";
109

1110
@Injectable({providedIn: 'root'})
1211
export class HeroService {
13-
heroes$ = this.store.select('heroes')
12+
baseUrl: string = 'https://localhost'
1413

1514
constructor(
1615
private http: HttpClient,
17-
private store: Store<{ heroes: List }>
16+
private store: Store<{
17+
heroes: List,
18+
show: Show
19+
}>
1820
) {
19-
this.heroes$ = store.select('heroes')
2021
}
2122

22-
getHeroes(): Observable<Hero[]> {
23+
getHeroes(): Observable<ApiList> {
2324
this.store.dispatch(HeroesActions.getHeroes({isLoading: true}))
2425
return this.http
25-
.get<Api>('https://localhost/heroes')
26-
.pipe(map(result => result['hydra:member']))
26+
.get<ApiList>(this.baseUrl + '/heroes')
27+
}
28+
29+
getHero(id: string | null) {
30+
this.store.dispatch(HeroesActions.getHero({isLoading: true}))
31+
return this.http
32+
.get<ApiShow>(this.baseUrl + `/heroes/${id}`)
2733
}
2834
}

src/app/store/action/heroes.actions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
import {createActionGroup, emptyProps, props} from "@ngrx/store";
1+
import {createActionGroup, props} from "@ngrx/store";
22
import {Hero} from "../../interface/hero.model";
33

44
export const HeroesActions = createActionGroup({
55
source: 'Heroes',
66
events: {
7-
'Get Heroes': props<{items?: Array<Hero>, isLoading?: Boolean}>(),
8-
'Get Hero': props<{ hero: Hero }>(),
7+
'Get Heroes': props<{ items?: Array<Hero>, isLoading?: Boolean, error?: String }>(),
8+
'Get Hero': props<{ item?: Hero, isLoading?: Boolean, error?: String }>(),
99
'Put Hero': props<{ hero: Hero }>(),
1010
'Delete Hero': props<{ id: Number }>(),
1111
}
Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
import {createReducer, on} from '@ngrx/store'
22
import {HeroesActions} from "../action/heroes.actions";
3-
import fetchApi from '../../../utils/api'
4-
import {List} from "../../interface/list";
3+
import {List} from "../../interface/list.model";
4+
import {Show} from "../../interface/show.model";
55

6-
export const initialState: List = {
6+
export const initialStateList: List = {
77
isLoading: false,
88
items: [],
99
error: '',
1010
}
11+
1112
export const heroesReducer = createReducer(
12-
initialState,
13+
initialStateList,
1314
on(HeroesActions.getHeroes,
14-
(_state, {items, isLoading}) => ({
15+
(_state, {items, isLoading, error}) => ({
1516
..._state,
1617
items,
17-
isLoading
18+
isLoading,
19+
error
1820
}))
1921
)

src/app/store/reducer/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import {heroesReducer} from "./heroes.reducer";
2+
import {showReducer} from "./show.reducer";
23

34
export default {
4-
heroes: heroesReducer
5+
heroes: heroesReducer,
6+
show: showReducer
57
}

src/app/store/reducer/show.reducer.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import {Show} from "../../interface/show.model";
2+
import {createReducer, on} from "@ngrx/store";
3+
import {HeroesActions} from "../action/heroes.actions";
4+
5+
export const initialStateShow: Show = {
6+
isLoading: false,
7+
item: undefined,
8+
error: ''
9+
}
10+
11+
export const showReducer = createReducer(
12+
initialStateShow,
13+
on(HeroesActions.getHero,
14+
(_state, {item, isLoading = false, error = ''}) => ({
15+
..._state,
16+
item,
17+
isLoading,
18+
error
19+
})
20+
)
21+
)

0 commit comments

Comments
 (0)