Improve timezone-select component

This commit is contained in:
Igor Kulikov 2021-01-05 16:20:30 +02:00
parent 8766902d45
commit 75d18d7303
2 changed files with 39 additions and 27 deletions

View File

@ -63,6 +63,15 @@ export class TimezoneSelectComponent implements ControlValueAccessor, OnInit, Af
this.requiredValue = coerceBooleanProperty(value); this.requiredValue = coerceBooleanProperty(value);
} }
private userTimezoneByDefaultValue: boolean;
get userTimezoneByDefault(): boolean {
return this.userTimezoneByDefaultValue;
}
@Input()
set userTimezoneByDefault(value: boolean) {
this.userTimezoneByDefaultValue = coerceBooleanProperty(value);
}
@Input() @Input()
disabled: boolean; disabled: boolean;
@ -129,8 +138,7 @@ export class TimezoneSelectComponent implements ControlValueAccessor, OnInit, Af
writeValue(value: string | null): void { writeValue(value: string | null): void {
this.searchText = ''; this.searchText = '';
if (value !== null) { getTimezoneInfo(value, this.defaultTimezoneId, this.userTimezoneByDefaultValue).subscribe(
getTimezoneInfo(value, this.defaultTimezoneId).subscribe(
(foundTimezone) => { (foundTimezone) => {
if (foundTimezone !== null) { if (foundTimezone !== null) {
this.selectTimezoneFormGroup.get('timezone').patchValue(foundTimezone, {emitEvent: false}); this.selectTimezoneFormGroup.get('timezone').patchValue(foundTimezone, {emitEvent: false});
@ -147,10 +155,6 @@ export class TimezoneSelectComponent implements ControlValueAccessor, OnInit, Af
} }
} }
); );
} else {
this.modelValue = null;
this.selectTimezoneFormGroup.get('timezone').patchValue('', {emitEvent: false});
}
this.dirty = true; this.dirty = true;
} }
@ -165,8 +169,8 @@ export class TimezoneSelectComponent implements ControlValueAccessor, OnInit, Af
if (this.ignoreClosePanel) { if (this.ignoreClosePanel) {
this.ignoreClosePanel = false; this.ignoreClosePanel = false;
} else { } else {
if (!this.modelValue && this.defaultTimezoneId) { if (!this.modelValue && (this.defaultTimezoneId || this.userTimezoneByDefaultValue)) {
getTimezoneInfo(this.defaultTimezoneId).subscribe( getTimezoneInfo(this.defaultTimezoneId, this.defaultTimezoneId, this.userTimezoneByDefaultValue).subscribe(
(defaultTimezoneInfo) => { (defaultTimezoneInfo) => {
if (defaultTimezoneInfo !== null) { if (defaultTimezoneInfo !== null) {
this.ngZone.run(() => { this.ngZone.run(() => {

View File

@ -19,7 +19,7 @@ import { deepClone, isDefined, isUndefined } from '@app/core/utils';
import * as moment_ from 'moment'; import * as moment_ from 'moment';
import { Observable } from 'rxjs/internal/Observable'; import { Observable } from 'rxjs/internal/Observable';
import { from, of } from 'rxjs'; import { from, of } from 'rxjs';
import { map, tap } from 'rxjs/operators'; import { map, mergeMap, tap } from 'rxjs/operators';
const moment = moment_; const moment = moment_;
@ -518,14 +518,22 @@ export function getTimezones(): Observable<TimezoneInfo[]> {
} }
} }
export function getTimezoneInfo(timezoneId: string, defaultTimezoneId?: string): Observable<TimezoneInfo> { export function getTimezoneInfo(timezoneId: string, defaultTimezoneId?: string, userTimezoneByDefault?: boolean): Observable<TimezoneInfo> {
return getTimezones().pipe( return getTimezones().pipe(
map((timezoneList) => { mergeMap((timezoneList) => {
let foundTimezone = timezoneList.find(timezoneInfo => timezoneInfo.id === timezoneId); let foundTimezone = timezoneList.find(timezoneInfo => timezoneInfo.id === timezoneId);
if (!foundTimezone && defaultTimezoneId) { if (!foundTimezone) {
if (userTimezoneByDefault) {
return getDefaultTimezone().pipe(
map((userTimezone) => {
return timezoneList.find(timezoneInfo => timezoneInfo.id === userTimezone);
})
);
} else if (defaultTimezoneId) {
foundTimezone = timezoneList.find(timezoneInfo => timezoneInfo.id === defaultTimezoneId); foundTimezone = timezoneList.find(timezoneInfo => timezoneInfo.id === defaultTimezoneId);
} }
return foundTimezone; }
return of(foundTimezone);
}) })
); );
} }