Passed
Push — develop ( 409ed7...be5e17 )
by Franck
02:07
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 } 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, OnChanges, OnDestroy {
14
15
    @Input() product!: Product;
16
    @Output() finish = new EventEmitter();
17
18
    productForm: FormGroup;
19
    categories!: Category[];
20
    categorySub: Subscription = new Subscription;
21
    idCategory: 1 | undefined;
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(): any {
43
        return this.productForm.get("productInfos")?.invalid;
44
    }
45
46
    get isIllustrationInvalid(): any {
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
    updateForm(product: Product) {
79
        this.productForm.patchValue({
80
            productInfos: {
81
                name: product.name,
82
                description: product.description,
83
                price: product.price,
84
                stock: product.stock,
85
            }
86
        });
87
        product.oldImage = product.image;
88
        this.selectCategory(product.Category);
89
    }
90
91
    detecteFiles (event:any) {
92
        this.file = event.target.files[0];
93
    }
94
95
    ngOnInit() {
96
        this.categorySub = this.categoriesService.getCategory().subscribe(
97
            (response) => {
98
                this.categories = response.result;
99
            }
100
        );
101
    }
102
103
    ngOnChanges(): void {
104
        if (this.product) {
105
            this.updateForm(this.product);
106
        }
107
    }
108
109
    ngOnDestroy() {
110
        this.categorySub.unsubscribe();
111
    }
112
113
}
114