File

src/app/recipients/recipients.component.ts

Implements

OnInit OnDestroy

Metadata

selector app-recipients
styleUrls recipients.component.css
templateUrl ./recipients.component.html

Index

Properties
Methods

Constructor

constructor(templateService: TemplateService, errorService: ErrorStore)
Parameters :
Name Type Optional
templateService TemplateService no
errorService ErrorStore no

Methods

configTemplates
configTemplates(templates: Template[])
Parameters :
Name Type Optional
templates Template[] no
Returns : void
delete
delete(id: string)
Parameters :
Name Type Optional
id string no
Returns : void
loadTemplates
loadTemplates()
Returns : void
ngOnDestroy
ngOnDestroy()
Returns : void
ngOnInit
ngOnInit()
Returns : void

Properties

loaderHeader
loaderHeader: string
Type : string
Default value : 'Retrieving Data'
message
message: string
Type : string
Default value : 'Please wait a moment while we retrieve your information.'
model
model:
Default value : new RecipientListModel()
subscription
subscription: Subscription
Type : Subscription
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs/Subscription';
import { ExtendedArray } from '../extensions';
import { Template } from '../models/template.model';
import { TemplateService } from '../services';
import { ErrorStore } from '../stores';
import { RecipientListModel } from './recipientListModel';

@Component({
  selector: 'app-recipients',
  templateUrl: './recipients.component.html',
  styleUrls: ['./recipients.component.css'],
})
export class RecipientsComponent implements OnInit, OnDestroy {
  model = new RecipientListModel();
  loaderHeader = 'Retrieving Data';
  message = 'Please wait a moment while we retrieve your information.';
  subscription: Subscription;

  constructor(private templateService: TemplateService, private errorService: ErrorStore) {}

  ngOnInit() {
    this.loadTemplates();
    console.log('RecipientsComponent loaded');
  }

  ngOnDestroy(): void {
    if (this.subscription) {
      this.subscription.unsubscribe();
    }
  }

  loadTemplates() {
    this.model.isLoading = true;
    this.subscription = this.templateService.getAllTemplates().subscribe((templates) => {
      // gets from cache for fast rendering
      this.configTemplates(templates);
      this.templateService.reloadCache(); // triggers cache to reload from api
    });
  }

  delete(id: string) {
    this.loaderHeader = 'Deleting Recipient';
    this.message = 'Please wait a moment while we delete your recipient.';
    this.model.isLoading = true;
    this.templateService.deleteTemplate(id).subscribe((wasDeleted) => {
      if (!wasDeleted) {
        this.errorService.addError('Error', 'Failed to delete template');
        this.errorService.displayErrors();
        return;
      }
      this.loadTemplates();
    });
  }

  configTemplates(templates: Template[]) {
    const tmp = new ExtendedArray<Template>(templates);
    const grouped = tmp.groupBy('firstCharOfName');
    this.model.groupTitles = Object.keys(grouped);
    this.model.recipientsObj = grouped;
    this.model.isLoading = false;
  }
}
<app-empty-recipient-list *ngIf="!model.isLoading && model.count === 0"></app-empty-recipient-list>

<app-loader *ngIf="model.isLoading" [header]="loaderHeader" [message]="message"></app-loader>

<app-recipient-list *ngIf="!model.isLoading && model.recipientsObj && model.count > 0" [recipients]="model.recipientsObj"
    [groupTitles]="model.groupTitles" (delete)="delete($event)"></app-recipient-list>
Legend
Html element
Component
Html element with directive

results matching ""

    No results matching ""