All files / src/app/superadmin/settings edit-custom-texts.component.ts

2.85% Statements 1/35
0% Branches 0/13
0% Functions 0/6
3.03% Lines 1/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139                                                                                                  1x                                                                                                                                                                                  
import { Component } from '@angular/core';
import { MatSnackBar } from '@angular/material/snack-bar';
import { FormBuilder, FormGroup } from '@angular/forms';
import { CustomtextService, MainDataService, customTextDefaults } from '../../shared/shared.module';
import { BackendService } from '../backend.service';
import { EditCustomTextComponent } from './edit-custom-text.component';
import { AppError, KeyValuePairs } from '../../app.interfaces';
import { AppConfig } from '../../shared/classes/app.config';
 
export interface CustomTextData {
  key: string,
  label: string,
  defaultValue: string,
  value: string
}
 
export interface CustomTextDataGroup {
  label: string,
  texts: CustomTextData[]
}
 
@Component({
  selector: 'tc-custom-texts',
  template: `
    <form [formGroup]="customTextsForm">
      <mat-accordion>
        <mat-expansion-panel *ngFor="let ctGroup of customTextGroups | keyvalue">
          <mat-expansion-panel-header>
            <mat-panel-title>
              <h4>{{ctGroup.value.label}}</h4>
            </mat-panel-title>
          </mat-expansion-panel-header>
          <tc-custom-text *ngFor="let ct of ctGroup.value.texts"
                           [parentForm]="customTextsForm"
                           [ctKey]="ct.key"
                           [ctLabel]="ct.label"
                           [ctDefaultValue]="ct.defaultValue"
                           [ctInitialValue]="ct.value"
                           (valueChange)="valueChanged($event)">
          </tc-custom-text>
          <button mat-raised-button color="primary" [disabled]="!dataChanged" (click)="saveData()">
            Speichern
          </button>
        </mat-expansion-panel>
      </mat-accordion>
    </form>
  `
})
 
export class EditCustomTextsComponent {
  customTextGroups: {
    [key in 'booklet' | 'login' | 'syscheck' | 'gm']: CustomTextDataGroup
  } = {
      booklet: {
        label: 'Testheft',
        texts: []
      },
      login: {
        label: 'Login',
        texts: []
      },
      syscheck: {
        label: 'System-Check',
        texts: []
      },
      gm: {
        label: 'Gruppenmonitor',
        texts: []
      }
    };
 
  customTextsForm: FormGroup;
  changedData: KeyValuePairs = {};
  dataChanged = false;
 
  constructor(
    private formBuilder: FormBuilder,
    private snackBar: MatSnackBar,
    private mainDataService: MainDataService,
    private backendService: BackendService,
    private customtextService: CustomtextService
  ) {
    this.customTextsForm = new FormGroup({});
 
    Object.keys(customTextDefaults)
      .forEach(ctKey => {
        const keySplits = ctKey.split('_');
        Iif (!keySplits.length) {
          return;
        }
        const groupKey = keySplits[0];
        Iif (Object.keys(this.customTextGroups).includes(groupKey)) {
          this.customTextGroups[groupKey as 'booklet' | 'login' | 'syscheck' | 'gm'].texts.push({
            key: ctKey,
            label: customTextDefaults[ctKey].label,
            defaultValue: customTextDefaults[ctKey].defaultvalue,
            value: this.mainDataService.appConfig?.customTexts[ctKey] ?? ''
          });
        }
      });
  }
 
  valueChanged(editCustomTextComponent: EditCustomTextComponent): void {
    if (editCustomTextComponent.ctInitialValue) {
      if (editCustomTextComponent.value === editCustomTextComponent.ctInitialValue) {
        Iif (this.changedData[editCustomTextComponent.ctKey]) delete this.changedData[editCustomTextComponent.ctKey];
      } else {
        this.changedData[editCustomTextComponent.ctKey] = editCustomTextComponent.value;
      }
    } else if (editCustomTextComponent.value === editCustomTextComponent.ctDefaultValue) {
      Iif (this.changedData[editCustomTextComponent.ctKey]) delete this.changedData[editCustomTextComponent.ctKey];
    } else {
      this.changedData[editCustomTextComponent.ctKey] = editCustomTextComponent.value;
    }
    this.dataChanged = Object.keys(this.changedData).length > 0;
  }
 
  saveData():void {
    this.backendService.setCustomTexts(this.changedData)
      .subscribe(() => {
        Iif (!this.mainDataService.appConfig) {
          throw new AppError({
            description: '',
            label: 'appConfig not yet loaded',
            type: 'script'
          });
        }
        this.snackBar.open(
          'Textersetzungen gespeichert', 'Info', { duration: 3000 }
        );
        this.dataChanged = false;
        Object.keys(this.changedData).forEach(ctKey => {
          (this.mainDataService.appConfig as AppConfig).customTexts[ctKey] = this.changedData[ctKey];
        });
        this.customtextService.addCustomTexts(this.changedData);
      });
  }
}