-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathproducts.html
141 lines (130 loc) · 6.42 KB
/
products.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Products CRUD</title>
<!-- add alpine, axios and tailwind -->
<script defer src="https://cdn.jsdelivr.net/npm/alpinejs@3.x.x/dist/cdn.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
<script src="//cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100" x-data="store" x-init="fetchProducts()">
<div class="container mx-auto p-4">
<h1 class="text-2xl font-bold mb-4">Products Dashboard</h1>
<!-- Create Form -->
<form @submit.prevent id="createProductForm" class="mb-4 bg-white p-4 rounded shadow">
<h2 class="text-lg font-semibold mb-2">Add New Product</h2>
<div class="grid grid-cols-3 gap-4">
<input x-model="name" type="text" id="name" placeholder="Name" class="border p-2 rounded" required>
<input x-model="description" type="text" id="description" placeholder="Description" class="border p-2 rounded" required>
<input x-model="price" type="number" id="price" placeholder="Price" class="border p-2 rounded" required>
</div>
<button @click="addProduct()" type="submit" class="mt-2 px-4 py-2 bg-indigo-500 text-white rounded hover:bg-indigo-600">Add Product</button>
</form>
<!-- Products List -->
<table class="w-full bg-white rounded shadow">
<thead class="bg-gray-200">
<tr>
<th class="p-2 text-left">Name</th>
<th class="p-2 text-left">Description</th>
<th class="p-2 text-left">Price</th>
<th class="p-2 text-left">Actions</th>
</tr>
</thead>
<tbody id="productsList">
<!-- Products will be dynamically inserted here -->
<template x-for="product in products" :key="product.id">
<tr>
<td class="p-2" x-text="product.name"></td>
<td class="p-2" x-text="product.description"></td>
<td class="p-2" x-text="'$' + product.price"></td>
<td class="p-2 flex gap-2">
<button @click="editProduct(product)" class="edit-btn px-2 py-1 bg-amber-500 text-white rounded hover:bg-amber-600">Edit</button>
<button @click="deleteProduct(product)" class="delete-btn px-2 py-1 bg-rose-500 text-white rounded hover:bg-rose-600">Delete</button>
</td>
</tr>
</template>
</tbody>
</table>
<!-- Edit Product Modal -->
<div x-show="open" id="editProductModal" class="fixed inset-0 bg-gray-800 bg-opacity-50 flex items-center justify-center" style="display: none;">
<div class="bg-white p-4 rounded shadow w-1/3">
<h2 class="text-lg font-semibold mb-2">Edit Product</h2>
<form @submit.prevent id="editProductForm">
<div class="mb-4">
<label class="block text-sm font-medium mb-1">Name</label>
<input x-model="editName" type="text" id="editName" class="border p-2 rounded w-full" required>
</div>
<div class="mb-4">
<label class="block text-sm font-medium mb-1">Description</label>
<input x-model="editDescription" type="text" id="editDescription" class="border p-2 rounded w-full" required>
</div>
<div class="mb-4">
<label class="block text-sm font-medium mb-1">Price</label>
<input x-model="editPrice" type="number" id="editPrice" class="border p-2 rounded w-full" required>
</div>
<div class="flex justify-end gap-2">
<button @click="open=false" type="button" id="cancelEdit" class="px-4 py-2 bg-gray-300 rounded hover:bg-gray-400">Cancel</button>
<button @click="saveProduct()" type="submit" class="px-4 py-2 bg-indigo-500 text-white rounded hover:bg-indigo-600">Save</button>
</div>
</form>
</div>
</div>
</div>
<script>
const store = {
products: [],
name: "",
description: "",
price: "",
open: false,
editName:"",
editDescription:"",
editPrice:"",
editId:"",
fetchProducts() {
axios.get('http://localhost:3000/products')
.then(response => {
this.products = response.data
})
},
addProduct() {
axios.post('http://localhost:3000/products', {
name: this.name,
description: this.description,
price: this.price
}).then(response => {
fetchProducts()
})
},
deleteProduct(product) {
console.log(product)
if (confirm('Are you sure you want to delete this product?')) {
axios.delete(`http://localhost:3000/products/${product.id}`)
.then(response => {
fetchProducts()
})
}
},
editProduct(product) {
this.editId = product.id
this.editName= product.name
this.editDescription = product.description
this.editPrice= product.price
this.open = true
},
saveProduct() {
axios.put(`http://localhost:3000/products/${this.editId}`, {
name: this.editName,
description: this.editDescription,
price: this.editPrice
}).then(response => {
fetchProducts()
this.open = false
})
}
}
</script>
</body>
</html>