All files / src/app/components/timetrack/work-time/break-tracker break-tracker.component.ts

78.68% Statements 48/61
48.38% Branches 15/31
64.7% Functions 11/17
78.57% Lines 44/56

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 140 141 142 143 144 145 146 147 1484x 4x 4x 4x 4x 4x                                         4x               1x   9x               1x   9x               9x   9x               11x 9x   9x   9x   9x                       11x       4x 4x 4x 2x         2x   2x 1x   1x 1x 1x     4x       4x         4x       2x 2x   2x       2x 2x       1x   1x     1x     1x 1x   1x   1x        
import { CommonModule } from "@angular/common";
import { ChangeDetectorRef, Component, Input, OnChanges } from "@angular/core";
import { ControlValueAccessor, FormsModule, NG_VALUE_ACCESSOR, ValidationErrors } from "@angular/forms";
import { TranslateModule } from "@ngx-translate/core";
import { TimeInputComponent } from "@shared/components/time-input/time-input.component";
import { getCurrentTime } from "@shared/helper/utils";
import { WorkBreak } from "../work-time";
 
@Component({
    selector: "app-break-tracker",
    standalone: true,
    imports: [
        CommonModule,
        FormsModule,
        TimeInputComponent,
        TranslateModule,
    ],
    providers: [
        {
            provide: NG_VALUE_ACCESSOR,
            useExisting: BreakTrackerComponent,
            multi: true,
        },
    ],
    templateUrl: "./break-tracker.component.html",
})
export class BreakTrackerComponent implements OnChanges, ControlValueAccessor {
    //Optional inputs
    /**
     * Minimal Starttime for breaks (in minutes)
     * @default 0
     * @memberof BreakTrackerComponent
     */
    @Input() set start (val: number | undefined) {
        this._start = val ?? 0;
    }
    _start = 0;
 
    /**
     * Maximum Endtime for breaks (in minutes)
     * @default 1439 (equals 23:59)
     * @memberof BreakTrackerComponent
     */
    @Input() set end (val: number | undefined) {
        this._end = val ?? 1439;
    }
    _end = 1439;
 
    @Input() errors: ValidationErrors | undefined | null;
 
    /**
     * Disable any changes
     * @memberof BreakTrackerComponent
     */
    disabled = false;
 
    breaks: WorkBreak[] = [];
 
    // Reactive Form (instead of Model Binding)
    writeValue (breaks: WorkBreak[]) {this.breaks = breaks;}
    registerOnChange (fn: (value: WorkBreak[] | undefined) => void) {this.onChangeCallback = fn;}
    registerOnTouched (fn: () => void) {this.onTouchedCallback = fn;}
    setDisabledState (disabled: boolean) {this.disabled = disabled;}
 
    onChangeCallback: (value: WorkBreak[] | undefined) => void = () => null;
    onTouchedCallback: () => void = () => null;
 
    isOnBreak = false;
    currentBreak!: WorkBreak;
    noInsertPause = false;
 
    constructor (private readonly cdr: ChangeDetectorRef) {}
 
    // Lifecycle Hook
    ngOnChanges () {
        const noWorkStart = this._start === undefined || this._end === undefined;
        this.noInsertPause = noWorkStart || this.disabled; 
    }
 
    /**
     * Fired on input change 
     * Pass current value as output
     */
    onChange = () => this.onChangeCallback(this.breaks);
 
    // UI Methods
    toggleBreak () {
        this.isOnBreak = !this.isOnBreak;
        const currentTime = getCurrentTime(); 
        if(this.isOnBreak) {
            this.currentBreak = {
                start: currentTime,
                end: undefined,
                isOn: true,
            };
            this.breaks.push(this.currentBreak);
        } else {
            if(this.currentBreak.start === currentTime) {
                this.removeBreak(this.currentBreak);
            } else {
                this.currentBreak.end = getCurrentTime();
                this.currentBreak.isOn = false;
                this.onChange();
            }
        }
        this.onChange();
    }
 
    newBreak () {
        this.breaks.push({
            start: getCurrentTime(),
            end: undefined,
            isOn: false,
        });
        this.onChange();
    } 
 
    removeBreak (breakToRemove: WorkBreak) {
        this.breaks = this.breaks.filter(breakEntry => breakEntry !== breakToRemove);
        Iif(breakToRemove.isOn && this.isOnBreak)
            this.isOnBreak = false;
        this.onChange();
    }
 
    breakInvalid () {
        const breaks = this.breaks;
        return breaks && breaks.length && !breaks[breaks.length - 1].end;
    }
 
    matchBreakTimes (workBreak: WorkBreak, index: number) {
        Iif(workBreak.start! < this._start)
            workBreak.start = this._start;
        Iif(workBreak.end! > this._end)
            workBreak.end = this._end;
 
        Iif(workBreak.end! < workBreak.start!)
            workBreak.end = workBreak.start;
 
        const beforeBreak = index <= 0 ? undefined : this.breaks[index - 1];
        const afterBreak = index >= this.breaks.length - 1 ? undefined : this.breaks[index + 1];
 
        Iif(beforeBreak && beforeBreak.end! > workBreak.start!) 
            workBreak.start = beforeBreak.end;
        Iif(afterBreak && afterBreak.start! < workBreak.end!)
            workBreak.end = afterBreak.start;
    }
}