Filtering updated for mail, todo app.

This commit is contained in:
mustafahlvc 2017-07-28 12:26:56 +03:00
parent 2d459864f1
commit 1f8b1a3f27
5 changed files with 52 additions and 39 deletions

View File

@ -3,6 +3,11 @@ export class FuseUtils
public static filterArrayByString(mainArr, searchText)
{
if ( searchText === '' )
{
return mainArr;
}
searchText = searchText.toLowerCase();
return mainArr.filter(itemObj => {

View File

@ -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);
});
}

View File

@ -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<any>
@ -11,6 +12,7 @@ export class MailService implements Resolve<any>
mails: Mail[];
selectedMails: Mail[];
currentMail: Mail;
searchText = '';
folders: any[];
filters: any[];
@ -24,10 +26,9 @@ export class MailService implements Resolve<any>
onFoldersChanged: BehaviorSubject<any> = new BehaviorSubject([]);
onFiltersChanged: BehaviorSubject<any> = new BehaviorSubject([]);
onLabelsChanged: BehaviorSubject<any> = new BehaviorSubject([]);
onSearchTextChanged: BehaviorSubject<any> = new BehaviorSubject('');
constructor(
private http: Http
)
constructor(private http: Http)
{
this.selectedMails = [];
}
@ -59,6 +60,19 @@ export class MailService implements Resolve<any>
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<any>
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<any>
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<any>
return new Mail(mail);
});
this.mails = FuseUtils.filterArrayByString(this.mails, this.searchText);
this.onMailsChanged.next(this.mails);
resolve(this.mails);

View File

@ -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);
});
}

View File

@ -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<any>
@ -11,6 +13,7 @@ export class TodoService implements Resolve<any>
todos: Todo[];
selectedTodos: Todo[];
currentTodo: Todo;
searchText = '';
filters: any[];
tags: any[];
@ -22,6 +25,7 @@ export class TodoService implements Resolve<any>
onFiltersChanged: BehaviorSubject<any> = new BehaviorSubject([]);
onTagsChanged: BehaviorSubject<any> = new BehaviorSubject([]);
onSearchTextChanged: BehaviorSubject<any> = new BehaviorSubject('');
constructor(private http: Http)
{
@ -55,6 +59,18 @@ export class TodoService implements Resolve<any>
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<any>
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<any>
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<any>
return new Todo(todo);
});
this.todos = FuseUtils.filterArrayByString(this.todos, this.searchText);
this.onTodosChanged.next(this.todos);
resolve(this.todos);