Passed
Push — develop ( 932b2d...bd0b1d )
by Franck
02:06
created

AddOrEditProductModalComponent.updateForm   A

Complexity

Conditions 1

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 12
rs 9.9
c 0
b 0
f 0
cc 1
1
import { CategoriesService } from './../../services/categories.service';
2
import { Component, Input, OnDestroy, OnInit, Output, EventEmitter, OnChanges, SimpleChanges } from '@angular/core';
3
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
4
import { Product } from 'src/app/models/product';
5
import { Category } from 'src/app/models/category';
6
import { Subscription } from 'rxjs';
7
8
@Component({
9
    selector: 'app-add-or-edit-product-modal',
10
    templateUrl: './add-or-edit-product-modal.component.html',
11
    styleUrls: ['./add-or-edit-product-modal.component.scss']
12
})
13
export class AddOrEditProductModalComponent implements OnInit, OnDestroy, OnChanges {
14
15
    @Input() product: Product;
16
    @Output() finish = new EventEmitter();
17
18
    productForm: FormGroup;
19
    categories: Category[];
20
    categorySub: Subscription;
21
    idCategory: 1;
22
    file!: File;
23
24
    constructor(public fb: FormBuilder, private categoriesService:CategoriesService ) {
25
        this.productForm = fb.group({
26
                productInfos: fb.group({
27
                    name: ['',Validators.required],
28
                    description: ['',Validators.required],
29
                    price: ['',Validators.required],
30
                    stock: ['',Validators.required],
31
                }),
32
                illustration: fb.group({
33
                    image: ['',Validators.required],
34
                }),
35
        });
36
    }
37
38
    selectCategory(id: any) {
39
        this.idCategory = id;
40
    }
41
42
    get isProductInfosInvalid(): boolean {
43
        return this.productForm.get('productInfos').invalid;
44
    }
45
46
    get isIllustrationInvalid(): boolean {
47
        if (this.product) {
48
            return false;
49
        }
50
        return this.productForm.get('illustration').invalid;
51
    }
52
53
    handleCancel() {
54
        this.finish.emit();
55
        this.close();
56
    }
57
58
    handleFinish() {
59
        const product = {
60
            ...this.productForm.get('productInfos').value,
61
            ...this.productForm.get('illustration').value,
62
            Category: this.idCategory
63
        };
64
        if (this.file) {
65
            product.image = this.file.name;
66
        } else {
67
            product.image = this.product.oldImage;
68
        }
69
        this.finish.emit({product: product, file: this.file ? this.file : null});
70
        this.close();
71
    }
72
73
    close() {
74
        this.productForm.reset();
75
        this.idCategory = 1;
76
    }
77
78
    detecteFiles (event) {
79
        this.file = event.target.files[0];
80
    }
81
82
    updateForm (product: Product) {
83
        this.productForm.patchValue({
84
            productInfos:{
85
                name: product.name,
86
                description: product.description,
87
                price: product.price,
88
                stock: product.stock,
89
            }
90
        });
91
        product.oldImage = product.image;
92
        this.selectCategory(product.Category);
93
    }
94
95
    ngOnInit(): void {
96
        this.categorySub = this.categoriesService.getCategory().subscribe(
97
            (response) => {
98
                this.categories = response.result;
99
            }
100
        );
101
    }
102
103
    ngOnDestroy(): void {
104
        this.categorySub.unsubscribe();
105
    }
106
107
    ngOnChanges(changes: SimpleChanges): void {
108
        if (this.product) {
109
            this.updateForm(this.product);
110
        }
111
    }
112
113
}
114