Passed
Push — develop ( 098d47...e41a2d )
by Franck
01:00 queued 12s
created

ShowProductComponent.handleFinish   A

Complexity

Conditions 5

Size

Total Lines 17
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 15
dl 0
loc 17
rs 9.1832
c 0
b 0
f 0
cc 5
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 { ProductService } from 'src/app/services/products.service';
6
import { environment } from "src/environments/environment";
7
import { HttpEventType } from '@angular/common/http';
8
import { Response } from "src/app/models/response";
9
10
@Component({
11
    selector: 'app-show-product',
12
    templateUrl: './show-product.component.html',
13
    styleUrls: ['./show-product.component.scss']
14
})
15
export class ShowProductComponent implements OnInit {
16
17
    @Input()
18
    products: Product[];
19
    productModalOpen = false;
20
    selectedProduct: Product;
21
    delete = false;
22
    productToBeDelete: Product;
23
    file: File;
24
    progress = 0;
25
    baseUrlImage = `${environment.api_image}`;
26
27
    constructor(private productService: ProductService,private fileService: FileUploadService) { }
28
29
    ngOnInit(): void { }
30
31
    onEdit(product: Product): void {
32
        this.productModalOpen = true;
33
        this.selectedProduct = product;
34
    }
35
36
    onDelete(product: Product): void {
37
        this.delete = true;
38
        this.productToBeDelete = product;
39
    }
40
41
    addProduct(): void {
42
        this.selectedProduct = undefined;
43
        this.productModalOpen = true;
44
    }
45
46
    handleFinish(event) {
47
        if (event && event.product) {
48
            let product = event.product ? event.product : null;
49
            this.file = event.file ? event.file : null;
50
            // TODO delete console.log
51
            console.log(product);
52
            if(this.selectedProduct) {
53
                // Edit product
54
                product.idProduct = this.selectedProduct.idProduct;
55
                this.editProductToServer(product);
56
            } else {
57
                // Add product
58
                this.addProductToServer(product);
59
            }
60
        }
61
        this.productModalOpen = false;
62
    }
63
64
    uploadImage(event) {
65
        return new Promise(
66
            (resolve, reject) => {
67
                switch (event.type) {
68
                    case HttpEventType.Sent:
69
                        // TODO delete console.log
70
                        console.log("Requete Success");
71
                        break;
72
                    case HttpEventType.UploadProgress:
73
                        this.progress = Math.round(event.loaded / event.total * 100);
74
                        if (this.progress === 100) {
75
                            resolve(true);
76
                        }
77
                        break;
78
                    case HttpEventType.Response:
79
                        // TODO delete console.log
80
                        console.log(event.body);
81
                        setTimeout(() => {
82
                            this.progress = 0;
83
                        }, 1500);
84
                        break;
85
                }
86
            }
87
        );
88
    }
89
90
    addProductToServer(product) {
91
        this.productService.addProduct(product).subscribe(
92
            (data) => {
93
                if (data.status === 200) {
94
                    if (this.file) {
95
                        this.fileService.uploadImage(this.file).subscribe(
96
                            (event: HttpEvent<any>) => {
97
                                this.uploadImage(event).then(
98
                                    () => {
99
                                        product.idProduct = data.args.lastInsertId;
100
                                        product.Category = product.category;
101
                                        this.products.push(product);
102
                                    }
103
                                );
104
                            }
105
                        );
106
                    }
107
                }
108
            }
109
        );
110
    }
111
112
    editProductToServer(product) {
113
        this.productService.editProduct(product).subscribe(
114
            (data: Response) => {
115
                if (data.status === 200) {
116
                    if (this.file) {
117
                        this.fileService.uploadImage(this.file).subscribe(
118
                            (event: HttpEvent<any>) => {
119
                                this.uploadImage(event).then(
120
                                    () => {
121
                                        this.updateProducts(product);
122
                                    }
123
                                );
124
                            }
125
                        );
126
                        this.fileService.deleteImage(product.oldImage).subscribe(
127
                            (data: Response) => {
128
                                // TODO delete console.log
129
                                console.log(data);
130
                            }
131
                        );
132
                    } else {
133
                        this.updateProducts(product);
134
                    }
135
                } else {
136
                    // TODO delete console.log
137
                    console.log(data.message);
138
                }
139
            }
140
        );
141
    }
142
143
    updateProducts(product) {
144
        // update frontend
145
        const index = this.products.findIndex(p => p.idProduct === product.idProduct);
146
        product.Category = product.category;
147
        this.products = [
148
            ...this.products.slice(0,index),
149
            product,
150
            ...this.products.slice(index+1)
151
        ];
152
    }
153
154
    handleCancelDelete() {
155
        this.delete = false;
156
    }
157
158
    handleConfirmDelete(){
159
            this.productService.deleteProduct(this.productToBeDelete).subscribe(
160
            (data: Response) => {
161
                if(data.status == 200){
162
                    // Delete Product Image
163
                    this.fileService.deleteImage(this.productToBeDelete.image).subscribe(
164
                        (data: Response) => {
165
                            // TODO delete console.log
166
                            console.log(data);
167
                        }
168
                    )
169
                    // TODO delete console.log
170
                    console.log(data);
171
                    // Update Frontend
172
                    const index = this.products.findIndex(p => p.idProduct == this.productToBeDelete.idProduct);
173
                    this.products.splice(index,1);
174
                }else{
175
                    // TODO delete console.log
176
                    console.log(data.message);
177
                }
178
            }
179
        )
180
        this.handleCancelDelete();
181
    }
182
183
}
184