@@ -2,6 +2,7 @@ const addNewProductBtn = document.querySelector("#add-new-product-btn");
2
2
const backdrop = document . querySelector ( "#backdrop" ) ;
3
3
const closeBtn = document . querySelector ( "#close" ) ;
4
4
const addProductBtn = document . querySelector ( "#add-product-btn" ) ;
5
+ const tableBody = document . querySelector ( "#table-body" ) ;
5
6
6
7
addNewProductBtn . addEventListener ( "click" , function ( ) {
7
8
backdrop . style . display = "block" ;
@@ -18,45 +19,173 @@ const initials = localStorage.getItem("initials");
18
19
19
20
document . getElementById ( "initials" ) . innerHTML = initials
20
21
21
- addProductForm . addEventListener ( "submit" , async ( e ) => {
22
- e . preventDefault ( ) ;
23
- // console.log("Product added successfully")
24
-
25
- // Get the form data
26
- // const productData = new FormData(e.target)
27
- // const productDataObject = Object.fromEntries(productData.entries())
28
- // console.log(productDataObject)
29
- const productNameInput = document . getElementById ( "product_name" ) ;
30
- const priceInput = document . getElementById ( "price" ) ;
31
- const perInput = document . getElementById ( "per" ) ;
32
- const statusInput = document . getElementById ( "status" ) ;
33
- // const statusInput2 = document.getElementById("status2")
34
-
35
- const productData = {
36
- product_name : productNameInput . value ,
37
- price : priceInput . value ,
38
- per : perInput . value ,
39
- product_status : statusInput . checked ? "Available" : "Out of stock" ,
40
- } ;
41
-
42
- // console.log(productData);
43
22
23
+ const productNameInput = document . getElementById ( "product_name" ) ;
24
+ const priceInput = document . getElementById ( "price" ) ;
25
+ const perInput = document . getElementById ( "per" ) ;
26
+ const statusInput = document . getElementById ( "status" ) ;
27
+ // Handle form submission for adding new product
28
+ if ( addProductBtn . innerHTML === "Add Product" ) {
29
+ addProductForm . addEventListener ( "submit" , async ( e ) => {
30
+ e . preventDefault ( ) ;
31
+ // console.log("Product added successfully")
32
+
33
+ // Get the form data
34
+ // const productData = new FormData(e.target)
35
+ // const productDataObject = Object.fromEntries(productData.entries())
36
+ // console.log(productDataObject)
37
+ // const statusInput2 = document.getElementById("status2")
38
+
39
+ const productData = {
40
+ product_name : productNameInput . value ,
41
+ price : priceInput . value ,
42
+ per : perInput . value ,
43
+ product_status : statusInput . checked ? "Available" : "Out of stock" ,
44
+ } ;
45
+
46
+ // console.log(productData);
47
+
48
+ try {
49
+ const response = await fetch ( `${ baseUrl } /products/create` , {
50
+ method : "POST" ,
51
+ headers : {
52
+ "Content-Type" : "application/json" ,
53
+ Authorization : `Bearer ${ userToken } ` ,
54
+ } ,
55
+ body : JSON . stringify ( productData ) ,
56
+ } ) ;
57
+ const data = await response . json ( ) ;
58
+ console . log ( data ) ;
59
+ if ( data . message === "Product Created successfully" ) {
60
+ alert ( data . message ) ;
61
+ backdrop . style . display = "none" ;
62
+ addProductForm . reset ( ) ;
63
+ fetchAllProducts ( ) ;
64
+ }
65
+
66
+ } catch ( error ) {
67
+ console . log ( "Error ==>" , error ) ;
68
+ }
69
+ } ) ;
70
+ }
71
+
72
+ // fetch all products
73
+ const fetchAllProducts = async ( ) => {
44
74
try {
45
- const response = await fetch ( `${ baseUrl } /products/create` , {
46
- method : "POST" ,
47
- headers : {
48
- "Content-Type" : "application/json" ,
49
- Authorization : `Bearer ${ userToken } ` ,
50
- } ,
51
- body : JSON . stringify ( productData ) ,
75
+ const response = await fetch ( `${ baseUrl } /products/all` , {
76
+ headers : { Authorization : `Bearer ${ userToken } ` , }
52
77
} ) ;
78
+
79
+ tableBody . innerHTML = "" ; // Clear existing table rows
53
80
const data = await response . json ( ) ;
54
- console . log ( data ) ;
55
- if ( data . message === "Product Created successfully" ) {
56
- alert ( data . message ) ;
57
- addProductForm . reset ( ) ;
81
+ // console.log(data)
82
+ if ( data . message === "All products retrieved successfully" ) {
83
+ data . products . forEach ( ( product ) => {
84
+ const rows = document . createElement ( "tr" ) ;
85
+ rows . innerHTML = `
86
+ <td>${ product . product_name } </td>
87
+ <td>${ product . price } </td>
88
+ <td>${ product . per } </td>
89
+ <td>${ product . order_no } </td>
90
+ <td>${ product . product_status } </td>
91
+ <td>
92
+ <button class="edit-btn" data-id=${ product . order_no } >Edit</button>
93
+ <button class="delete-btn" data-id=${ product . order_no } >Delete</button>
94
+ </td>
95
+ ` ;
96
+ tableBody . appendChild ( rows ) ;
97
+ } )
58
98
}
59
99
} catch ( error ) {
60
100
console . log ( "Error ==>" , error ) ;
61
101
}
62
- } ) ;
102
+ }
103
+
104
+ // Fetch Products when the page loads
105
+ fetchAllProducts ( ) ;
106
+
107
+ // Handle Product edit and delete
108
+ tableBody . addEventListener ( "click" , async ( e ) => {
109
+ e . preventDefault ( )
110
+ const target = e . target ;
111
+ const order_no = target . dataset . id
112
+
113
+ if ( target . classList . contains ( "edit-btn" ) ) {
114
+
115
+ try {
116
+ const response = await fetch ( `${ baseUrl } /products/${ order_no } ` , {
117
+ headers : { Authorization : `Bearer ${ userToken } ` } ,
118
+ } ) ;
119
+ const data = await response . json ( ) ;
120
+ console . log ( data ) ;
121
+ if ( data . message === "Product retrieved successfully" ) {
122
+ document . getElementById ( "backdrop-heading" ) . innerHTML = "Edit Product" ;
123
+ addProductBtn . innerHTML = "Save" ;
124
+ backdrop . style . display = "block" ;
125
+
126
+
127
+ document . getElementById ( "product_name" ) . value = data . product [ 0 ] . product_name ;
128
+ document . getElementById ( "price" ) . value = data . product [ 0 ] . price
129
+ document . getElementById ( "per" ) . value = data . product [ 0 ] . per
130
+ // check the radio button based on the product_status
131
+ if ( data . product [ 0 ] . product_status === "Available" ) {
132
+ document . getElementById ( "status" ) . checked = true
133
+ } else {
134
+ document . getElementById ( "status2" ) . checked = true
135
+ }
136
+ }
137
+
138
+ // Handle Updating Product
139
+ if ( addProductBtn . innerHTML === "Save" ) {
140
+ addProductForm . addEventListener ( "submit" , async ( e ) => {
141
+ e . preventDefault ( ) ;
142
+ const updatedProductData = {
143
+ product_name : productNameInput . value ,
144
+ price : priceInput . value ,
145
+ per : perInput . value ,
146
+ product_status : statusInput . checked ? "Available" : "Out of stock" ,
147
+ } ;
148
+
149
+ try {
150
+ const response = await fetch ( `${ baseUrl } /products/${ order_no } ` , {
151
+ method : "PUT" ,
152
+ headers : {
153
+ "Content-Type" : "application/json" ,
154
+ Authorization : `Bearer ${ userToken } `
155
+ } ,
156
+ body : JSON . stringify ( updatedProductData )
157
+ } ) ;
158
+ const data = await response . json ( ) ;
159
+ // console.log(data);
160
+ if ( data . message === "Product updated successfully" ) {
161
+ alert ( data . message ) ;
162
+ backdrop . style . display = "none" ;
163
+ addProductForm . reset ( ) ;
164
+ fetchAllProducts ( ) ;
165
+ }
166
+ } catch ( error ) {
167
+ console . log ( "Error ==>" , error ) ;
168
+ }
169
+ } )
170
+ }
171
+ } catch ( error ) {
172
+ console . log ( "Error ==>" , error ) ;
173
+ }
174
+
175
+ } else if ( target . classList . contains ( "delete-btn" ) ) {
176
+ try {
177
+ const response = await fetch ( `${ baseUrl } /products/${ order_no } ` , {
178
+ method : "DELETE" ,
179
+ headers : { Authorization : `Bearer ${ userToken } ` } ,
180
+ } ) ;
181
+ const data = await response . json ( )
182
+
183
+ if ( data . message === "Product deleted successfully" ) {
184
+ alert ( data . message ) ;
185
+ fetchAllProducts ( ) ;
186
+ }
187
+ } catch ( error ) {
188
+ console . log ( "Error ==>" , error ) ;
189
+ }
190
+ }
191
+ } )
0 commit comments