All files / components/option/edit upp-edit.ts

90.09% Statements 100/111
75.25% Branches 146/194
94.73% Functions 18/19
89.62% Lines 95/106

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 2596x 6x 6x 6x 6x   6x     6x 6x 6x 6x                           14x       6x 14x 14x   14x   14x 14x       14x               1x       2x 1x     1x 1x 1x 1x 1x   1x                         1x       2x       2x 2x 2x 2x 2x   2x   2x 1x     1x 1x 1x 1x 1x 1x       2x       1x   1x       1x 1x     1x             14x   15x 15x 15x 15x 15x 15x   15x 15x 14x                                           1x                   15x   15x 15x 1x     15x       127x 13x   127x             14x 14x     30x       5x 5x 5x               1x 1x 1x                 1x 1x 1x   1x 1x 1x 1x       1x     1x       1x 1x 1x 1x     1x       1x       1x       1x 1x    
import { Component, 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 { ViewRenderer } from '@unpispas/upp-wdgt';
import { OnForm } from '@unpispas/upp-wdgt';
 
import { alertService } from '@unpispas/upp-base';
import { languageService, languageSupport } from '@unpispas/upp-base';
import { dataService } from '@unpispas/upp-data';
import { viewService } from '@unpispas/upp-base';
 
import { CategoryView } from '@unpispas/upp-data';
import { ProductOptView } from '@unpispas/upp-data';
 
@Component({
    selector: 'upp-option-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 UppOptionEditComponent extends languageSupport implements OnForm, OnDestroy {
    @Input() category: CategoryView | null = null;
    @Input() option: ProductOptView | null = null;
 
    @Output() Updated = new EventEmitter<CategoryView>();
 
    constructor(private lang: languageService, private change: ViewRenderer, private alertCtrl: alertService, private data: dataService, public view: viewService){
        super(lang, change.cdref);
    }
 
    ngOnDestroy(): void {
        super.OnDestroy();
    }
 
    /********************************/
    /* ONFORM IMPLEMENTATION        */
    /********************************/
    
    CanAccept(): boolean[] {
        return [ this.OptionForm.valid || ((this.OptionForm.get('check')?.value || false) == false), true ];
    }
 
    async OnApply(): Promise <boolean> {
        if (!this.category || !this.option){
            return false;
        }
 
        const _name: string | null = this.OptionForm.get('name')?.value || null;
        const _price: string | null = this.OptionForm.get('price')?.value || null;
        const _descr: string | null = this.OptionForm.get('description')?.value || null;
        const _sort: number | null = this.OptionForm.get('sort')?.value || null;
        const _negate: boolean = this.OptionForm.get('negate')?.value || false;
 
        if (!this.option.Enabled || !_name || (_name.length == 0)){
            // nothing to do (we are only applying)
        }
        else E{
            this.option.name = _name;
            this.option.description = _descr;
            this.option.sort = _sort;
            this.option.price = _price ? parseFloat(_price) : 0.00;
            Iif (_negate && (this.option.price > 0.00)){
                this.option.price =  (0 - this.option.price);
            }
        }
 
        return true;
    }
 
    async OnAccept(): Promise <boolean> {
        Iif (!this.category || !this.option){
            return false;
        }
 
        const _name: string | null = this.OptionForm.get('name')?.value || null;
        const _price: string | null = this.OptionForm.get('price')?.value || null;
        const _descr: string | null = this.OptionForm.get('description')?.value || null;
        const _sort: number | null = this.OptionForm.get('sort')?.value || null;
        const _negate: boolean = this.OptionForm.get('negate')?.value || false;
 
        this._optionForm = null;
 
        if (!this.option.Enabled || !_name || (_name.length == 0)){
            this.option.status = 'DE';
        }
        else {
            this.option.name = _name;
            this.option.description = _descr;
            this.option.sort = _sort;
            this.option.price = _price ? parseFloat(_price) : 0.00;
            if (_negate && (this.option.price > 0.00)){
                this.option.price =  (0 - this.option.price);
            }
        }
 
        return true;
    }
 
    async OnCancel(): Promise <boolean> {
        this._optionForm = null;
 
        Iif (!this.category || !this.option){
            return false;
        }
 
        if (this.option.ToInsert){
            this.category.DelOption(this.option.object);
        }
                
        return true;
    } 
 
    /********************************/
    /* OPTION FORM                  */
    /********************************/
 
    private _optionForm: FormGroup | null = null;   
    private LoadOptionForm(): FormGroup {
        const _name: string | null = this.option?.name || null;
        const _price: number | null = this.option?.price || 0.00;
        const _descr: string | null = this.option?.description || null;
        const _sort: number | null = this.category?.sort || null;
        const _negate: boolean = ((this.option?.price || 1) < 0);
        const _check: boolean = (this.option?.Enabled) || false;
 
        let _form: FormGroup | null = this._optionForm;
        if (_form == null){
            _form = new FormGroup({
                name: new FormControl(_name, [
                    Validators.required,
                ]),
                price: new FormControl(_price, [
                    Validators.required,
                ]),
                description: new FormControl(_descr, [
                    // no validators required
                ]),
                sort: new FormControl(_sort, [
                    // no validators required
                ]),
                negate: new FormControl(_negate, [
                    // no validators required
                ]),
                check: new FormControl(_check, [
                    // no validators required
                ])
            });
        }
        else {
            _form.patchValue({
                name: _name,
                price: _price,
                description: _descr,
                sort: _sort,
                negate: _negate,
                check: _check
            });
        }
 
        this.change.markForCheck();
        
        _form.markAsPristine();
        setTimeout(() => {
            _form?.markAsPristine();
        }, 0)    
        
        return _form;
    }
 
    get OptionForm(): FormGroup {
        if (this._optionForm == null){
            this._optionForm = this.LoadOptionForm();
        }
        return this._optionForm;        
    }   
 
    /********************************/
    /* DISPLAY OPTIONS              */
    /********************************/ 
 
    private _editdescr = false;
    private _prevdecr: string | null = null;
 
    get EditDescription(): boolean {
        return this._editdescr;
    }
 
    set EditDescription(value: boolean){
        this._editdescr = value;
        this._prevdecr = (value)? this.OptionForm.get('description')?.value || null : null;
        this.change.markForCheck();
    }
 
    /********************************/
    /* EVENT HANDLERS               */
    /********************************/  
    
    OnNameChanged(){
        const _name: string | null = this.OptionForm.get('name')?.value || null;
        this.OptionForm.get('check')?.setValue(_name && (_name.length > 0));
        this.OnOptionChanged();
    }
 
    OnPriceChanged(){
        this.OptionForm.get('check')?.setValue(true);
        this.OnOptionChanged();
    }
 
    OnOptionChanged(){
        const _check: boolean = this.OptionForm.get('check')?.value || false;
        const _price: string | null = this.OptionForm.get('price')?.value || null;
        const _negate: boolean = this.OptionForm.get('negate')?.value || false;
 
        if (this.option){
            this.option.Enabled = _check;
            this.option.price = _price ? parseFloat(_price) : 0.00;
            Iif (_negate && (this.option.price > 0.00)){
                this.option.price =  (0 - this.option.price);
            }
 
            this.option.DoRefresh('OPTIONCHANGED');
        }
 
        this.Updated.emit();
    }
 
    OnNegate(){
        const negate = this.OptionForm.get('negate');
        if (negate){
            const _negate: boolean = negate.value;
            negate.setValue(!_negate);
        }
 
        this.change.markForCheck();
    }
    
    OnEditDescription(){
        this.EditDescription = !this.EditDescription;
    }    
 
    OnAcceptDescription(){
        this.EditDescription = false;
    }  
    
    OnCancelDescription(){
        this.OptionForm.get('description')?.setValue(this._prevdecr);
        this.EditDescription = false;
    }
}