Skip to content

Commit 5d016fc

Browse files
author
Armen Vardanyan
committed
Fixes
1 parent 8a53bc2 commit 5d016fc

19 files changed

+107
-33
lines changed

database.json

+12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@
1414
},
1515
{
1616
"id": 3
17+
},
18+
{
19+
"name": "dfvdfvdfv",
20+
"id": 4
21+
},
22+
{
23+
"name": "dsfsf",
24+
"id": 5
25+
},
26+
{
27+
"name": "Cate4gory",
28+
"id": 6
1729
}
1830
],
1931
"expenses": [

package-lock.json

+3-13
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
"@angular/platform-browser": "~9.0.0",
2222
"@angular/platform-browser-dynamic": "~9.0.0",
2323
"@angular/router": "~9.0.0",
24-
"@ngrx/effects": "^12.2.0",
24+
"@ngrx/effects": "^9.2.1",
2525
"@ngrx/store": "^9.1.2",
2626
"rxjs": "~6.5.4",
2727
"tslib": "^1.10.0",

src/app/app.module.ts

+9-1
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,18 @@ import { StoreModule } from '@ngrx/store';
55
import { MatListModule } from '@angular/material/list';
66
import { MatInputModule } from '@angular/material/input';
77
import { MatButtonModule } from '@angular/material/button';
8+
import { MatSnackBarModule } from '@angular/material/snack-bar';
89

910
import { AppRoutingModule } from './app-routing.module';
1011
import { AppComponent } from './app.component';
1112
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
1213
import { categoryReducer } from './state/reducer';
1314
import { CategoryListContainerComponent } from './category-list/category-list-container/category-list-container.component';
1415
import { CategoryListPresenterComponent } from './category-list/category-list-presenter/category-list-presenter.component';
16+
import { EffectsModule } from '@ngrx/effects';
17+
import { CategoriesEffects } from './state/effects';
18+
import { CategoryService } from './services/category.service';
19+
import { HttpClientModule } from '@angular/common/http';
1520

1621
@NgModule({
1722
declarations: [
@@ -24,12 +29,15 @@ import { CategoryListPresenterComponent } from './category-list/category-list-pr
2429
AppRoutingModule,
2530
BrowserAnimationsModule,
2631
StoreModule.forRoot({categories: categoryReducer}),
32+
EffectsModule.forRoot([CategoriesEffects]),
2733
MatListModule,
2834
MatInputModule,
2935
MatButtonModule,
36+
MatSnackBarModule,
37+
HttpClientModule,
3038
FormsModule,
3139
],
32-
providers: [],
40+
providers: [CategoryService],
3341
bootstrap: [AppComponent]
3442
})
3543
export class AppModule { }

src/app/category-list/category-list-container/category-list-container.component.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export class CategoryListContainerComponent implements OnInit {
2929
this.store.dispatch(addCategory({payload: category})); // this is where magic happens
3030
}
3131

32-
deleteCategory(categoryName: string) {
33-
this.store.dispatch(deleteCategory({payload: categoryName}));
32+
deleteCategory(categoryId: number) {
33+
this.store.dispatch(deleteCategory({payload: categoryId}));
3434
}
3535

3636
}

src/app/category-list/category-list-presenter/category-list-presenter.component.html

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<mat-list>
22
<mat-list-item *ngFor="let category of categories">
33
{{ category.name }}
4-
<button mat-button (click)="deleteCategory(category.name)">Delete</button>
4+
<button mat-button (click)="deleteCategory(category.id)">Delete</button>
55
</mat-list-item>
66
</mat-list>
77
<mat-form-field>

src/app/logs/logs.component.html

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<p>
2+
logs works!
3+
</p>

src/app/logs/logs.component.scss

Whitespace-only changes.

src/app/logs/logs.component.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Component, OnInit } from '@angular/core';
2+
3+
@Component({
4+
selector: 'app-logs',
5+
templateUrl: './logs.component.html',
6+
styleUrls: ['./logs.component.scss']
7+
})
8+
export class LogsComponent implements OnInit {
9+
10+
constructor() { }
11+
12+
ngOnInit() {
13+
}
14+
15+
}

src/app/logs/logs.module.ts

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { NgModule } from '@angular/core';
2+
import { CommonModule } from '@angular/common';
3+
import { LogsComponent } from './logs.component';
4+
5+
@NgModule({
6+
imports: [
7+
CommonModule
8+
],
9+
declarations: [LogsComponent]
10+
})
11+
export class LogsModule { }

src/app/logs/state/actions.ts

Whitespace-only changes.

src/app/logs/state/effects.ts

Whitespace-only changes.

src/app/logs/state/reducer.ts

Whitespace-only changes.

src/app/logs/state/selectors.ts

Whitespace-only changes.

