Newer
Older
smart-home-server / webclient / src / js / FavoritesStore.js
@Eugene Sukhodolskiy Eugene Sukhodolskiy 9 hours ago 679 bytes Webclient. Areas favorites feature
const STORAGE_KEY = "sh_fav_areas";

const FavoritesStore = {
	getAll() {
		try {
			return JSON.parse(localStorage.getItem(STORAGE_KEY) || "[]");
		} catch {
			return [];
		}
	},

	has(id) {
		return this.getAll().includes(String(id));
	},

	add(id) {
		const ids = this.getAll();
		const sid = String(id);
		if (!ids.includes(sid)) {
			ids.push(sid);
			localStorage.setItem(STORAGE_KEY, JSON.stringify(ids));
		}
	},

	remove(id) {
		const ids = this.getAll().filter(i => i !== String(id));
		localStorage.setItem(STORAGE_KEY, JSON.stringify(ids));
	},

	toggle(id) {
		this.has(id) ? this.remove(id) : this.add(id);
		return this.has(id);
	}
};

export { FavoritesStore };