import { Component } from '@angular/core' ; import { Company } from '../models/Company' ; import { LetterService } from './letter.service' ; import { Letter } from '../models/Letter' ; import { Templates } from '../models/Templates' ; @ Component ( { selector : 'app-root' , templateUrl : './app.component.html' , styleUrls : [ './app.component.css' ] } ) export class AppComponent { type { string } sender = '' ; templateName = '' ; templateNames : string [ ] = [ ] ; letter : Letter ; type { { companyName : string } [ ] } companyList : Company [ ] = [ ] ; type { { companyName : string } } currentCompany : Company = new Company ; public changeTemplate ( ) { this . letter = this . getTemplate ( this . templateName ) ; } public getTemplate ( templateName ) { const templates = new Templates ( ) ; for ( const template of templates . Templates ) { if ( template . name === templateName ) { return template ; } } return null ; } returns { string } public getDate ( ) { return this . letterService . getDate ( ) ; } /**
     * saves company to localstorage
     */ public saveCompany ( ) { let existingCompany = null ; for ( const oneCompany of this . companyList ) { if ( oneCompany . companyName === this . currentCompany . companyName ) { existingCompany = oneCompany ; } } if ( existingCompany !== null ) { existingCompany . letter . push ( JSON . parse ( JSON . stringify ( this . letter ) ) ) ; } else { const newCompany = JSON . parse ( JSON . stringify ( this . currentCompany ) ) ; newCompany . letter . push ( this . letter ) ; this . companyList . push ( JSON . parse ( JSON . stringify ( newCompany ) ) ) ; } this . saveCompanyList ( ) ; this . currentCompany = new Company ( ) ; for ( const textBlock of this . letter . textblocks ) { textBlock . selected = true ; } } /**
     * saves sender to localstorage
     */ public saveSender ( ) { window . localStorage . setItem ( 'sender' , this . sender ) ; } param { Company } company public printPdf ( company , letter ) { this . letterService . createPdf ( company , letter , this . sender ) ; letter . printed = this . getDate ( ) ; this . saveCompanyList ( ) ; } public email ( company , letter ) { return this . letterService . generateEmail ( company , letter , this . sender ) ; } /**
     * saves company list in localstorage
     */ public saveCompanyList ( ) { window . localStorage . setItem ( 'companyList' , JSON . stringify ( this . companyList ) ) ; } param company public createLetter ( company ) { this . currentCompany = company ; this . letter = new Templates ( ) . Templates [ 0 ] ; this . templateName = this . letter . name ; } param company public editCompany ( company , letter ) { const companyLetters = [ ] ; for ( const singleLetter of company . letter ) { if ( singleLetter !== letter ) { companyLetters . push ( singleLetter ) ; } } company . letter = companyLetters ; this . currentCompany = company ; this . letter = letter ; if ( company . letter . length === 0 ) { const companies = [ ] ; for ( const oneCompany of this . companyList ) { if ( oneCompany . companyName !== company . companyName ) { companies . push ( oneCompany ) ; } } this . companyList = companies ; this . saveCompanyList ( ) ; } } /**
     * constructor, called when instantiated
     * load company list and sender information
     */ constructor ( public letterService ) { const storeCompanylist = window . localStorage . getItem ( 'companyList' ) ; if ( storeCompanylist !== null ) { this . companyList = JSON . parse ( storeCompanylist ) ; } const storeSender = window . localStorage . getItem ( 'sender' ) ; if ( storeSender !== null ) { this . sender = storeSender ; } const templates = new Templates ( ) ; for ( const template of templates . Templates ) { this . templateNames . push ( template . name ) ; } this . letter = templates . Templates [ 0 ] ; this . templateName = this . letter . name ; console . log ( this . letter ) ; } }