-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprice.component.ts
More file actions
304 lines (271 loc) · 9.95 KB
/
price.component.ts
File metadata and controls
304 lines (271 loc) · 9.95 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
import {ChangeDetectorRef, Component, Injector, TemplateRef, ViewChild} from '@angular/core';
import {
AbstractDynamicLoaderComponent,
ComponentLoaderService,
DynamicComponent,
DynamicEventType,
PossibleTemplateList,
TemplateList
} from '@dontcode/plugin-common';
import {FormControl} from "@angular/forms";
import {PriceFinderService} from "../../shared/services/price-finder.service";
import {AbstractOnlineShopScrapper, ScrappedProduct} from "../../shared/online-shop-scrapper";
import {PriceModel} from "../../shared/price-model";
import {Action, ActionHandler, ActionType, DontCodeModelManager} from "@dontcode/core";
/**
* Displays and refresh a price of a product in a shop.
* When the price is new, it tries to fill the name with the shop and the product name
*/
@Component({
selector: 'dontcode-commerce-price',
templateUrl: './price.component.html',
styleUrls: ['./price.component.scss']
})
export class PriceComponent extends AbstractDynamicLoaderComponent implements ActionHandler {
static readonly baseUpdatePriceIcons='pi pi-refresh';
updatePriceIcon = PriceComponent.baseUpdatePriceIcons;
@ViewChild('inlineView', {static: true})
private inlineView!: TemplateRef<any>;
@ViewChild('fullEditView', {static: true})
private fullEditView!: TemplateRef<any>;
override value: PriceModel;
parsingError: { message?: string, url?: string, status?: number, content?: string } | null = null;
productSelectionMode = false;
productNameLinked = false;
listOfSelectableProducts = new Array<ScrappedProduct>();
constructor(loader: ComponentLoaderService, protected priceFinder: PriceFinderService,
injector: Injector, ref: ChangeDetectorRef) {
super(loader, injector, ref);
this.defineSubField('cost', 'Other currency');
this.defineSubField('priceDate', 'Date & Time');
this.defineSubField('shop', 'Shop');
this.defineSubField('urlInShop', 'Website (url)');
this.value = {};
}
providesTemplates(key?: string): TemplateList {
return new TemplateList(this.inlineView, null, this.fullEditView);
}
canProvide(key?: string): PossibleTemplateList {
return new PossibleTemplateList(true, false, true);
}
override setValue(val: any) {
super.setValue(val);
/*if( val!=null) {
this.priceFinder.updatePriceIfPossible(val, this.parentPosition??'').then(() => {
this.parsingError=null;
}).catch(() => {
val.inError=true;
});
}*/
// Eventually set default product name
this.enableProductNameLookup();
}
override createAndRegisterFormControls (): void {
let control = new FormControl (null, {updateOn:'blur'});
this.form.registerControl('nameInShop', control);
control = new FormControl ({value:null, disabled:true}, {updateOn:'blur'});
this.form.registerControl('idInShop', control);
}
cannotUpdatePrice():boolean {
if ((this.value!=null) && (this.value.shop!=null) && (this.value.nameInShop!=null)) {
return false;
}
return true;
}
async updatePrice(): Promise<void> {
this.parsingError=null;
if( this.value==null) return;
try {
if( this.value.idInShop==null) {
this.updatePriceIcon = PriceComponent.baseUpdatePriceIcons+ ' pi-spin'
/* const testProduct = new ScrappedProduct();
testProduct.productName="Test Product";
testProduct.productId="TEST-PRODUCT";
testProduct.currencyCode="EUR";
testProduct.productPrice=12;
this.selectedProduct(testProduct);*/
if ((this.value.nameInShop!=null) && (this.value.shop!=null)) {
// The user defined the product name, let's find the matching ones and let the user select only one of them
await this.priceFinder.searchProducts(this.value.nameInShop, this.value.shop).then(value => {
if( value!=null) {
this.listOfSelectableProducts = value;
this.productSelectionMode=true;
this.ref.markForCheck();
this.ref.detectChanges();
}
}).catch(reason => {
this.parsingError=this.translateToError(reason);
this.ref.markForCheck();
this.ref.detectChanges();
});
}
} else if (this.value.shop!=null) {
// We know the product id and the shop, let's update the price directly
await this.priceFinder.findPrice(this.value, this.value.shop, this.parentPosition??"").then (newPrice => {
if (newPrice!=null) {
this.value.inError=false;
this.setSubFieldValue('cost', newPrice.cost);
this.setSubFieldValue('priceDate', new Date());
}
}).catch ((reason) => {
this.value.inError=true;
this.parsingError=this.translateToError(reason);
this.ref.markForCheck();
this.ref.detectChanges();
});
}
} finally {
this.updatePriceIcon=PriceComponent.baseUpdatePriceIcons;
this.ref.markForCheck();
this.ref.detectChanges();
}
}
/**
* The user has selected one product amongst the list found with its keyword
* @param product
*/
selectedProduct(product: ScrappedProduct|null) {
this.productSelectionMode=false;
if( product!=null) {
this.value.idInShop=product.productId;
// this.value.nameInShop=product.productName??undefined;
this.hydrateValueToForm();
this.setSubFieldValue("cost", AbstractOnlineShopScrapper.toMoneyAmount(product));
this.setSubFieldValue('priceDate', new Date());
this.setSubFieldValue('urlInShop', product.productUrl);
this.value.cost=this.getSubFieldValue("cost");
this.value.priceDate = this.getSubFieldValue('priceDate');
this.value.urlInShop = this.getSubFieldValue('urlInShop');
} else {
this.clearProduct();
}
/* this.ref.markForCheck();
this.ref.detectChanges();*/
}
/**
* Override this method to listen to shop name change
* @param component
* @param type
* @param formName
*/
override applyComponentToSubField(component: DynamicComponent, type: string, formName: string): boolean {
const ret = super.applyComponentToSubField(component, type, formName);
if (formName=='shop') {
const valueChange = component.selectEventSourceFor(DynamicEventType.VALUE_CHANGE);
if (valueChange!=null) {
this.subscriptions.add(valueChange.eventSource.subscribe({
next: (event) => {
this.clearProduct ();
}
}));
}
}
return ret;
}
clearProduct ():void {
this.productSelectionMode=false;
this.parsingError=null;
if( this.value){
delete this.value.inError;
delete this.value.cost;
}
this.setSubFieldValue('cost', undefined);
if( this.value)
delete this.value.priceDate;
this.setSubFieldValue('priceDate', undefined);
if( this.value)
delete this.value.urlInShop;
this.setSubFieldValue('urlInShop', undefined);
if( this.value)
delete this.value.idInShop;
(this.form.get("idInShop") as FormControl)?.setValue(null, {emitEvent:false});
}
productNameChanged(event: Event) {
// const inputEvent = event as InputEvent;
this.clearProduct();
}
/**
* In case the product name is not defined, just try to pick-it up from parent form
* In this case listen to all changes of parent form
*/
enableProductNameLookup () {
// In case the name in shop is not defined, then try to pick-up a name from the parent form
if( (this.value?.nameInShop == null) && (this.parentForm!=null)) {
if (!this.productNameLinked) {
const parentNameControl = this.guessProductParentComponent ();
if( parentNameControl!=null) {
this.productNameLinked=true;
this.subscriptions.add(parentNameControl.valueChanges.subscribe(newValue => {
const nameInShopControl = this.form.get('nameInShop') as FormControl<string|null>;
if ((nameInShopControl.value==null) || (nameInShopControl.value=='') || (newValue.startsWith(nameInShopControl.value))) {
if( this.value == null) this.value={};
this.value.nameInShop=newValue;
nameInShopControl.setValue(newValue, {emitEvent:false, emitModelToViewChange:true});
}
}));
}
}
}
}
guessProductParentComponent (): FormControl|null {
if (this.parentForm!=null) {
const nameProp = DontCodeModelManager.guessNamePropertyOfObject(this.parentForm.controls);
if (nameProp!=null)
return this.parentForm.controls[nameProp] as FormControl;
}
return null;
}
protected translateToError(reason: any): any {
const ret:any={};
if( typeof reason === 'string') {
ret.message=reason;
return ret;
}
if (reason.status!=null) {
ret.status=reason.status;
}
if (reason.statusText!=null) {
ret.message=reason.statusText;
} else if (reason.message!=null) {
ret.message=reason.message;
}
if (reason.url!=null) {
ret.url=reason.url;
}
if (reason.error!=null) {
ret.content=reason.error;
}
if( ret.message==null) {
// Nothing found
ret.message=reason.toString();
}
return ret;
}
async performAction(action: Action): Promise<void> {
if (action.actionType==ActionType.EXTRACT) {
await this.priceFinder.updatePriceIfPossible(this.value, this.parentPosition??'').then (newPrice => {
if (newPrice!=null) {
this.value.inError=false;
this.setSubFieldValue('cost', newPrice.cost);
this.setSubFieldValue('priceDate', new Date());
}
}).catch ((reason) => {
this.value.inError=true;
this.parsingError=this.translateToError(reason);
this.ref.markForCheck();
this.ref.detectChanges();
});
}
}
isInError (): boolean {
return this.parsingError!=null;
}
errorMessage (): string {
if (this.parsingError==null) return '';
else {
let ret= (this.parsingError.status)?this.parsingError.status+':':'';
ret = ret+this.parsingError.message;
return ret;
}
}
}