All files / components/edit upp-edit.ts

94.53% Statements 121/128
78.52% Branches 117/149
100% Functions 20/20
94.35% Lines 117/124

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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 3056x 6x 6x 6x 6x       6x   6x 6x 6x                             18x       6x 18x 18x   18x 18x   18x 18x     18x     8x   8x       8x 8x 2x 2x 2x           18x               3x 1x     2x       3x 1x     2x 2x 2x 2x 2x   2x 2x 2x 2x 2x     2x 1x 1x           2x 1x 1x       2x   2x 2x       1x       1x   1x       2x   2x 1x     1x               33x 33x   33x       25x   25x 25x 12x     25x   25x       63x   63x 63x   63x       63x 63x 63x   63x 8x 8x   8x 8x     8x 8x         63x     18x   9x 9x 9x 9x 9x 9x   9x 9x 8x                                           1x                   9x   9x 9x 1x     9x       67x 7x   67x               3x       10x       10x       10x       10x 11x                 3x 1x     2x 2x 1x     1x     2x 2x       1x       1x 1x    
import { Component, OnChanges, OnDestroy } from '@angular/core';
import { Input, Output, EventEmitter } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { FormGroup } from '@angular/forms';
import { FormControl, Validators } from '@angular/forms';
import { AbstractControl, ValidationErrors } from '@angular/forms';
 
import { OnForm } from '@unpispas/upp-wdgt';
import { ViewRenderer } from '@unpispas/upp-wdgt';
 
import { languageService, languageSupport } from '@unpispas/upp-base';
import { viewService } from '@unpispas/upp-base';
import { dataService } from '@unpispas/upp-data';
 
import { ProductView } from '@unpispas/upp-data';
import { ProductOptView } from '@unpispas/upp-data';
import { CategoryView } from '@unpispas/upp-data';
 
@Component({
    selector: 'upp-category-edit',
    changeDetection: ChangeDetectionStrategy.OnPush,
    templateUrl: './upp-edit.html',
    styleUrls: [
      './upp-edit.scss'
    ],
    providers: [{
        provide: ViewRenderer,
        useFactory: (change: ChangeDetectorRef) => new ViewRenderer(change),
        deps: [ChangeDetectorRef]
    }]       
})
export class UppCategoryEditComponent extends languageSupport implements OnForm, OnChanges, OnDestroy {
    @Input() product: ProductView | null = null;
    @Input() category: CategoryView | null = null;
 
    @Output() Updated = new EventEmitter<void>();
    @Output() Closed = new EventEmitter<void>();
 
    constructor(private lang: languageService, private change: ViewRenderer, private data: dataService, public view: viewService){
        super(lang, change.cdref);
    }
 
    private _depends = new Set <ProductOptView> ();
 
    ngOnChanges(){
        this._categoryForm = null;
 
        Iif (!this.product || !this.category){
            return;
        }
 
        this._depends.clear();
        for (const depend of this.category.depends){
            const productoptview = this.data.alives.get(depend.option) as ProductOptView;
            if (productoptview){
                this._depends.add(productoptview);
            }
        }
    }
 
    ngOnDestroy(): void {
        super.OnDestroy();
    }
 
    /********************************/
    /* ONFORM IMPLEMENTATION        */
    /********************************/
    
    CanAccept(): boolean[] {
        if (!this.category){
            return [ true, true ];
        }
        
        return [ this.CategoryForm.dirty && this.CategoryForm.valid, false ];
    }
 
    async OnApply(): Promise <boolean> {
        if (!this.product || !this.category){
            return true;
        }
 
        const _name: string = this.CategoryForm.get('name')?.value || null;
        const _type: string = this.CategoryForm.get('type')?.value || null;
        const _sort: string = this.CategoryForm.get('sort')?.value || null;
        const _fxmin: string = this.CategoryForm.get('fxmin')?.value || null;
        const _fxmax: string = this.CategoryForm.get('fxmax')?.value || null;
 
        this.category.name = _name;
        this.category.type = ['SINGLE', 'MULTIPLE', 'FIXED'].includes(_type) ? _type : null;
        this.category.sort = _sort ? parseInt(_sort) : 0;
        this.category.fxmin = _fxmin ? parseInt(_fxmin) : null;
        this.category.fxmax = _fxmax ? parseInt(_fxmax) : null;
 
        // remove the existing dependencies
        for (const _depend of this.category.depends){
            const productoptview = this.data.alives.get(_depend.option) as ProductOptView;
            Iif (productoptview && !this._depends.has(productoptview)){
                _depend.status = 'DE';
            }
        }
 
        // include the created dependencies
        for (const _option of this._depends){
            if (_option.IsValid && _option.IsValid){
                this.category.AddDepend(_option.object);
            }
        }
 
        this.product.AddCategory(this.category.object);
        
        this.Updated.emit();     // activates main form acceptance
        return false;            // accepted form: do not continue
    }
 
