|
|
@ -1,7 +1,10 @@ |
|
|
|
import { AdminService } from '@ghostfolio/client/services/admin.service'; |
|
|
|
import { DataService } from '@ghostfolio/client/services/data.service'; |
|
|
|
import { PROPERTY_CURRENCIES } from '@ghostfolio/common/config'; |
|
|
|
|
|
|
|
import { |
|
|
|
ChangeDetectionStrategy, |
|
|
|
ChangeDetectorRef, |
|
|
|
Component, |
|
|
|
OnDestroy, |
|
|
|
OnInit |
|
|
@ -15,6 +18,10 @@ import { |
|
|
|
Validators |
|
|
|
} from '@angular/forms'; |
|
|
|
import { MatDialogRef } from '@angular/material/dialog'; |
|
|
|
import { uniq } from 'lodash'; |
|
|
|
import { Subject, takeUntil } from 'rxjs'; |
|
|
|
|
|
|
|
import { CreateAssetProfileDialogMode } from './interfaces/interfaces'; |
|
|
|
|
|
|
|
@Component({ |
|
|
|
changeDetection: ChangeDetectionStrategy.OnPush, |
|
|
@ -25,17 +32,29 @@ import { MatDialogRef } from '@angular/material/dialog'; |
|
|
|
}) |
|
|
|
export class CreateAssetProfileDialog implements OnInit, OnDestroy { |
|
|
|
public createAssetProfileForm: FormGroup; |
|
|
|
public mode: 'auto' | 'manual'; |
|
|
|
public mode: CreateAssetProfileDialogMode; |
|
|
|
|
|
|
|
private customCurrencies: string[]; |
|
|
|
private unsubscribeSubject = new Subject<void>(); |
|
|
|
|
|
|
|
public constructor( |
|
|
|
public readonly adminService: AdminService, |
|
|
|
private readonly changeDetectorRef: ChangeDetectorRef, |
|
|
|
private readonly dataService: DataService, |
|
|
|
public readonly dialogRef: MatDialogRef<CreateAssetProfileDialog>, |
|
|
|
public readonly formBuilder: FormBuilder |
|
|
|
) {} |
|
|
|
|
|
|
|
public ngOnInit() { |
|
|
|
this.initializeCustomCurrencies(); |
|
|
|
|
|
|
|
this.createAssetProfileForm = this.formBuilder.group( |
|
|
|
{ |
|
|
|
addCurrency: new FormControl(null, [ |
|
|
|
Validators.maxLength(3), |
|
|
|
Validators.minLength(3), |
|
|
|
Validators.required |
|
|
|
]), |
|
|
|
addSymbol: new FormControl(null, [Validators.required]), |
|
|
|
searchSymbol: new FormControl(null, [Validators.required]) |
|
|
|
}, |
|
|
@ -51,34 +70,75 @@ export class CreateAssetProfileDialog implements OnInit, OnDestroy { |
|
|
|
this.dialogRef.close(); |
|
|
|
} |
|
|
|
|
|
|
|
public onRadioChange(mode: 'auto' | 'manual') { |
|
|
|
public onRadioChange(mode: CreateAssetProfileDialogMode) { |
|
|
|
this.mode = mode; |
|
|
|
} |
|
|
|
|
|
|
|
public onSubmit() { |
|
|
|
this.mode === 'auto' |
|
|
|
? this.dialogRef.close({ |
|
|
|
if (this.mode === 'auto') { |
|
|
|
this.dialogRef.close({ |
|
|
|
dataSource: |
|
|
|
this.createAssetProfileForm.get('searchSymbol').value.dataSource, |
|
|
|
symbol: this.createAssetProfileForm.get('searchSymbol').value.symbol |
|
|
|
}); |
|
|
|
} else if (this.mode === 'currency') { |
|
|
|
const currency = this.createAssetProfileForm |
|
|
|
.get('addCurrency') |
|
|
|
.value.toUpperCase(); |
|
|
|
|
|
|
|
const currencies = uniq([...this.customCurrencies, currency]); |
|
|
|
|
|
|
|
this.dataService |
|
|
|
.putAdminSetting(PROPERTY_CURRENCIES, { |
|
|
|
value: JSON.stringify(currencies) |
|
|
|
}) |
|
|
|
: this.dialogRef.close({ |
|
|
|
.pipe(takeUntil(this.unsubscribeSubject)) |
|
|
|
.subscribe(() => { |
|
|
|
this.dialogRef.close(); |
|
|
|
}); |
|
|
|
} else if (this.mode === 'manual') { |
|
|
|
this.dialogRef.close({ |
|
|
|
dataSource: 'MANUAL', |
|
|
|
symbol: this.createAssetProfileForm.get('addSymbol').value |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
public ngOnDestroy() {} |
|
|
|
public get showCurrencyErrorMessage() { |
|
|
|
const addCurrencyFormControl = |
|
|
|
this.createAssetProfileForm.get('addCurrency'); |
|
|
|
|
|
|
|
if ( |
|
|
|
addCurrencyFormControl.hasError('maxlength') || |
|
|
|
addCurrencyFormControl.hasError('minlength') |
|
|
|
) { |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
public ngOnDestroy() { |
|
|
|
this.unsubscribeSubject.next(); |
|
|
|
this.unsubscribeSubject.complete(); |
|
|
|
} |
|
|
|
|
|
|
|
private atLeastOneValid(control: AbstractControl): ValidationErrors { |
|
|
|
const addCurrencyControl = control.get('addCurrency'); |
|
|
|
const addSymbolControl = control.get('addSymbol'); |
|
|
|
const searchSymbolControl = control.get('searchSymbol'); |
|
|
|
|
|
|
|
if (addSymbolControl.valid && searchSymbolControl.valid) { |
|
|
|
if ( |
|
|
|
addCurrencyControl.valid && |
|
|
|
addSymbolControl.valid && |
|
|
|
searchSymbolControl.valid |
|
|
|
) { |
|
|
|
return { atLeastOneValid: true }; |
|
|
|
} |
|
|
|
|
|
|
|
if ( |
|
|
|
addCurrencyControl.valid || |
|
|
|
!addCurrencyControl || |
|
|
|
addSymbolControl.valid || |
|
|
|
!addSymbolControl || |
|
|
|
searchSymbolControl.valid || |
|
|
@ -89,4 +149,15 @@ export class CreateAssetProfileDialog implements OnInit, OnDestroy { |
|
|
|
|
|
|
|
return { atLeastOneValid: true }; |
|
|
|
} |
|
|
|
|
|
|
|
private initializeCustomCurrencies() { |
|
|
|
this.adminService |
|
|
|
.fetchAdminData() |
|
|
|
.pipe(takeUntil(this.unsubscribeSubject)) |
|
|
|
.subscribe(({ settings }) => { |
|
|
|
this.customCurrencies = settings[PROPERTY_CURRENCIES] as string[]; |
|
|
|
|
|
|
|
this.changeDetectorRef.markForCheck(); |
|
|
|
}); |
|
|
|
} |
|
|
|
} |
|
|
|