# All files / features/areas/pages AreaTreePage.vue

29.85% Statements 20/67
25% Branches 11/44
14.28% Functions 4/28
29.5% Lines 18/61

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
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    15x

    4x

    14x

    1x

    1x

    1x

    1x

    4x

    4x
    4x
    4x
    4x

    4x
    4x
    4x
    4x

    4x
    4x

     <template>
      <section class="page">
        <GnPageHeader title="Tree" kicker="Areas">
          <template #actions>
            <GnButton variant="primary" icon="ph-plus" @click="openCreate">Create area</GnButton>
          </template>
        </GnPageHeader>

        <AppLoadingState v-if="areasStore.isLoading" text="Loading areas tree" />

        <AppErrorState
          v-else-if="areasStore.error"
          title="Areas loading failed"
          :message="areasStore.error.message"
          :retry="areasStore.loadAreas"
        />

        <AppEmptyState
          v-else-if="areasStore.areaTree.length === 0"
          title="No areas"
          message="No areas found. Create one to get started."
        />

        <ul v-else class="area-tree">
          <AreaTreeNode
            v-for="area in areasStore.areaTree"
            :key="area.id"
            :area="area"
            @rename="openRename"
            @remove="remove"
            @unassign="unassign"
          />
        </ul>

        <GnModal :open="showCreateModal" title="Create area" @update:open="showCreateModal = $event">
          <div class="form-group">
            <GnInput v-model="createForm.type" label="Type" placeholder="room" />
          </div>
          <div class="form-group">
            <GnInput v-model="createForm.alias" label="Alias" placeholder="kitchen" />
          </div>
          <div class="form-group">
            <GnInput v-model="createForm.display_name" label="Display name" placeholder="Kitchen" />
          </div>
          <div v-if="createError" class="form-group">
            <GnAlert variant="danger">{{ createError }}</GnAlert>
          </div>
          <template #footer>
            <GnButton variant="secondary" @click="showCreateModal = false">Cancel</GnButton>
            <GnButton variant="primary" icon="ph-plus" :loading="createLoading" @click="submitCreate">Create</GnButton>
          </template>
        </GnModal>

        <GnModal :open="showRenameModal" title="Rename area" @update:open="showRenameModal = $event">
          <div class="form-group">
            <GnInput v-model="renameForm.display_name" label="Display name" />
          </div>
          <div v-if="renameError" class="form-group">
            <GnAlert variant="danger">{{ renameError }}</GnAlert>
          </div>
          <template #footer>
            <GnButton variant="secondary" @click="showRenameModal = false">Cancel</GnButton>
            <GnButton variant="primary" icon="ph-check" :loading="renameLoading" @click="submitRename">Rename</GnButton>
          </template>
        </GnModal>
      </section>
    </template>

    <script setup>
    import { ref, reactive, onMounted } from "vue";
    import { useAreasStore } from "../../../stores/areas";
    import {
      GnPageHeader,
      GnButton,
      GnModal,
      GnInput,
      GnAlert,
    } from "gnexus-ui-kit/vue";
    import AreaTreeNode from "../components/AreaTreeNode.vue";
    import AppLoadingState from "../../../components/feedback/AppLoadingState.vue";
    import AppErrorState from "../../../components/feedback/AppErrorState.vue";
    import AppEmptyState from "../../../components/feedback/AppEmptyState.vue";

    const areasStore = useAreasStore();

    const showCreateModal = ref(false);
    const createLoading = ref(false);
    const createError = ref("");
    const createForm = reactive({ type: "", alias: "", display_name: "" });

    const showRenameModal = ref(false);
    const renameLoading = ref(false);
    const renameError = ref("");
    const renameForm = reactive({ areaId: null, display_name: "" });

    function openCreate() {
      createForm.type = "";
      createForm.alias = "";
      createForm.display_name = "";
      createError.value = "";
      showCreateModal.value = true;
    }

    async function submitCreate() {
      createLoading.value = true;
      createError.value = "";

      const result = await areasStore.createArea({ ...createForm });
      createLoading.value = false;

      if (!result.ok) {
        createError.value = result.error?.message || "Failed to create area";
        return;
      }

      showCreateModal.value = false;
    }

    function openRename(area) {
      renameForm.areaId = area.id;
      renameForm.display_name = area.display_name;
      renameError.value = "";
      showRenameModal.value = true;
    }

    async function submitRename() {
      renameLoading.value = true;
      renameError.value = "";

      const result = await areasStore.renameArea(renameForm.areaId, renameForm.display_name);
      renameLoading.value = false;

      if (!result.ok) {
        renameError.value = result.error?.message || "Failed to rename area";
        return;
      }

      showRenameModal.value = false;
    }

    async function remove(area) {
      if (!window.confirm(`Remove area "${area.display_name}"?`)) {
        return;
      }
      await areasStore.removeArea(area.id);
    }

    async function unassign(area) {
      await areasStore.unassignArea(area.id);
    }

    onMounted(() => {
      areasStore.loadAreas();
    });
    </script>

    <style scoped>
    .form-group {
      margin-bottom: 16px;
    }
    </style>