src/app/logs/state/state.ts

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export enum LogType {
2+
Spend = 'Spend',
3+
Income = 'Income',
4+
}
5+
6+
export interface Log {
7+
title: string;
8+
date: string;
9+
amount: number;
10+
type: LogType;
11+
categoryId: number;
12+
}
13+
14+
export interface LogsState {
15+
logs: Log[];
16+
loading: {
17+
list: boolean;
18+
add: boolean;
19+
};
20+
}
21+
22+
export const initialState: LogsState = {
23+
logs: [],
24+
loading: {
25+
list: false,
26+
add: false,
27+
},
28+
};

src/app/state/actions.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { Category } from './models';
44
export const addCategory = createAction('[Category List] Add Category', props<{payload: {name: string}}>());
55
export const deleteCategory = createAction('[Category List] Delete Category', props<{payload: number}>());
66

7-
export const addCategorySuccess = createAction('[Category List] Add Category Success', props<{payload: Category}>());
7+
export const addCategorySuccess = createAction('[Category List] Add Category Success', props<{payload: {data: Category, message?: string}}>());
88

99
export const addCategoryError = createAction('[Category List] Add Category Error');
1010

1111
export const categoriesListLoaded = createAction('[Category List] Categories List Loaded');
1212
export const loadCategoriesSuccess = createAction('[Category List] Load Categories Success', props<{payload: Category[]}>());
1313
export const loadCategoriesError = createAction('[Category List] Load Categories Error');
1414

15-
export const deleteCategorySuccess = createAction('[Category List] Delete Category Success', props<{payload: number}>());
15+
export const deleteCategorySuccess = createAction('[Category List] Delete Category Success', props<{payload: {data: number, message?: string}}>());
1616
export const deleteCategoryError = createAction('[Category List] Delete Category Error');

src/app/state/effects.ts

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1+
import { Injectable } from '@angular/core';
12
import { Actions, createEffect, ofType } from '@ngrx/effects';
3+
import { MatSnackBar } from '@angular/material/snack-bar';
24
import { of } from 'rxjs';
3-
import { mergeMap, map, catchError } from 'rxjs/operators';
5+
import { mergeMap, map, catchError, tap } from 'rxjs/operators';
46

57
import { CategoryService } from '../services/category.service';
68
import { categoriesListLoaded, loadCategoriesSuccess, loadCategoriesError, deleteCategory, deleteCategorySuccess, addCategory, addCategorySuccess, addCategoryError } from './actions';
79

10+
@Injectable()
811
export class CategoriesEffects {
912

1013
categoriesListLoaded$ = createEffect(() => this.actions$.pipe(
@@ -15,25 +18,31 @@ export class CategoriesEffects {
1518
))
1619
));
1720

18-
deleteCategory$ = createEffect(() => this.actions$.pipe(
19-
ofType(deleteCategory),
20-
mergeMap(({payload}) => this.categoriesService.deleteCategory(payload).pipe(
21-
map(() => deleteCategorySuccess({payload})),
22-
catchError(() => of(loadCategoriesError())),
23-
))
24-
));
21+
// deleteCategory$ = createEffect(() => this.actions$.pipe(
22+
// ofType(deleteCategory),
23+
// mergeMap(({payload}) => this.categoriesService.deleteCategory(payload).pipe(
24+
// map(() => deleteCategorySuccess({payload})),
25+
// catchError(() => of(loadCategoriesError())),
26+
// ))
27+
// ));
2528

2629
addCategory$ = createEffect(() => this.actions$.pipe(
2730
ofType(addCategory),
2831
mergeMap(({payload}) => this.categoriesService.addCategory(payload).pipe(
29-
map((result) => addCategorySuccess({payload: result})),
32+
map((result) => addCategorySuccess({payload: {data: result, message: 'Category successfully added'}})),
3033
catchError(() => of(addCategoryError())),
3134
)),
3235
));
3336

37+
handleSuccessMessage$ = createEffect(() => this.actions$.pipe(
38+
ofType(addCategorySuccess, deleteCategorySuccess),
39+
tap(({payload}) => this.snackBar.open(payload.message, 'Dismiss', {duration: 2000})),
40+
));
41+
3442
constructor(
3543
private readonly actions$: Actions,
3644
private readonly categoriesService: CategoryService,
45+
private readonly snackBar: MatSnackBar,
3746
) { }
3847

3948
}

src/app/state/reducer.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { CategoryState, initialState } from './state';
66
const _reducer = createReducer(
77
initialState,
88
on(addCategory, (state, action) => ({...state, categories: [...state.list, action.payload]})),
9-
on(loadCategoriesSuccess, (state, action) => ({...state, categories: action.payload})),
9+
on(loadCategoriesSuccess, (state, action) => ({...state, list: action.payload})),
1010
);
1111

1212
export function categoryReducer(state: CategoryState, action: Action) {

src/app/state/state.ts

+1-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,5 @@ export interface AppState {
99
}
1010

1111
export const initialState: CategoryState = {
12-
list: [
13-
{ name: 'Food' },
14-
],
12+
list: [],
1513
};

0 commit comments

Comments
 (0)