    async OnAccept(): Promise <boolean> {
        Iif (!this.product || !this.category){
            return true;
        }
 
        await this.OnApply();
 
        return false;            // accepted form: do not continue
    }
 
    async OnCancel(): Promise <boolean> {
        this._categoryForm = null;
 
        if (!this.category){
            return true;    // nothing to be cancelled: continue
        }
 
        return false;   // cancelled form: do not continue
    }  
 
    /********************************/
    /* CATEGORY FORM                */
    /********************************/
 
    get fxmin(): number[] {
        const min = 0;
        const max = this.category?.options.length || 0;
 
        return [min, max];
    }
 
    get fxmax(): number[] {
        let min = 1;
 
        const val = this.CategoryForm.get('fxmin')?.value;
        if (val){
            min = Math.max(parseInt(val), 1);
        }
 
        const max = Math.max(min, this.category?.options.length || 1);
 
        return [min, max];
    }
 
    private checkCategoryForm(group: AbstractControl): ValidationErrors | null {
        const _type = group.get('type');        
 
        const _fxmin = group.get('fxmin');
        const _fxmax = group.get('fxmax');
 
        Iif (!this.category){
            return null;
        }
 
        if (_type && _fxmin && _fxmax){
            _fxmin.setErrors(null);
            _fxmax.setErrors(null);                
    
            if (_type.value == 'FIXED'){
                const _usedmin = _fxmin.value || 0;
                const _usedmax = _fxmax.value || 0;
 
                if (isNaN(parseInt(_usedmin)) || (Number(_usedmin) < this.fxmin[0]) || (Number(_usedmin) > this.fxmin[1])){
                    _fxmin.setErrors({ invalid: true });   
                }            
 
                if (isNaN(parseInt(_usedmax)) || (Number(_usedmax) < this.fxmax[0]) || (Number(_usedmax) < Number(_usedmin)) || (Number(_usedmax) > this.fxmax[1])){
                    _fxmax.setErrors({ invalid: true });   
                }            
            }
        }
 
        return null;  
    }
 
    private _categoryForm: FormGroup | null = null;   
    private LoadCategoryForm(): FormGroup {
        const _name: string | null = this.category?.name || null;
        const _type: string | null = this.category?.type || null;
        const _sort: number | null = this.category?.sort || null;
        const _fxmin: number | null = this.category?.fxmin || null;
        const _fxmax: number | null = this.category?.fxmax || null;
        const _cond: boolean = (((this.category?.depends) || []).length > 0);
 
        let _form: FormGroup | null = this._categoryForm;
        if (_form == null){
            _form = new FormGroup({
                name: new FormControl(_name, [
                    Validators.required,
                ]),
                type: new FormControl(_type, [
                    Validators.required,
                ]),
                sort: new FormControl(_sort, [
                    // no validators required
                ]),
                fxmin: new FormControl(_fxmin, [
                    // no validators required
                ]),
                fxmax: new FormControl(_fxmax, [
                    // no validators required
                ]),
                cond: new FormControl(_cond, [
                    // no validators required
                ]),
            }, { validators: this.checkCategoryForm.bind(this) });
        }
        else {
            _form.patchValue({
                name: _name,
                type: _type,
                sort: _sort,
                fxmin: _fxmin,
                fxmax: _fxmax,
                cond: _cond,
            });
        }
 
        this.change.markForCheck();
        
        _form.markAsPristine();
        setTimeout(() => {
            _form?.markAsPristine();
        }, 0)    
        
        return _form;
    }
 
    get CategoryForm(): FormGroup {
        if (this._categoryForm == null){
            this._categoryForm = this.LoadCategoryForm();
        }
        return this._categoryForm;        
    }   
 
    /********************************/
    /* DISPLAY OPTIONS              */
    /********************************/ 
 
    CheckDependency(option: ProductOptView): boolean {
        return this._depends.has(option);
    }
 
    get CanRemove(): boolean {
        Iif (!this.product || !this.category){
            return false;
        }
 
        return (this.product.categories.includes(this.category));
    }
 
    get CanDepend(): boolean {
        Iif (!this.product || !this.category){
            return false;
        }
 
        return this.product.categories.some(_category => {
            return (_category != this.category) && _category.Enabled;
        })
    }
 
    /********************************/
    /* EVENT HANDLERS               */
    /********************************/  
 
    OnDependChange(event: CustomEvent, option: ProductOptView): void{
        if (!option){
            return;
        }
 
        const _isChecked = event.detail.checked;
        if (_isChecked){
            this._depends.add(option);
        }
        else {
            this._depends.delete(option);
        }
 
        this.CategoryForm.markAsDirty();
        option.DoRefresh('DEPENDCHANGED');
    }
 
    OnDeleteCategory(){
        Iif (!this.category){
            return;
        }
 
        this.category.status = 'DE';
        this.Closed.emit();
    }
}