Files
Picsur/frontend/src/app/routes/settings/syspref/settings-syspref.component.ts

68 lines
1.9 KiB
TypeScript
Raw Normal View History

2022-03-20 22:11:17 +01:00
import { Component, OnInit } from '@angular/core';
2022-03-19 19:30:47 +01:00
import { AutoUnsubscribe } from 'ngx-auto-unsubscribe-decorator';
2022-03-24 16:14:14 +01:00
import { SysPreferenceBaseResponse } from 'picsur-shared/dist/dto/api/pref.dto';
2022-03-21 22:58:16 +01:00
import { HasFailed } from 'picsur-shared/dist/types';
2022-03-28 17:26:50 +02:00
import { SnackBarType } from "src/app/models/dto/snack-bar-type.dto";
2022-03-19 19:30:47 +01:00
import { SysprefService as SysPrefService } from 'src/app/services/api/syspref.service';
2022-03-21 22:58:16 +01:00
import { UtilService } from 'src/app/util/util.service';
2022-03-18 20:45:30 +01:00
@Component({
templateUrl: './settings-syspref.component.html',
})
2022-03-20 22:11:17 +01:00
export class SettingsSysprefComponent implements OnInit {
2022-03-19 19:30:47 +01:00
render = true;
2022-03-24 16:14:14 +01:00
preferences: SysPreferenceBaseResponse[] = [];
2022-03-18 20:45:30 +01:00
2022-03-21 22:58:16 +01:00
constructor(
private sysprefService: SysPrefService,
private utilService: UtilService
) {}
2022-03-19 19:30:47 +01:00
async ngOnInit() {
this.subscribePreferences();
2022-03-21 22:58:16 +01:00
const success = await this.sysprefService.getPreferences();
if (HasFailed(success)) {
this.utilService.showSnackBar(
'Failed to load preferences',
SnackBarType.Error
);
}
2022-03-19 19:30:47 +01:00
}
@AutoUnsubscribe()
private subscribePreferences() {
return this.sysprefService.live.subscribe((preferences) => {
// If the preferences are the same, something probably went wrong, so reset
if (this.compareFlatObjectArray(this.preferences, preferences)) {
this.render = false;
setTimeout(() => {
this.render = true;
});
}
this.preferences = preferences;
});
}
private compareFlatObjectArray(a: any[], b: any[]): boolean {
if (a.length !== b.length) {
return false;
}
for (let i = 0; i < a.length; i++) {
if (!this.compareFlatObject(a[i], b[i])) {
return false;
}
}
return true;
}
private compareFlatObject(a: any, b: any): boolean {
for (const key in a) {
if (a.hasOwnProperty(key) && a[key] !== b[key]) {
return false;
}
}
return true;
}
2022-03-18 20:45:30 +01:00
}