Skip to content

Commit 2485902

Browse files
committed
feat: redux example
1 parent 8cf3683 commit 2485902

24 files changed

+476
-532
lines changed

angular.json

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"src/assets"
2929
],
3030
"styles": [
31+
"./node_modules/@angular/material/prebuilt-themes/indigo-pink.css",
3132
"src/styles.scss"
3233
],
3334
"scripts": []
@@ -123,6 +124,10 @@
123124
}
124125
}
125126
}
126-
}},
127-
"defaultProject": "vainilla-redux"
127+
}
128+
},
129+
"defaultProject": "vainilla-redux",
130+
"cli": {
131+
"analytics": "515d87fe-f79a-4f2c-8200-99d4b51cfc99"
132+
}
128133
}

package-lock.json

Lines changed: 33 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
"private": true,
1313
"dependencies": {
1414
"@angular/animations": "~10.0.0",
15+
"@angular/cdk": "^10.0.1",
1516
"@angular/common": "~10.0.0",
1617
"@angular/compiler": "~10.0.0",
1718
"@angular/core": "~10.0.0",
19+
"@angular/flex-layout": "^10.0.0-beta.32",
1820
"@angular/forms": "~10.0.0",
21+
"@angular/material": "^10.0.1",
1922
"@angular/platform-browser": "~10.0.0",
2023
"@angular/platform-browser-dynamic": "~10.0.0",
2124
"@angular/router": "~10.0.0",

src/app/api.service.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Injectable } from '@angular/core';
2+
import { of, Observable } from 'rxjs';
3+
import { delay } from 'rxjs/operators';
4+
5+
const data = [
6+
'🍎',
7+
'🍐',
8+
'🧀',
9+
'🍕',
10+
'🍬',
11+
'🥜',
12+
];
13+
14+
@Injectable({ providedIn: 'root'})
15+
export class ApiService {
16+
17+
constructor() { }
18+
19+
public getItems(): Observable<string[]> {
20+
return of(data).pipe(
21+
delay(2000)
22+
);
23+
}
24+
25+
}

src/app/app-state.service.ts

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { Injectable } from '@angular/core';
2+
import { Store } from './store/store.service';
3+
import {
4+
AddProduct,
5+
RemoveProduct,
6+
FilterCart,
7+
GetList,
8+
GetListSuccess,
9+
GetListFail
10+
} from './store/actions/cart.actions';
11+
12+
import { EMPTY, Subscription } from 'rxjs';
13+
import { ApiService } from './api.service';
14+
import { catchError, tap, map } from 'rxjs/operators';
15+
import { State } from './store/reducers';
16+
17+
@Injectable({
18+
providedIn: 'root',
19+
})
20+
export class AppState {
21+
22+
public cart$ = this.store.state.pipe(map((state) => state.cart));
23+
public list$ = this.cart$.pipe(map((state) => state.list));
24+
public items$ = this.cart$.pipe(map((state) => state.items));
25+
26+
constructor(
27+
public store: Store<State>,
28+
public api: ApiService
29+
) { }
30+
31+
public addProduct(product: string): void {
32+
this.store.dispatch(new AddProduct(product));
33+
}
34+
35+
public removeProduct(position: number): void {
36+
this.store.dispatch(new RemoveProduct(position));
37+
}
38+
39+
public filterList(product: string): void {
40+
this.store.dispatch(new FilterCart(product));
41+
}
42+
43+
public getProducts(): Subscription {
44+
this.store.dispatch(new GetList());
45+
46+
const request$ =
47+
this.api.getItems().pipe(
48+
tap((result) => this.store.dispatch(new GetListSuccess(result))),
49+
catchError(() => {
50+
this.store.dispatch(new GetListFail('Error'));
51+
return EMPTY;
52+
})
53+
);
54+
55+
return request$.subscribe();
56+
}
57+
58+
}

0 commit comments

Comments
 (0)