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

105 lines
2.9 KiB
TypeScript
Raw Normal View History

2022-03-19 18:30:47 +00:00
import { Component, Input, OnInit } from '@angular/core';
import { AbstractControl, FormControl, ValidationErrors } from '@angular/forms';
2022-03-19 18:30:47 +00:00
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-12-25 22:36:59 +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/failable';
import { filter } from 'rxjs';
import { ZodTypeAny } from 'zod';
import { Required } from '../../models/decorators/required.decorator';
import { Logger } from '../../services/logger/logger.service';
import { ErrorService } from '../../util/error-manager/error.service';
import { Throttle } from '../../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);
public formControl = new FormControl<any>(undefined, {
updateOn: 'blur',
validators: this.syncValidator.bind(this),
});
private pref: DecodedPref;
@Input('pref') set prefSet(pref: DecodedPref) {
this.pref = pref;
this.formControl.setValue(pref.value);
}
get type() {
return this.pref.type;
}
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-12-25 22:19:49 +00:00
@Input() @Required name = '';
@Input() helpText = '';
@Input() validator?: ZodTypeAny = undefined;
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();
}
getErrorMessage() {
if (this.formControl.errors) {
const errors = this.formControl.errors;
if (errors['error']) {
return errors['error'];
}
return 'Invalid value';
2022-03-19 18:30:47 +00:00
}
return '';
2022-03-19 18:30:47 +00:00
}
private syncValidator(control: AbstractControl): ValidationErrors | null {
if (!this.validator) return null;
2022-03-19 18:30:47 +00:00
const result = this.validator.safeParse(control.value);
2022-03-19 18:30:47 +00:00
if (!result.success) {
return { error: result.error.issues[0]?.message ?? 'Invalid value' };
}
2022-03-19 18:30:47 +00:00
return null;
2022-12-25 22:19:49 +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.formControl.valueChanges
.pipe(
filter(() => this.formControl.errors === null),
Throttle(300),
)
2022-03-28 21:03:26 +00:00
.subscribe(this.updatePreference.bind(this));
2022-03-19 18:30:47 +00:00
}
}