1- import { Component , OnInit , OnDestroy } from '@angular/core' ;
1+ import { Component , OnDestroy , OnInit } from '@angular/core' ;
22import { MatSnackBar } from '@angular/material/snack-bar' ;
3- import { Store , select } from '@ngrx/store' ;
4- import { Subject } from 'rxjs' ;
5- import { takeUntil } from 'rxjs/operators' ;
3+ import { select , Store } from '@ngrx/store' ;
4+ import { forkJoin , Subject } from 'rxjs' ;
5+ import { first , takeUntil } from 'rxjs/operators' ;
66
77import { ROUTE_ANIMATIONS_ELEMENTS } from '@app/core' ;
88
@@ -16,6 +16,7 @@ import {
1616import { selectTodos } from '../todos.selectors' ;
1717import { Todo , TodosFilter , TodosState } from '../todos.model' ;
1818import { State } from '../../examples.state' ;
19+ import { TranslateService } from '@ngx-translate/core' ;
1920
2021@Component ( {
2122 selector : 'anms-todos' ,
@@ -29,7 +30,11 @@ export class TodosContainerComponent implements OnInit, OnDestroy {
2930 todos : TodosState ;
3031 newTodo = '' ;
3132
32- constructor ( public store : Store < State > , public snackBar : MatSnackBar ) { }
33+ constructor (
34+ public store : Store < State > ,
35+ public snackBar : MatSnackBar ,
36+ public translateService : TranslateService
37+ ) { }
3338
3439 ngOnInit ( ) {
3540 this . store
@@ -73,26 +78,58 @@ export class TodosContainerComponent implements OnInit, OnDestroy {
7378
7479 onAddTodo ( ) {
7580 this . store . dispatch ( new ActionTodosAdd ( { name : this . newTodo } ) ) ;
76- this . showNotification ( `"${ this . newTodo } " added` ) ;
81+ this . translateService
82+ . get ( 'anms.examples.todos.added.notification' , { name : this . newTodo } )
83+ . pipe ( first ( ) )
84+ . subscribe ( addedMessage => {
85+ this . showNotification ( addedMessage ) ;
86+ } ) ;
7787 this . newTodo = '' ;
7888 }
7989
8090 onToggleTodo ( todo : Todo ) {
81- const newStatus = todo . done ? 'active' : 'done' ;
8291 this . store . dispatch ( new ActionTodosToggle ( { id : todo . id } ) ) ;
83- this . showNotification ( `Toggled "${ todo . name } " to ${ newStatus } ` , 'Undo' )
84- . onAction ( )
85- . subscribe ( ( ) => this . onToggleTodo ( { ...todo , done : ! todo . done } ) ) ;
92+ const newStatus$ = this . translateService
93+ . get ( `anms.examples.todos.filter.${ todo . done ? 'active' : 'done' } ` )
94+ . pipe ( first ( ) ) ;
95+ const undo$ = this . translateService
96+ . get ( 'anms.examples.todos.undo' )
97+ . pipe ( first ( ) ) ;
98+ const toggledMessage$ = this . translateService
99+ . get ( 'anms.examples.todos.toggle.notification' , { name : todo . name } )
100+ . pipe ( first ( ) ) ;
101+ forkJoin ( newStatus$ , undo$ , toggledMessage$ ) . subscribe (
102+ ( [ newStatus , undo , toggledMessage ] ) => {
103+ this . showNotification ( `${ toggledMessage } ${ newStatus } ` , undo )
104+ . onAction ( )
105+ . subscribe ( ( ) => this . onToggleTodo ( { ...todo , done : ! todo . done } ) ) ;
106+ }
107+ ) ;
86108 }
87109
88110 onRemoveDoneTodos ( ) {
89111 this . store . dispatch ( new ActionTodosRemoveDone ( ) ) ;
90- this . showNotification ( 'Removed done todos' ) ;
112+ this . translateService
113+ . get ( 'anms.examples.todos.remove.notification' )
114+ . pipe ( first ( ) )
115+ . subscribe ( removedMessage => {
116+ this . showNotification ( removedMessage ) ;
117+ } ) ;
91118 }
92119
93120 onFilterTodos ( filter : TodosFilter ) {
94121 this . store . dispatch ( new ActionTodosFilter ( { filter } ) ) ;
95- this . showNotification ( `Filtered to ${ filter . toLowerCase ( ) } ` ) ;
122+ const filterToMessage$ = this . translateService
123+ . get ( 'anms.examples.todos.filter.notification' )
124+ . pipe ( first ( ) ) ;
125+ const filterMessage$ = this . translateService
126+ . get ( `anms.examples.todos.filter.${ filter . toLowerCase ( ) } ` )
127+ . pipe ( first ( ) ) ;
128+ forkJoin ( filterToMessage$ , filterMessage$ ) . subscribe (
129+ ( [ filterToMessage , filterMessage ] ) => {
130+ this . showNotification ( `${ filterToMessage } ${ filterMessage } ` ) ;
131+ }
132+ ) ;
96133 }
97134
98135 private showNotification ( message : string , action ?: string ) {
0 commit comments