diff --git a/angular.json b/angular.json index 10eacaa..91429ee 100644 --- a/angular.json +++ b/angular.json @@ -63,8 +63,8 @@ { "type": "all", "baseline": "750kb", - "maximumWarning": "200kb", - "maximumError": "400kb" + "maximumWarning": "4mb", + "maximumError": "6mb" } ] }, diff --git a/fake-rest-api/endpoints.json b/fake-rest-api/endpoints.json index 5f5a038..070ed70 100644 --- a/fake-rest-api/endpoints.json +++ b/fake-rest-api/endpoints.json @@ -23,296 +23,6 @@ "value": 12.12 } ] - }, - { - "packages": [ - { - "name": "adfadf", - "weight": 3.123, - "value": 10.64 - }, - { - "name": "fadfag", - "weight": 3.231, - "value": 47.91 - } - ], - "id": "UiYl4c4" - }, - { - "packages": [ - { - "name": "Some Piro", - "weight": 3.123, - "value": 5.9 - }, - { - "name": "fadf", - "weight": 3.123, - "value": 36.17 - } - ], - "id": "dv-A5NI" - }, - { - "packages": [ - { - "name": "adfadf", - "weight": 3.123, - "value": 4.49 - } - ], - "id": "06Le_ZV" - }, - { - "packages": [ - { - "name": "adfaf", - "weight": 2.123, - "value": 28.18 - } - ], - "id": "yDEyJZr" - }, - { - "packages": [ - { - "name": "adfaf", - "weight": 2.123, - "value": 28.18 - } - ], - "id": "sCW_SoJ" - }, - { - "packages": [ - { - "name": "adfaf", - "weight": 2.123, - "value": 28.18 - } - ], - "id": "NdX8N75" - }, - { - "packages": [ - { - "name": "adfadf", - "weight": 2.123, - "value": 3.61 - } - ], - "id": "tCKvdGt" - }, - { - "packages": [ - { - "name": "adfadf", - "weight": 2.123, - "value": 3.61 - } - ], - "id": "j5HF1R1" - }, - { - "packages": [ - { - "name": "adfa", - "weight": 3.123, - "value": 28.18 - } - ], - "id": "ipp6qtc" - }, - { - "packages": [ - { - "name": "adf", - "weight": 3.123, - "value": 20.28 - } - ], - "id": "GZmi38V" - }, - { - "packages": [ - { - "name": "adf", - "weight": 3.23, - "value": 1.86 - } - ], - "id": "cw1tEGO" - }, - { - "packages": [ - { - "name": "adf", - "weight": 1.23, - "value": 20.38 - } - ], - "id": "MAsKCx2" - }, - { - "packages": [ - { - "name": "adf", - "weight": 2.12, - "value": 2.74 - } - ], - "id": "uoWSFUK" - }, - { - "packages": [ - { - "name": "adfaf", - "weight": 2.12, - "value": 1.86 - } - ], - "id": "w4ES0ld" - }, - { - "packages": [ - { - "name": "adfaf", - "weight": 2.12, - "value": 2.36 - } - ], - "id": "cfzzK15" - }, - { - "packages": [ - { - "name": "adf", - "weight": 2.123, - "value": 32.12 - } - ], - "id": "U-0rFqc" - }, - { - "packages": [ - { - "name": "adf", - "weight": 2.123, - "value": 28.18 - } - ], - "id": "0XKdK-J" - }, - { - "packages": [ - { - "name": "adf", - "weight": 3.12, - "value": 3.12 - } - ], - "id": "1LbceN-" - }, - { - "packages": [ - { - "name": "adf", - "weight": 3.12, - "value": 2.74 - } - ], - "id": "ZOgGs1_" - }, - { - "packages": [ - { - "name": "adf", - "weight": 3.12, - "value": 3.47 - } - ], - "id": "LAfdN44" - }, - { - "packages": [ - { - "name": "adf", - "weight": 3.12, - "value": 23.12 - } - ], - "id": "nsDbkQI" - }, - { - "packages": [ - { - "name": "adf", - "weight": 1, - "value": 25.56 - } - ], - "id": "IPy0uew" - }, - { - "packages": [ - { - "name": "dadf", - "weight": 2.1, - "value": 0.88 - } - ], - "id": "S9sNOAP" - }, - { - "packages": [ - { - "name": "adfadf", - "weight": 1, - "value": 20.28 - } - ], - "id": "TIklosU" - }, - { - "packages": [ - { - "name": "adf", - "weight": 2.1, - "value": 136.8 - } - ], - "id": "k2wTxjs" - }, - { - "packages": [ - { - "name": "adf", - "weight": 2.12, - "value": 20.28 - } - ], - "id": "EqELFUC" - }, - { - "packages": [ - { - "name": "adf", - "weight": 1.23, - "value": 20.28 - } - ], - "id": "6Tdb86O" - }, - { - "packages": [ - { - "name": "adf", - "weight": 2.12, - "value": 3.61 - } - ], - "id": "UoXbpwz" } ] } diff --git a/libs/data/src/lib/data.ts b/libs/data/src/lib/data.ts index 1b1cc1e..d48a0c3 100644 --- a/libs/data/src/lib/data.ts +++ b/libs/data/src/lib/data.ts @@ -13,6 +13,7 @@ export interface Package { name: string; weight: number | string; value: number | Value; + currency?: Currency; } export interface CurrencyRates { diff --git a/libs/ui-components/src/lib/components/package-form/package-form.component.html b/libs/ui-components/src/lib/components/package-form/package-form.component.html index d9d4c3f..e55f3b6 100644 --- a/libs/ui-components/src/lib/components/package-form/package-form.component.html +++ b/libs/ui-components/src/lib/components/package-form/package-form.component.html @@ -1,7 +1,7 @@
{ this.packagesData.emit(val); this.isPackageFormValid.emit(this.shipment.valid); - console.log( - 'this.shipment.valueChanges, this.shipment.valid:', - this.shipment.valid - ); }); } @@ -57,7 +53,11 @@ export class PackageFormComponent implements OnInit, OnDestroy { disableRemovingPackage(): boolean { this.packages = this.shipment.get('packages') as FormArray; - return this.packages.length === 1; + return this.packages.length <= 1; + } + + get packageItemsForms() { + return this.shipment.get('packages'); } ngOnDestroy() { diff --git a/libs/ui-components/src/lib/components/shipment-form/shipment-form.component.ts b/libs/ui-components/src/lib/components/shipment-form/shipment-form.component.ts index 5df370a..74470e6 100644 --- a/libs/ui-components/src/lib/components/shipment-form/shipment-form.component.ts +++ b/libs/ui-components/src/lib/components/shipment-form/shipment-form.component.ts @@ -1,10 +1,11 @@ -import { Component, OnInit } from '@angular/core'; +import { Component, OnInit, ViewChild } from '@angular/core'; import { FormBuilder } from '@angular/forms'; import { CurrencyConversionRates, Package, Shipment, Value } from '@ss/data'; import { Big } from 'big.js'; import { CurrencyService } from '../../services/currency/currency.service'; import { ShipmentService } from '../../services/shipment/shipment.service'; import { SnackbarService } from '../../services/snackbar/snackbar.service'; +import { PackageFormComponent } from '../package-form/package-form.component'; @Component({ selector: 'ss-shipment-form', @@ -12,7 +13,7 @@ import { SnackbarService } from '../../services/snackbar/snackbar.service'; styleUrls: ['./shipment-form.component.scss'] }) export class ShipmentFormComponent implements OnInit { - packageData; + packageData = [{ weight: 0, value: 0 }] as Package[]; rates: CurrencyConversionRates; shipmentTableColumns: string[] = ['packageName', 'packageWeight', 'packageValue']; @@ -25,6 +26,7 @@ export class ShipmentFormComponent implements OnInit { value: 0 }; shipmentFormValid: boolean; + @ViewChild(PackageFormComponent, { static: true }) packageForm: PackageFormComponent; constructor( private fb: FormBuilder, @@ -46,7 +48,7 @@ export class ShipmentFormComponent implements OnInit { this.dataSource = $event.packages; this.totalPackages = $event.packages.length; - if (this.shipmentFormValid) { + if (this.shipmentFormValid && !this.packageForm.packages.untouched) { this.getTotalValue(); this.getTotalWeight(); } @@ -94,10 +96,12 @@ export class ShipmentFormComponent implements OnInit { } updateShipmentForm($event) { - this.shipmentFormValid = this.isShipmentFormValid($event); - if (this.shipmentFormValid) { - this.getTotalValue(); - this.getTotalWeight(); + if (this.packageForm.packages.length > 0) { + this.shipmentFormValid = this.isShipmentFormValid($event); + if (this.shipmentFormValid) { + this.getTotalValue(); + this.getTotalWeight(); + } } } @@ -109,24 +113,38 @@ export class ShipmentFormComponent implements OnInit { packageData.name = i.name; packageData.weight = Number(i.weight); if (i.currency === 'USD') { - packageData.value = Number(new Big(i.value).div(this.rates.USD).toFixed(2)); + packageData.value = Number( + new Big((i.value as unknown) as Big).div(this.rates.USD).toFixed(2) + ); } else if (i.currency === 'GBP') { - packageData.value = Number(new Big(i.value).div(this.rates.GBP).toFixed(2)); + packageData.value = Number( + new Big((i.value as unknown) as Big).div(this.rates.GBP).toFixed(2) + ); } else if (i.currency === 'EUR') { packageData.value = Number(Number(i.value).toFixed(2)); } shipment.packages.push(packageData); }); - this.shipmentService - .add(shipment) - .subscribe( - () => {}, - error => {}, - () => - this.snackbarService.openSnackBar( - `Shipment successfully saved! : ${JSON.stringify(shipment)}` - ) - ); + this.shipmentService.add(shipment).subscribe( + () => {}, + error => {}, + () => { + this.snackbarService.openSnackBar( + `Shipment successfully saved! : ${JSON.stringify(shipment)}` + ); + this.clearForm(); + } + ); + } + + clearForm() { + while (this.packageForm.packages.length !== 1) { + this.packageForm.packages.removeAt(0); + } + this.packageForm.packages.reset(); + this.totalPackages = 0; + this.totalWeight = 0; + this.totalValue.value = 0; } }