import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Store } from '@ngrx/store'; import { Observable } from 'rxjs'; import { Mail } from 'app/main/apps/mail-ngrx/mail.model'; import { MailAppState } from 'app/main/apps/mail-ngrx/store/reducers'; import { getFiltersArr, getFoldersArr, getLabelsArr, getMailsArr } from 'app/main/apps/mail-ngrx/store/selectors'; @Injectable() export class MailNgrxService { foldersArr: any; filtersArr: any; labelsArr: any; selectedMails: Mail[]; mails: Mail[]; constructor( private http: HttpClient, private store: Store ) { this.store.select(getFoldersArr).subscribe(folders => { this.foldersArr = folders; }); this.store.select(getFiltersArr).subscribe(filters => { this.filtersArr = filters; }); this.store.select(getLabelsArr).subscribe(labels => { this.labelsArr = labels; }); this.store.select(getMailsArr).subscribe(mails => { this.mails = mails; }); this.selectedMails = []; } getAllMails(): Observable { return this.http.get('api/mail-mails'); } getFolders(): Observable { return this.http.get('api/mail-folders'); } getFilters(): Observable { return this.http.get('api/mail-filters'); } getLabels(): Observable { return this.http.get('api/mail-labels'); } getMails(handle): Observable { if ( handle.id === 'labelHandle' ) { const labelId = this.labelsArr.find(label => label.handle === handle.value).id; return this.http.get('api/mail-mails?labels=' + labelId); } else if ( handle.id === 'filterHandle' ) { return this.http.get('api/mail-mails?' + handle.value + '=true'); } else // folderHandle { const folderId = this.foldersArr.find(folder => folder.handle === handle.value).id; return this.http.get('api/mail-mails?folder=' + folderId); } } /** * Update the mail * @param mail * @returns {Promise} */ updateMail(mail) { return this.http.post('api/mail-mails/' + mail.id, {...mail}); } }