mirror of
https://github.com/bitwarden/browser
synced 2026-01-06 10:33:57 +00:00
copy common ts code over from browser repo
This commit is contained in:
61
src/services/settings.service.ts
Normal file
61
src/services/settings.service.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import UserService from './user.service';
|
||||
import UtilsService from './utils.service';
|
||||
|
||||
const Keys = {
|
||||
settingsPrefix: 'settings_',
|
||||
equivalentDomains: 'equivalentDomains',
|
||||
};
|
||||
|
||||
export default class SettingsService {
|
||||
private settingsCache: any;
|
||||
|
||||
constructor(private userService: UserService) {
|
||||
}
|
||||
|
||||
clearCache(): void {
|
||||
this.settingsCache = null;
|
||||
}
|
||||
|
||||
getEquivalentDomains(): Promise<any> {
|
||||
return this.getSettingsKey(Keys.equivalentDomains);
|
||||
}
|
||||
|
||||
async setEquivalentDomains(equivalentDomains: string[][]) {
|
||||
await this.setSettingsKey(Keys.equivalentDomains, equivalentDomains);
|
||||
}
|
||||
|
||||
async clear(userId: string): Promise<void> {
|
||||
await UtilsService.removeFromStorage(Keys.settingsPrefix + userId);
|
||||
this.settingsCache = null;
|
||||
}
|
||||
|
||||
// Helpers
|
||||
|
||||
private async getSettings(): Promise<any> {
|
||||
if (this.settingsCache == null) {
|
||||
const userId = await this.userService.getUserId();
|
||||
this.settingsCache = UtilsService.getObjFromStorage(Keys.settingsPrefix + userId);
|
||||
}
|
||||
return this.settingsCache;
|
||||
}
|
||||
|
||||
private async getSettingsKey(key: string): Promise<any> {
|
||||
const settings = await this.getSettings();
|
||||
if (settings != null && settings[key]) {
|
||||
return settings[key];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private async setSettingsKey(key: string, value: any): Promise<void> {
|
||||
const userId = await this.userService.getUserId();
|
||||
let settings = await this.getSettings();
|
||||
if (!settings) {
|
||||
settings = {};
|
||||
}
|
||||
|
||||
settings[key] = value;
|
||||
await UtilsService.saveObjToStorage(Keys.settingsPrefix + userId, settings);
|
||||
this.settingsCache = settings;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user