Passed
Push — develop ( a03407...932b2d )
by Franck
02:04
created

ShowProductComponent.handleFinish   B

Complexity

Conditions 8

Size

Total Lines 39
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

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