File

src/lib/service/inbox/inbox.service.ts

Description

InboxService handles interaction with the inbox service on the backend side.

Index

Properties
Methods

Constructor

constructor(backend: BackendService)
Parameters :
Name Type Optional
backend BackendService no

Methods

getItems
getItems()

Fetches inbox items from the backend.

Observable of inbox items.

getState
getState()

Fetches state informations for the users inbox

Returns : InboxState

inbox state object

markAsRead
markAsRead(item: InboxItem)

Marks an inbox item as read.

Parameters :
Name Type Optional Description
item InboxItem no

the item to be marked as read

Returns : Observable<any>

Observable

refreshInboxState
refreshInboxState()

Public access to fetch inbox

Returns : void
removeItem
removeItem(item: InboxItem)

Removes an item from the inbox.

Parameters :
Name Type Optional Description
item InboxItem no

inbox item to be removed

Returns : Observable<any>

Observable

Public updateInboxItems
updateInboxItems(id?: string, item?: any, multi?: any[])

Soft update for data in the inbox.

Parameters :
Name Type Optional
id string yes
item any yes
multi any[] yes
Returns : void

Properties

Public inboxItems$
inboxItems$: Observable<InboxItem[]>
Type : Observable<InboxItem[]>
Default value : this.inboxItemsGridData$.pipe(this.backend.gridDataFilter)
Public inboxItemsGridData$
inboxItemsGridData$: Observable<InboxItem[]>
Type : Observable<InboxItem[]>
Default value : this.inboxItemsSource.asObservable()
Public inboxState$
inboxState$: Observable<InboxState>
Type : Observable<InboxState>
Default value : this.inboxStateSource.asObservable()

inbox.service

import {map, tap, catchError, expand, concatMap, skipWhile, filter} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {EMPTY, Observable, ReplaySubject} from 'rxjs';
import {BackendService} from '../backend/backend.service';
import {InboxItem, InboxState} from '../../model/inbox-item.model';
import {Utils} from '../../util/utils';

/**
 * InboxService handles interaction with the inbox service on the backend side.
 */
@Injectable({
  providedIn: 'root'
})
export class InboxService {

  // backends maximum page size
  private MAX_PAGE_SIZE = 1000;

  private inboxState: InboxState;
  private inboxStateSource = new ReplaySubject<InboxState>(1);
  public inboxState$: Observable<InboxState> = this.inboxStateSource.asObservable();

  private inboxItems: InboxItem[];
  private inboxItemsSource = new ReplaySubject<InboxItem[]>(1);
  public inboxItemsGridData$: Observable<InboxItem[]> = this.inboxItemsSource.asObservable();
  public inboxItems$: Observable<InboxItem[]> = this.inboxItemsGridData$.pipe(this.backend.gridDataFilter);

  constructor(private backend: BackendService) {
  }

  /**
   * Fetches inbox items from the backend.
   *
   * @returns Observable of inbox items.
   */
  getItems(): Observable<InboxItem[]> {
    return this.getAllPages()
      .pipe(
        map(res => res.map(item => new InboxItem(item))),
        tap(res => {
          this.inboxItems = res;
          this.inboxItemsSource.next(this.inboxItems);
        }
        ),
        catchError(Utils.throw(() => this.inboxItemsSource.next([]))));
  }

  private getAllPages(): Observable<any[]> {
    let items = [];
    let pageNumber = 1;
    return this.getPage().pipe(
      expand(res => {
        return !res.last ? this.getPage(pageNumber++) : EMPTY;
      }),
      tap(res => items = [...items, ...res.content]),
      skipWhile(res => !res.last),
      map(_ => items)
    );
  }

  private getPage(index?: number) {
    return this.backend
      .getJson(`/get?size=${this.MAX_PAGE_SIZE}&page=${index || 0}`, this.backend.getInboxBase());
  }

  /**
   * Fetches state informations for the users inbox
   *
   * @returns inbox state object
   */
  getState(): InboxState {
    return this.inboxState;
  }

  /**
   * Public access to fetch inbox
   */
  refreshInboxState() {
    this.fetchInboxState();
  }

  /**
   * Retrieve inbox state
   */
  private fetchInboxState() {
    this.backend
      .getJson('/state', this.backend.getInboxBase())
      .pipe(map(res => res as InboxState))
      .subscribe(res => {
        this.inboxState = res;
        this.inboxStateSource.next(this.inboxState);
      }, Utils.logError(null, 'Failed to fetch inbox state!'));
  }

  /**
   * Removes an item from the inbox.
   *
   * @param item inbox item to be removed
   * @returns Observable<any>
   */
  removeItem(item: InboxItem): Observable<any> {
    return this.backend
      .del('/' + item.id, this.backend.getInboxBase())
      .pipe(
        tap(() => this.updateInboxItems(item.id))
      );
  }

  /**
   * Soft update for data in the inbox.
   *
   * @param id
   * @param item
   * @param multi
   */
  public updateInboxItems(id?: string, item?: any, multi?: any[]) {
    if (this.backend.update(this.inboxItems, multi || [{id, item}])) {
      this.inboxItemsSource.next(this.inboxItems);
    }
  }

  /**
   * Marks an inbox item as read.
   *
   * @param item the item to be marked as read
   * @returns Observable<any>
   */
  markAsRead(item: InboxItem): Observable<any> {
    return this.backend.put('/read/' + item.id, this.backend.getInboxBase());
  }
}

results matching ""

    No results matching ""