Newer
Older
smart-home-server / webclient-vue / src / stores / areas.js
@Eugene Sukhodolskiy Eugene Sukhodolskiy 7 hours ago 698 bytes Scaffold Vue web client
import { defineStore } from "pinia";
import { areasApi } from "../api/modules/areas";

export const useAreasStore = defineStore("areas", {
  state: () => ({
    areas: [],
    isLoading: false,
    error: null,
  }),

  getters: {
    areasById(state) {
      return Object.fromEntries(state.areas.map((area) => [String(area.id), area]));
    },
  },

  actions: {
    async loadAreas() {
      this.isLoading = true;
      this.error = null;

      const result = await areasApi.list();
      this.isLoading = false;

      if (!result.ok) {
        this.error = result.error;
        return result;
      }

      this.areas = result.data?.data?.areas || [];
      return result;
    },
  },
});