Passed
Push — develop ( 409ed7...be5e17 )
by Franck
02:07
created

ShowProductComponent.editProductToServer   A

Complexity

Conditions 3

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 18
dl 0
loc 24
rs 9.5
c 0
b 0
f 0
cc 3
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
import { environment } from "src/environments/environment";
7
import { Response } from "src/app/models/response";
8
9
@Component({
10
    selector: "app-show-product",
11
    templateUrl: "./show-product.component.html",
12
    styleUrls: ["./show-product.component.scss"]
13
})
14
15
export class ShowProductComponent implements OnInit {
16
17
    @Input()
18
    products: Product[] = [];
19
    productModalOpen = false;
20
    selectedProduct!: Product;
21
    file!: File;
22
    progress = 0;
23
    baseUrlImage = `${environment.api_image}`;
24
25
    constructor(private productsService: ProductsService,private fileService: FileUploadService) { }
26
27
    ngOnInit(): void { }
28
29
    onEdit(product: Product): void {
30
        this.productModalOpen = true;
31
        this.selectedProduct = product;
32
    }
33
34
    onDelete(product: Product): void { }
35
36
    addProduct(): void {
37
        this.selectedProduct = undefined;
38
        this.productModalOpen = true;
39
    }
40
41
    handleFinish(event: any) {
42
        let product = event.product ? event.product : null;
43
        this.file = event.file ? event.file : null;
44
        if(product) {
45
            if(this.selectedProduct) {
46
                product.idProduct = this.selectedProduct.idProduct;
47
                this.editProductToServer(product);
48
            } else {
49
                this.addProductToServer(product);
50
            }
51
        }
52
        this.productModalOpen = false;
53
    }
54
    uploadImage(event: HttpEvent<any>) {
55
        switch (event.type) {
56
            case HttpEventType.Sent:
57
                break;
58
            case HttpEventType.UploadProgress:
59
                    this.progress = Math.round(event.loaded / event.total! * 100);
60
                break;
61
            case HttpEventType.Response:
62
            setTimeout(() => {
63
                this.progress = 0;
64
            }, 1500);
65
        }
66
    }
67
    addProductToServer(product: Product) {
68
        this.productsService.addProduct(product).subscribe(
69
            (data) => {
70
                if (data.status === 200) {
71
                    if (this.file) {
72
                        this.fileService.uploadImage(this.file).subscribe(
73
                            (event: HttpEvent<any>) => {
74
                                this.uploadImage(event);
75
                            }
76
                        );
77
                    }
78
                    product.idProduct = data.args.lastInsertId;
79
                    this.products.push(product);
80
                }
81
            }
82
        );
83
    }
84
    editProductToServer(product: Product) {
85
        this.productsService.editProduct(product).subscribe (
86
            (data) => {
87
                if (data.status === 200) {
88
                    if (this.file) {
89
                        this.fileService.uploadImage(this.file).subscribe(
90
                            (event: HttpEvent<any>) => {
91
                                this.uploadImage(event);
92
                            }
93
                        );
94
                        this.fileService.deleteImage(product.oldImage).subscribe(
95
                            (data: Response) => {
96
                                console.log(data);
97
                            }
98
                        );
99
                    }
100
                    const index = this.products.findIndex(p => p.idProduct == product.idProduct);
101
                    this.products = [
102
                        ...this.products.slice(0, index),
103
                        product,
104
                        ...this.products.slice(index + 1)
105
                    ];
106
                } else {
107
                    console.log(data.message);
108
                }
109
            }
110
        );
111
    };
112
}
113