Skip to content

Commit 2b4d55a

Browse files
committed
Implement product delete
1 parent cfd66b5 commit 2b4d55a

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

backend/src/store/actions.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,7 @@ export function createProduct({commit}, product) {
5757
}
5858
return axiosClient.post('/products', product)
5959
}
60+
61+
export function deleteProduct({commit}, id) {
62+
axiosClient.delete(`/products/${id}`)
63+
}

backend/src/views/Products.vue

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,14 @@
4949
@click="sortProducts('updated_at')">
5050
Last Updated At
5151
</TableHeaderCell>
52+
<TableHeaderCell field="actions">
53+
Actions
54+
</TableHeaderCell>
5255
</tr>
5356
</thead>
5457
<tbody v-if="products.loading">
5558
<tr>
56-
<td colspan="5">
59+
<td colspan="6">
5760
<Spinner/>
5861
</td>
5962
</tr>
@@ -64,16 +67,75 @@
6467
<td class="border-b p-2 ">
6568
<img class="w-16 h-16 object-cover" :src="product.image_url" :alt="product.title">
6669
</td>
67-
<td class="border-b p-2 max-w-[200px] whitespace-nowrap overflow-hidden text-ellipsis">{{
68-
product.title
69-
}}
70+
<td class="border-b p-2 max-w-[200px] whitespace-nowrap overflow-hidden text-ellipsis">
71+
{{ product.title }}
7072
</td>
7173
<td class="border-b p-2">
7274
{{ product.price }}
7375
</td>
7476
<td class="border-b p-2 ">
7577
{{ product.updated_at }}
7678
</td>
79+
<td class="border-b p-2 ">
80+
<Menu as="div" class="relative inline-block text-left">
81+
<div>
82+
<MenuButton
83+
class="inline-flex items-center justify-center w-full justify-center rounded-full w-10 h-10 bg-black bg-opacity-0 text-sm font-medium text-white hover:bg-opacity-5 focus:bg-opacity-5 focus:outline-none focus-visible:ring-2 focus-visible:ring-white focus-visible:ring-opacity-75"
84+
>
85+
<DotsVerticalIcon
86+
class="h-5 w-5 text-indigo-500"
87+
aria-hidden="true"/>
88+
</MenuButton>
89+
</div>
90+
91+
<transition
92+
enter-active-class="transition duration-100 ease-out"
93+
enter-from-class="transform scale-95 opacity-0"
94+
enter-to-class="transform scale-100 opacity-100"
95+
leave-active-class="transition duration-75 ease-in"
96+
leave-from-class="transform scale-100 opacity-100"
97+
leave-to-class="transform scale-95 opacity-0"
98+
>
99+
<MenuItems
100+
class="absolute z-10 right-0 mt-2 w-32 origin-top-right divide-y divide-gray-100 rounded-md bg-white shadow-lg ring-1 ring-black ring-opacity-5 focus:outline-none"
101+
>
102+
<div class="px-1 py-1">
103+
<MenuItem v-slot="{ active }">
104+
<button
105+
:class="[
106+
active ? 'bg-indigo-600 text-white' : 'text-gray-900',
107+
'group flex w-full items-center rounded-md px-2 py-2 text-sm',
108+
]"
109+
>
110+
<PencilIcon
111+
:active="active"
112+
class="mr-2 h-5 w-5 text-indigo-400"
113+
aria-hidden="true"
114+
/>
115+
Edit
116+
</button>
117+
</MenuItem>
118+
<MenuItem v-slot="{ active }">
119+
<button
120+
:class="[
121+
active ? 'bg-indigo-600 text-white' : 'text-gray-900',
122+
'group flex w-full items-center rounded-md px-2 py-2 text-sm',
123+
]"
124+
@click="deleteProduct(product)"
125+
>
126+
<TrashIcon
127+
:active="active"
128+
class="mr-2 h-5 w-5 text-indigo-400"
129+
aria-hidden="true"
130+
/>
131+
Delete
132+
</button>
133+
</MenuItem>
134+
</div>
135+
</MenuItems>
136+
</transition>
137+
</Menu>
138+
</td>
77139
</tr>
78140
</tbody>
79141
</table>
@@ -120,6 +182,8 @@ import Spinner from "../components/core/Spinner.vue";
120182
import {PRODUCTS_PER_PAGE} from "../constants";
121183
import TableHeaderCell from "../components/core/Table/TableHeaderCell.vue";
122184
import AddNewProduct from "./AddNewProduct.vue";
185+
import {Menu, MenuButton, MenuItem, MenuItems} from "@headlessui/vue";
186+
import {DotsVerticalIcon, PencilIcon, TrashIcon} from '@heroicons/vue/outline'
123187
124188
const perPage = ref(PRODUCTS_PER_PAGE);
125189
const search = ref('');
@@ -170,6 +234,17 @@ function sortProducts(field) {
170234
function showAddNewModal() {
171235
showProductModal.value = true
172236
}
237+
238+
function deleteProduct(product) {
239+
if (!confirm(`Are you sure you want to delete the product?`)) {
240+
return
241+
}
242+
store.dispatch('deleteProduct', product.id)
243+
.then(res => {
244+
// TODO Show notification
245+
store.dispatch('getProducts')
246+
})
247+
}
173248
</script>
174249

175250
<style scoped>

0 commit comments

Comments
 (0)