From 1f8b1a3f2708bd6b1d67281732a9ead5dfbab1ed Mon Sep 17 00:00:00 2001 From: mustafahlvc Date: Fri, 28 Jul 2017 12:26:56 +0300 Subject: [PATCH] Filtering updated for mail, todo app. --- src/app/core/fuseUtils.ts | 5 +++++ src/app/main/apps/mail/mail.component.ts | 19 +---------------- src/app/main/apps/mail/mail.service.ts | 26 +++++++++++++++++++++--- src/app/main/apps/todo/todo.component.ts | 19 +---------------- src/app/main/apps/todo/todo.service.ts | 22 ++++++++++++++++++++ 5 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/app/core/fuseUtils.ts b/src/app/core/fuseUtils.ts index 71bcb5a0..66f706ed 100644 --- a/src/app/core/fuseUtils.ts +++ b/src/app/core/fuseUtils.ts @@ -3,6 +3,11 @@ export class FuseUtils public static filterArrayByString(mainArr, searchText) { + if ( searchText === '' ) + { + return mainArr; + } + searchText = searchText.toLowerCase(); return mainArr.filter(itemObj => { diff --git a/src/app/main/apps/mail/mail.component.ts b/src/app/main/apps/mail/mail.component.ts index 6b0ca199..a4812a2f 100644 --- a/src/app/main/apps/mail/mail.component.ts +++ b/src/app/main/apps/mail/mail.component.ts @@ -16,15 +16,12 @@ export class MailComponent implements OnInit, OnDestroy folders: any[]; filters: any[]; labels: any[]; - mails: any[]; searchInput: FormControl; onSelectedMailsChanged: Subscription; onFoldersChanged: Subscription; onFiltersChanged: Subscription; onLabelsChanged: Subscription; - onMailsChanged: Subscription; - constructor(private mailService: MailService) { @@ -33,12 +30,6 @@ export class MailComponent implements OnInit, OnDestroy ngOnInit() { - // Subscribe to update mails on changes - this.onMailsChanged = - this.mailService.onMailsChanged - .subscribe(mails => { - this.mails = mails; - }); this.onSelectedMailsChanged = this.mailService.onSelectedMailsChanged @@ -73,15 +64,7 @@ export class MailComponent implements OnInit, OnDestroy .debounceTime(300) .distinctUntilChanged() .subscribe(searchText => { - if ( searchText !== '' ) - { - const newArr = FuseUtils.filterArrayByString(this.mails, searchText); - this.mailService.onMailsChanged.next(newArr); - } - else - { - this.mailService.getMails(); - } + this.mailService.onSearchTextChanged.next(searchText); }); } diff --git a/src/app/main/apps/mail/mail.service.ts b/src/app/main/apps/mail/mail.service.ts index ce67da3a..9b85f6cc 100644 --- a/src/app/main/apps/mail/mail.service.ts +++ b/src/app/main/apps/mail/mail.service.ts @@ -4,6 +4,7 @@ import { Observable } from 'rxjs/Observable'; import { Http } from '@angular/http'; import { Mail } from './mail.model'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; +import { FuseUtils } from "app/core/fuseUtils"; @Injectable() export class MailService implements Resolve @@ -11,6 +12,7 @@ export class MailService implements Resolve mails: Mail[]; selectedMails: Mail[]; currentMail: Mail; + searchText = ''; folders: any[]; filters: any[]; @@ -24,10 +26,9 @@ export class MailService implements Resolve onFoldersChanged: BehaviorSubject = new BehaviorSubject([]); onFiltersChanged: BehaviorSubject = new BehaviorSubject([]); onLabelsChanged: BehaviorSubject = new BehaviorSubject([]); + onSearchTextChanged: BehaviorSubject = new BehaviorSubject(''); - constructor( - private http: Http - ) + constructor(private http: Http) { this.selectedMails = []; } @@ -59,6 +60,19 @@ export class MailService implements Resolve this.setCurrentMail(null); } + this.onSearchTextChanged.subscribe(searchText => { + if ( searchText !== '' ) + { + this.searchText = searchText; + this.getMails(); + } + else + { + this.searchText = searchText; + this.getMails(); + } + }); + resolve(); }, reject @@ -154,6 +168,8 @@ export class MailService implements Resolve return new Mail(mail); }); + this.mails = FuseUtils.filterArrayByString(this.mails, this.searchText); + this.onMailsChanged.next(this.mails); resolve(this.mails); @@ -179,6 +195,8 @@ export class MailService implements Resolve return new Mail(mail); }); + this.mails = FuseUtils.filterArrayByString(this.mails, this.searchText); + this.onMailsChanged.next(this.mails); resolve(this.mails); @@ -207,6 +225,8 @@ export class MailService implements Resolve return new Mail(mail); }); + this.mails = FuseUtils.filterArrayByString(this.mails, this.searchText); + this.onMailsChanged.next(this.mails); resolve(this.mails); diff --git a/src/app/main/apps/todo/todo.component.ts b/src/app/main/apps/todo/todo.component.ts index fd921524..1fb7f0f9 100644 --- a/src/app/main/apps/todo/todo.component.ts +++ b/src/app/main/apps/todo/todo.component.ts @@ -3,7 +3,6 @@ import { Subscription } from 'rxjs/Subscription'; import { TodoService } from './todo.service'; import { FormControl } from '@angular/forms'; import { Todo } from './todo.model'; -import { FuseUtils } from '../../../core/fuseUtils'; @Component({ selector : 'fuse-todo', @@ -14,12 +13,10 @@ export class TodoComponent implements OnInit, OnDestroy { hasSelectedTodos: boolean; isIndeterminate: boolean; - todos: Todo[]; filters: any[]; tags: any[]; searchInput: FormControl; - onTodosChanged: Subscription; onSelectedTodosChanged: Subscription; onFiltersChanged: Subscription; onTagsChanged: Subscription; @@ -31,12 +28,6 @@ export class TodoComponent implements OnInit, OnDestroy ngOnInit() { - // Subscribe to update todos on changes - this.onTodosChanged = - this.todoService.onTodosChanged - .subscribe(todos => { - this.todos = todos; - }); this.onSelectedTodosChanged = this.todoService.onSelectedTodosChanged @@ -64,15 +55,7 @@ export class TodoComponent implements OnInit, OnDestroy .debounceTime(300) .distinctUntilChanged() .subscribe(searchText => { - if ( searchText !== '' ) - { - const newArr = FuseUtils.filterArrayByString(this.todos, searchText); - this.todoService.onTodosChanged.next(newArr); - } - else - { - this.todoService.getTodos(); - } + this.todoService.onSearchTextChanged.next(searchText); }); } diff --git a/src/app/main/apps/todo/todo.service.ts b/src/app/main/apps/todo/todo.service.ts index 4039a4b4..2d43f4c9 100644 --- a/src/app/main/apps/todo/todo.service.ts +++ b/src/app/main/apps/todo/todo.service.ts @@ -4,6 +4,8 @@ import { Observable } from 'rxjs/Observable'; import { Http } from '@angular/http'; import { Todo } from './todo.model'; import { BehaviorSubject } from 'rxjs/BehaviorSubject'; +import { Subscription } from 'rxjs/Subscription'; +import { FuseUtils } from '../../../core/fuseUtils'; @Injectable() export class TodoService implements Resolve @@ -11,6 +13,7 @@ export class TodoService implements Resolve todos: Todo[]; selectedTodos: Todo[]; currentTodo: Todo; + searchText = ''; filters: any[]; tags: any[]; @@ -22,6 +25,7 @@ export class TodoService implements Resolve onFiltersChanged: BehaviorSubject = new BehaviorSubject([]); onTagsChanged: BehaviorSubject = new BehaviorSubject([]); + onSearchTextChanged: BehaviorSubject = new BehaviorSubject(''); constructor(private http: Http) { @@ -55,6 +59,18 @@ export class TodoService implements Resolve this.setCurrentTodo(null); } + this.onSearchTextChanged.subscribe(searchText => { + if ( searchText !== '' ) + { + this.searchText = searchText; + this.getTodos(); + } + else + { + this.searchText = searchText; + this.getTodos(); + } + }); resolve(); }, reject @@ -129,6 +145,8 @@ export class TodoService implements Resolve return new Todo(todo); }); + this.todos = FuseUtils.filterArrayByString(this.todos, this.searchText); + this.onTodosChanged.next(this.todos); resolve(this.todos); @@ -160,6 +178,8 @@ export class TodoService implements Resolve return new Todo(todo); }); + this.todos = FuseUtils.filterArrayByString(this.todos, this.searchText); + this.onTodosChanged.next(this.todos); resolve(this.todos); @@ -188,6 +208,8 @@ export class TodoService implements Resolve return new Todo(todo); }); + this.todos = FuseUtils.filterArrayByString(this.todos, this.searchText); + this.onTodosChanged.next(this.todos); resolve(this.todos);