aboutsummaryrefslogtreecommitdiff
path: root/src/components/items_table.vue
blob: f429eb8bccdad74197920dd03a915d70f8bcb242 (plain)
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
<script setup lang="js">
import { ref, onMounted } from 'vue'
import { RouterLink } from 'vue-router'
import axios from 'axios'
import { useToast } from 'vue-toast-notification'

const toast = useToast({
  position: 'top-right'
})

const allBrands = ref([])
const allItems = ref([])
const isLoading = ref(false)

const getAllItems = async () => {
  allItems.value = []
   allBrands.value = []
   isLoading.value = true

   try {
     const res = await axios.get('/item')
     if (res.status === 200) {
       allItems.value = res.data.data
     }

     const r = await axios.get('/brand')
     if (r.status === 200) {
       allBrands.value = r.data.data
     }
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }

  isLoading.value = false
}

const handleDelete = async (id) => {
  try {
    const res = await axios.delete(`/item/${id}`)
    if (res.status === 200) {
      toast.success('Successfully deleted item')
    }

    getAllItems()
  } catch (err) {
    toast.error('An unhandled exception occoured. Please check logs')
    console.error(err)
  }
}

onMounted(() => {
  getAllItems()
})
</script>

<template>
  <div v-if="isLoading" class="w-100 d-flex justify-content-center">
    <div class="spinner-border" role="status">
      <span class="sr-only"></span>
    </div>
  </div>

  <div v-else-if="allItems.length === 0" class="w-100 d-flex justify-content-center">
    <p>No items added! <RouterLink to="/item/new">Add new.</RouterLink></p>
  </div>

  <table v-else class="table table-striped table-hover">
    <thead>
      <tr>
        <th scope="col">#</th>
        <th scope="col">Item Name</th>
        <th scope="col">Description</th>
        <th scope="col">Brand</th>
        <th scope="col">HSN</th>
        <th scope="col">Unit Price</th>
        <th scope="col">UOM</th>

        <th scope="col" class="table-action-column">
          <div class="wrapper">
            <RouterLink to="/item/new">
              <button class="btn btn-dark">
                <i class="bi bi-plus-lg"></i>
              </button>
            </RouterLink>
            <button class="btn btn-dark" v-on:click="getAllItems">
              <i class="bi bi-arrow-clockwise"></i>
            </button>
          </div>
        </th>
      </tr>
    </thead>

    <tbody>
      <tr v-for="(item, index) in allItems" :key="item['id']">
        <td scope="row">{{ index + 1 }}</td>
        <td>{{ item.Name }}</td>
        <td>{{ item.Description }}</td>
        <td>{{ item.Brand.Name }}</td>
        <td>{{ item.HSN }}</td>
        <td>{{ item.UnitPrice }}</td>
        <td>{{ item.UnitOfMeasure }}</td>

        <td class="table-action-column">
          <div class="wrapper">
            <button class="btn" data-bs-toggle="dropdown" aria-expanded="false">
              <i class="bi bi-caret-down-fill"></i>
            </button>

            <div class="dropdown">
              <ul class="dropdown-menu">
                <li>
                  <button class="dropdown-item disabled" v-on:click="console.log('Edit: ', item.ID)">
                    Edit Item
                  </button>
                </li>
                <li>
                  <button class="dropdown-item" v-on:click="handleDelete(item.ID)">
                    Delete Item
                  </button>
                </li>
              </ul>
            </div>
          </div>
        </td>
      </tr>
    </tbody>
  </table>
</template>

<style>
.table-action-column .wrapper {
  display: flex;
  justify-content: flex-end;
}

thead .table-action-column .wrapper {
  gap: 0.5rem;
}

tbody .table-action-column .btn {
  opacity: 0;
  transition: opacity 300ms;
}

tbody tr:hover .table-action-column .btn {
  opacity: 1;
  transition: opacity 300ms;
}
</style>