forked from jebaGem/AngularRedux
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple-asset.transformers.ts
87 lines (78 loc) · 3.82 KB
/
simple-asset.transformers.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import { Observable } from 'rxjs/Observable';
import { ISimpleAssetState, Asset, IBrandModel } from './simple-asset';
import { FormArray, FormControl, Validators, FormGroup } from '@angular/forms';
import { StringConstants } from '../shared/string-constants';
import { isNil, isEqual } from 'lodash';
import { ApiAccountData } from '../core/account/interfaces/api-account';
import { IRootState } from '../store/root.state';
export const getFormArrayFromObj =
(state$: Observable<IRootState>): Observable<any> =>
state$.map(state => {
const { fieldSetup, assets, assetTypes, brandModels } = state.simpleAssetsReducer;
const assetRows = [];
if (!assets || !fieldSetup) {
return new FormArray(assetRows);
}
// Return empty row if assetTypes are not loaded for nordicSetup
// else return empty row if brandModels are not loaded for nlSetup
if (fieldSetup === StringConstants.fieldSetup.nordicSetup &&
(!assetTypes || assetTypes.length < 1)) {
return new FormArray(assetRows);
} else if (fieldSetup === StringConstants.fieldSetup.nlSetup &&
(!brandModels || brandModels.length < 1)) {
return new FormArray(assetRows);
}
assets.forEach((asset: Asset, i: number) => {
const unitPrice: string | number = asset.unitPrice === 0 ? '' : asset.unitPrice;
let controls = {
ind: new FormControl(i),
unitPrice: new FormControl(unitPrice, [Validators.required]),
totalAmount: new FormControl(asset.totalAmount),
};
if (fieldSetup === StringConstants.fieldSetup.nlSetup) {
const defaultModel = brandModels[0];
const brandModel: any = (asset.brandModel !== '')
? asset.brandModel
: defaultModel.modelId;
const brandName: string = !isNil(asset.brandName) && asset.brandName !== ''
? asset.brandName
: defaultModel.brandName;
const brandId: string = !isNil(asset.id) && asset.id !== ''
? asset.id
: defaultModel.modelId;
const quantity: string | number = asset.quantity === 0 ? 1 : asset.quantity;
controls = Object.assign({}, controls, {
brandModel: new FormControl(brandModel, [Validators.required]),
year: new FormControl(asset.year),
brandName: new FormControl(brandName),
quantity: new FormControl(quantity, [Validators.min(1), Validators.required]),
});
} else if (fieldSetup === StringConstants.fieldSetup.nordicSetup) {
const assetType = (asset.assetType !== '') ? asset.assetType : assetTypes[0].name;
const quantity: string | number = asset.quantity === 0 ? '' : asset.quantity;
controls = Object.assign({}, controls, {
assetType: new FormControl(assetType, [Validators.required]),
description: new FormControl(asset.description, [Validators.required]),
quantity: new FormControl(quantity, [Validators.min(1), Validators.required]),
});
}
if (!isNil(state.appReducer.steps.active) && state.appReducer.steps.active.locked) {
Object.keys(controls).forEach(ctrlName => {
controls[ctrlName].disable();
});
}
assetRows.push(new FormGroup(controls));
});
return new FormArray(assetRows);
});
export const get100Items =
(brandModels$: Observable<IBrandModel[]>): Observable<IBrandModel[]> =>
brandModels$.map(items => items.slice(0, 100));
export const canLoadData = (account$: Observable<ApiAccountData.IApiAccountData>)
: Observable<boolean> =>
account$.map(account => {
return (!isNil(account) &&
!isNil(account.id) &&
!isNil(account.name) &&
!isNil(account.programId));
});