Picsur/frontend/src/app/components/pref-option/pref-option.component.ts

105 lines
2.8 KiB
TypeScript
Raw Normal View History

2022-03-19 18:30:47 +00:00
import { Component, Input, OnInit } from '@angular/core';
import { AutoUnsubscribe } from 'ngx-auto-unsubscribe-decorator';
2022-04-13 19:14:24 +00:00
import {
2022-04-14 18:42:09 +00:00
DecodedPref,
2022-09-06 14:32:16 +00:00
PrefValueType,
2022-04-13 19:14:24 +00:00
} from 'picsur-shared/dist/dto/preferences.dto';
import { AsyncFailable, HasFailed } from 'picsur-shared/dist/types';
import { Subject } from 'rxjs';
import { Required } from 'src/app/models/decorators/required.decorator';
import { Logger } from 'src/app/services/logger/logger.service';
import { ErrorService } from 'src/app/util/error-manager/error.service';
2022-04-13 19:14:24 +00:00
import { Throttle } from 'src/app/util/throttle';
2022-03-19 18:30:47 +00:00
@Component({
2022-04-13 19:14:24 +00:00
selector: 'pref-option',
templateUrl: './pref-option.component.html',
styleUrls: ['./pref-option.component.scss'],
2022-03-19 18:30:47 +00:00
})
2022-04-13 19:14:24 +00:00
export class PrefOptionComponent implements OnInit {
private readonly logger = new Logger(PrefOptionComponent.name);
2022-04-14 18:42:09 +00:00
@Input() @Required pref: DecodedPref;
2022-04-13 19:14:24 +00:00
@Input('update') @Required updateFunction: (
key: string,
2022-06-05 10:20:16 +00:00
pref: PrefValueType,
2022-04-13 19:14:24 +00:00
) => AsyncFailable<any>;
2022-04-14 18:42:09 +00:00
@Input() @Required translator: {
[key in string]: string;
};
2022-03-19 18:30:47 +00:00
private updateSubject = new Subject<PrefValueType>();
2022-03-19 18:30:47 +00:00
constructor(private readonly errorService: ErrorService) {}
2022-03-19 18:30:47 +00:00
ngOnInit(): void {
this.subscribeUpdate();
}
get name(): string {
2022-04-14 18:42:09 +00:00
return this.translator[this.pref.key] ?? this.pref.key;
2022-03-19 18:30:47 +00:00
}
get valString(): string {
if (this.pref.type !== 'string') {
throw new Error('Not a string preference');
}
return this.pref.value as string;
}
get valNumber(): number {
if (this.pref.type !== 'number') {
throw new Error('Not an int preference');
}
return this.pref.value as number;
}
get valBool(): boolean {
if (this.pref.type !== 'boolean') {
throw new Error('Not a boolean preference');
}
return this.pref.value as boolean;
}
update(value: any) {
this.updateSubject.next(value);
}
stringUpdateWrapper(e: Event) {
this.update((e.target as HTMLInputElement).value);
}
numberUpdateWrapper(e: Event) {
2022-04-01 11:30:15 +00:00
const value = (e.target as HTMLInputElement).valueAsNumber;
if (isNaN(value)) return;
this.update(value);
2022-03-19 18:30:47 +00:00
}
private async updatePreference(value: PrefValueType) {
2022-04-13 19:14:24 +00:00
const result = await this.updateFunction(this.pref.key, value);
2022-03-28 21:03:26 +00:00
if (!HasFailed(result)) {
2022-04-14 18:42:09 +00:00
const message =
this.pref.type === 'string'
? `Updated ${this.name}`
: this.pref.type === 'number'
? `Updated ${this.name}`
: this.pref.type === 'boolean'
? value
? `Enabled ${this.name}`
: `Disabled ${this.name}`
: '';
this.errorService.success(message);
2022-03-28 21:03:26 +00:00
} else {
this.errorService.showFailure(result, this.logger);
2022-03-28 21:03:26 +00:00
}
}
2022-03-19 18:30:47 +00:00
@AutoUnsubscribe()
subscribeUpdate() {
return this.updateSubject
2022-04-13 19:14:24 +00:00
.pipe(Throttle(300))
2022-03-28 21:03:26 +00:00
.subscribe(this.updatePreference.bind(this));
2022-03-19 18:30:47 +00:00
}
}