All files / stores areas.js

93.22% Statements 55/59
68.75% Branches 22/32
84.61% Functions 11/13
94.54% Lines 52/55

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124        15x 15x   15x 22x     15x 22x 22x 22x   22x 2x   20x       15x       15x     2x 14x                         15x           6x 6x 6x   6x 6x   6x 6x 6x   6x 1x     1x 1x     5x 6x       2x   2x 2x 2x 2x       2x       2x   2x 2x 2x 2x       2x       2x   2x 4x     2x       1x   1x 1x 1x 1x       1x        
import { defineStore } from "pinia";
import { areasApi } from "../api/modules/areas";
 
function buildAreaTree(areas) {
  const map = {};
  const roots = [];
 
  for (const area of areas) {
    map[area.id] = { ...area, children: [] };
  }
 
  for (const area of areas) {
    const node = map[area.id];
    const isSelfReference = area.parent_id && area.parent_id == area.id;
    const parentExists = area.parent_id && map[area.parent_id];
 
    if (!isSelfReference && parentExists) {
      map[area.parent_id].children.push(node);
    } else {
      roots.push(node);
    }
  }
 
  Iif (roots.length === 0 && areas.length > 0) {
    return Object.values(map);
  }
 
  return roots;
}
 
export const useAreasStore = defineStore("areas", {
  state: () => ({
    areas: [],
    isLoading: false,
    error: null,
    _listAbortController: null,
  }),
 
  getters: {
    areasById(state) {
      return Object.fromEntries(state.areas.map((area) => [String(area.id), area]));
    },
 
    areaTree(state) {
      return buildAreaTree(state.areas);
    },
  },
 
  actions: {
    async loadAreas() {
      this._listAbortController?.abort();
      const controller = new AbortController();
      this._listAbortController = controller;
 
      this.isLoading = true;
      this.error = null;
 
      const result = await areasApi.list({ signal: controller.signal });
      this._listAbortController = null;
      this.isLoading = false;
 
      if (!result.ok) {
        Iif (result.error?.type === "timeout") {
          return result;
        }
        this.error = result.error;
        return result;
      }
 
      this.areas = result.data?.data?.areas || [];
      return result;
    },
 
    async createArea(payload) {
      const result = await areasApi.newArea(payload);
 
      Eif (result.ok) {
        const newArea = result.data?.data?.area;
        Eif (newArea) {
          this.areas.push(newArea);
        }
      }
 
      return result;
    },
 
    async renameArea(areaId, displayName) {
      const result = await areasApi.updateDisplayName({ area_id: areaId, display_name: displayName });
 
      Eif (result.ok) {
        const idx = this.areas.findIndex((a) => a.id === areaId);
        Eif (idx !== -1) {
          this.areas[idx] = { ...this.areas[idx], display_name: displayName };
        }
      }
 
      return result;
    },
 
    async removeArea(areaId) {
      const result = await areasApi.remove(areaId);
 
      Eif (result.ok) {
        this.areas = this.areas.filter((a) => a.id !== areaId);
      }
 
      return result;
    },
 
    async unassignArea(areaId) {
      const result = await areasApi.unassign(areaId);
 
      Eif (result.ok) {
        const idx = this.areas.findIndex((a) => a.id === areaId);
        Eif (idx !== -1) {
          this.areas[idx] = { ...this.areas[idx], parent_id: 0 };
        }
      }
 
      return result;
    },
  },
});