Browse Source

feat: improve logic in currency-selector

pull/2487/head
Dhoni77 2 years ago
parent
commit
0dce17795f
  1. 2
      libs/ui/src/lib/currency-selector/currency-selector.component.html
  2. 65
      libs/ui/src/lib/currency-selector/currency-selector.component.ts

2
libs/ui/src/lib/currency-selector/currency-selector.component.html

@ -15,7 +15,7 @@
<mat-option <mat-option
*ngFor="let currencyItem of filteredCurrencies" *ngFor="let currencyItem of filteredCurrencies"
class="line-height-1" class="line-height-1"
[value]="currencyItem.value" [value]="currencyItem"
> >
{{ currencyItem.label }} {{ currencyItem.label }}
</mat-option> </mat-option>

65
libs/ui/src/lib/currency-selector/currency-selector.component.ts

@ -16,13 +16,8 @@ import {
} from '@angular/material/autocomplete'; } from '@angular/material/autocomplete';
import { MatFormFieldControl } from '@angular/material/form-field'; import { MatFormFieldControl } from '@angular/material/form-field';
import { MatInput } from '@angular/material/input'; import { MatInput } from '@angular/material/input';
import { Subject, of, tap } from 'rxjs'; import { Subject } from 'rxjs';
import { import { map, startWith, takeUntil } from 'rxjs/operators';
debounceTime,
distinctUntilChanged,
switchMap,
takeUntil
} from 'rxjs/operators';
import { Currency } from '@ghostfolio/common/interfaces/currency.interface'; import { Currency } from '@ghostfolio/common/interfaces/currency.interface';
import { AbstractMatFormField } from '../symbol-autocomplete/abstract-mat-form-field'; import { AbstractMatFormField } from '../symbol-autocomplete/abstract-mat-form-field';
@ -75,41 +70,44 @@ export class CurrencySelectorComponent
this.control.disable(); this.control.disable();
} }
this.control.valueChanges
.pipe(takeUntil(this.unsubscribeSubject))
.subscribe(() => {
if (super.value?.value) {
super.value.value = null;
}
});
this.control.valueChanges this.control.valueChanges
.pipe( .pipe(
debounceTime(400),
distinctUntilChanged(),
takeUntil(this.unsubscribeSubject), takeUntil(this.unsubscribeSubject),
tap(() => { startWith(''),
this.isLoading = true; map((value) => (value ? this.filter(value) : this.currencies.slice()))
this.changeDetectorRef.markForCheck();
}),
switchMap((query) => {
return of(
this.currencies.filter((currency) =>
currency.label.toLowerCase().includes(query?.toLowerCase() || '')
) || []
);
})
) )
.subscribe((filteredCurrencies: Currency[]) => { .subscribe((val) => {
this.filteredCurrencies = filteredCurrencies; this.filteredCurrencies = val;
this.isLoading = false;
this.changeDetectorRef.markForCheck();
}); });
} }
public displayFn(currency: string) { public displayFn(currency: Currency) {
return currency; return currency?.label ?? '';
} }
public get empty() { public get empty() {
return this.input?.empty; return this.input?.empty;
} }
private filter(value: Currency | string) {
const filterValue =
typeof value === 'string'
? value?.toLowerCase()
: value?.value.toLowerCase();
return this.currencies.filter((currency) =>
currency.value.toLowerCase().startsWith(filterValue)
);
}
public focus() { public focus() {
this.input.focus(); this.input.focus();
} }
@ -124,7 +122,8 @@ export class CurrencySelectorComponent
public onUpdateCurrency(event: MatAutocompleteSelectedEvent) { public onUpdateCurrency(event: MatAutocompleteSelectedEvent) {
super.value = { super.value = {
value: event.option.value label: event.option.value.label,
value: event.option.value.value
} as Currency; } as Currency;
} }
@ -142,11 +141,9 @@ export class CurrencySelectorComponent
private validateRequired() { private validateRequired() {
const requiredCheck = super.required const requiredCheck = super.required
? !super.value?.value && ? !super.value.label || !super.value.value
!this.currencies
.map((currency) => currency.value)
.includes(super.value.value)
: false; : false;
if (requiredCheck) { if (requiredCheck) {
this.ngControl.control.setErrors({ invalidData: true }); this.ngControl.control.setErrors({ invalidData: true });
} }

Loading…
Cancel
Save