Passed
Push — develop ( ea7818...4af8e5 )
by Franck
02:50
created

ShowProductComponent.handleFinish   B

Complexity

Conditions 8

Size

Total Lines 42
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 31
dl 0
loc 42
rs 7.2693
c 0
b 0
f 0
cc 8
1
import { HttpEvent, HttpEventType } from "@angular/common/http";
2
import { Component, Input, OnInit } from "@angular/core";
3
import { Product } from "src/app/models/product";
4
import { FileUploadService } from "src/app/services/file-upload.service";
5
import { ProductsService } from "src/app/services/products.service";
6
7
@Component({
8
    selector: "app-show-product",
9
    templateUrl: "./show-product.component.html",
10
    styleUrls: ["./show-product.component.scss"]
11
})
12
13
export class ShowProductComponent implements OnInit {
14
15
    @Input()
16
    products: Product[] = [];
17
    productModalOpen = false;
18
    selectedProduct!: Product;
19
    file!: File;
20
    progress = 0;
21
22
    constructor(private productsService: ProductsService,private fileService: FileUploadService) {}
23
24
    ngOnInit(): void {
25
    }
26
27
    onEdit(product: Product): void {
28
        this.productModalOpen = true;
29
        this.selectedProduct = product;
30
    }
31
32
    onDelete(product: Product): void {
33
34
    }
35
36
    addProduct(): void {
37
        this.productModalOpen = true;
38
    }
39
40
    handleFinish(event: any) {
41
        let product = event.product ? event.product : null;
42
        this.file = event.file ? event.file : null;
43
        if(product) {
44
            if(this.selectedProduct) {
45
                // Edit Product
46
            } else {
47
                // Add Product
48
                this.productsService.addProduct(product).subscribe(
49
                    (data) => {
50
                        if (data.status === 200) {
51
                            // Update Product
52
                            if (this.file) {
53
                                this.fileService.uploadImage(this.file).subscribe(
54
                                    (data) => {
55
                                        (event: HttpEvent<any>) => {
56
                                            switch (event.type) {
57
                                                case HttpEventType.Sent:
58
                                                        console.log("Success");
59
                                                    break;
60
                                                case HttpEventType.UploadProgress:
61
                                                        this.progress = Math.round(event.loaded / event.total! * 100);
62
                                                    break;
63
                                                case HttpEventType.Response:
64
                                                        console.log(event.body);
65
                                                setTimeout(() => {
66
                                                    this.progress = 0;
67
                                                }, 1500);
68
                                            };
69
                                        }
70
                                    }
71
                                )
72
                            }
73
                            product.idProduct = data.args.lastInsertId;
74
                            this.products.push(product);
75
                        }
76
                    }
77
                );
78
            }
79
        }
80
        this.productModalOpen = false;
81
    };
82
83
}
84