8
8
< div class ="content ">
9
9
< ion-card >
10
10
< ion-card-content >
11
+ < div id ="loginForm ">
11
12
< ion-text > < h2 > Login Admin Product</ h2 > </ ion-text >
12
13
< ion-list >
13
14
< ion-item lines ="full ">
22
23
</ ion-item >
23
24
</ ion-list >
24
25
< ion-button expand ="block " fill ="outline " id ="loginBtn "> Sign in</ ion-button >
25
- < ion-button expand ="block " fill ="outline " id ="logoutBtn " style ="display: none; "> Logout</ ion-button >
26
-
26
+ </ div >
27
27
< form id ="productForm " style ="display: none; ">
28
28
< h2 > Tambah Produk</ h2 >
29
29
< p />
@@ -33,10 +33,11 @@ <h2>Tambah Produk</h2>
33
33
< input type ="text " id ="categories " placeholder ="Kategori " required > < br >
34
34
< input type ="text " id ="tags " placeholder ="Tag " required > < br >
35
35
< input type ="number " id ="rating " placeholder ="Rating (1-5) " required > < br >
36
- < textarea id ="images " placeholder ="Masukkan URL gambar, pisahkan dengan koma " required > </ textarea > < br >
36
+ < textarea id ="image " placeholder ="Masukkan URL gambar, pisahkan dengan koma " required > </ textarea > < br >
37
37
< textarea id ="deskripsi " placeholder ="Deskripsi Produk " required > </ textarea > < br >
38
38
< button type ="submit "> Tambah Produk</ button >
39
39
</ form >
40
+ < ion-button expand ="block " fill ="outline " id ="logoutBtn " style ="display: none; "> Logout</ ion-button >
40
41
</ ion-card-content >
41
42
</ ion-card >
42
43
< div id ="productList "> </ div >
@@ -55,15 +56,19 @@ <h2>Tambah Produk</h2>
55
56
appId : "1:489703180131:web:4383f5ef04c1d414ce3556" ,
56
57
measurementId : "G-ZWW4K4NVQ6"
57
58
} ;
59
+
60
+ firebase . initializeApp ( firebaseConfig ) ;
61
+
58
62
// Initialize Firebase
59
- const app = firebase . initializeApp ( firebaseConfig ) ;
60
63
const auth = firebase . auth ( ) ;
61
64
const db = firebase . firestore ( ) ;
65
+
62
66
// DOM Content Loaded
63
67
document . addEventListener ( "DOMContentLoaded" , function ( ) {
64
68
const loginBtn = document . getElementById ( "loginBtn" ) ;
65
69
const logoutBtn = document . getElementById ( "logoutBtn" ) ;
66
70
const productForm = document . getElementById ( "productForm" ) ;
71
+ const loginForm = document . getElementById ( "loginForm" ) ;
67
72
68
73
// 🔹 Cek apakah elemen ada sebelum menambahkan event listener
69
74
if ( loginBtn && logoutBtn && productForm ) {
@@ -75,10 +80,10 @@ <h2>Tambah Produk</h2>
75
80
const userCredential = await auth . signInWithEmailAndPassword ( email , password ) ;
76
81
const user = userCredential . user ;
77
82
78
- // Cek apakah user adalah admin
79
- const idTokenResult = await user . getIdTokenResult ( ) ;
80
- if ( idTokenResult . claims . admin ) {
81
- showAlert ( "Login berhasil sebagai admin" ) ;
83
+ // Cek apakah user adalah admin di Firestore
84
+ const adminDoc = await db . collection ( "admins" ) . doc ( user . uid ) . get ( ) ;
85
+ if ( adminDoc . exists ) {
86
+ showAlert ( "Login sebagai admin berhasil " ) ;
82
87
updateUI ( true ) ;
83
88
} else {
84
89
showAlert ( "Anda bukan admin product!" ) ;
@@ -100,10 +105,12 @@ <h2>Tambah Produk</h2>
100
105
function updateUI ( isAdmin ) {
101
106
if ( isAdmin ) {
102
107
productForm . style . display = "block" ;
108
+ loginForm . style . display = "none" ;
103
109
loginBtn . style . display = "none" ;
104
110
logoutBtn . style . display = "block" ;
105
111
} else {
106
112
productForm . style . display = "none" ;
113
+ loginForm . style . display = "block" ;
107
114
loginBtn . style . display = "block" ;
108
115
logoutBtn . style . display = "none" ;
109
116
}
@@ -120,6 +127,114 @@ <h2>Tambah Produk</h2>
120
127
} ) ;
121
128
}
122
129
} ) ;
130
+
131
+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
132
+ const form = document . getElementById ( "productForm" ) ;
133
+ const productList = document . getElementById ( "productList" ) ;
134
+
135
+ if ( ! form ) {
136
+ console . error ( "Form dengan ID 'productForm' tidak ditemukan!" ) ;
137
+ return ;
138
+ }
139
+
140
+ form . addEventListener ( "submit" , async function ( e ) {
141
+ e . preventDefault ( ) ;
142
+
143
+ // Pastikan semua elemen input ada
144
+ const titleInput = document . getElementById ( "title" ) ;
145
+ const hargaInput = document . getElementById ( "harga" ) ;
146
+ const stokInput = document . getElementById ( "stok" ) ;
147
+ const categoriesInput = document . getElementById ( "categories" ) ;
148
+ const tagsInput = document . getElementById ( "tags" ) ;
149
+ const ratingInput = document . getElementById ( "rating" ) ;
150
+ const imageInput = document . getElementById ( "image" ) ;
151
+ const deskripsiInput = document . getElementById ( "deskripsi" ) ;
152
+
153
+ if ( ! titleInput || ! hargaInput || ! stokInput || ! categoriesInput || ! tagsInput || ! ratingInput || ! imageInput || ! deskripsiInput ) {
154
+ console . error ( "Satu atau lebih elemen input tidak ditemukan!" ) ;
155
+ return ;
156
+ }
157
+
158
+ const images = imageInput . value . split ( "," ) . map ( img => img . trim ( ) ) . filter ( img => img !== "" ) ;
159
+
160
+ const product = {
161
+ title : titleInput . value ,
162
+ harga : hargaInput . value ,
163
+ stok : stokInput . value ,
164
+ categories : categoriesInput . value ,
165
+ tags : tagsInput . value ,
166
+ rating : parseFloat ( ratingInput . value ) || 0 ,
167
+ images : images ,
168
+ deskripsi : deskripsiInput . value
169
+ } ;
170
+
171
+ try {
172
+ const user = auth . currentUser ;
173
+ if ( ! user ) {
174
+ showAlert ( "Anda harus login sebagai admin!" ) ;
175
+ return ;
176
+ }
177
+
178
+ // Refresh token untuk memastikan claims terbaru
179
+ await user . getIdToken ( true ) ;
180
+ const idTokenResult = await user . getIdTokenResult ( ) ;
181
+
182
+ console . log ( "User claims:" , idTokenResult . claims ) ;
183
+
184
+ if ( ! idTokenResult . claims . admin ) {
185
+ showAlert ( "Anda bukan admin!" ) ;
186
+ return ;
187
+ }
188
+
189
+ await db . collection ( "products" ) . add ( product ) ;
190
+ showAlert ( "Produk berhasil ditambahkan!" , "success" ) ;
191
+ form . reset ( ) ;
192
+ loadProducts ( ) ;
193
+ } catch ( error ) {
194
+ console . error ( "Error:" , error ) ;
195
+ showAlert ( "Gagal menambahkan produk." ) ;
196
+ }
197
+ } ) ;
198
+
199
+ async function loadProducts ( ) {
200
+ if ( ! productList ) {
201
+ console . error ( "Elemen 'productList' tidak ditemukan!" ) ;
202
+ return ;
203
+ }
204
+
205
+ productList . innerHTML = "" ;
206
+
207
+ try {
208
+ const querySnapshot = await db . collection ( "products" ) . get ( ) ;
209
+ querySnapshot . forEach ( ( doc ) => {
210
+ const data = doc . data ( ) ;
211
+ const productElement = document . createElement ( "div" ) ;
212
+
213
+ let imageSlides = data . images . map ( ( img , index ) => `
214
+ <div class="slide" ${ index === 0 ? 'style="display:block;"' : 'style="display:none;"' } >
215
+ <img src="${ img } " alt="Product Image">
216
+ </div>
217
+ ` ) . join ( "" ) ;
218
+
219
+ productElement . innerHTML = `
220
+ <div class="carousel">
221
+ ${ imageSlides }
222
+ <button class="prev" onclick="prevSlide(this)">❮</button>
223
+ <button class="next" onclick="nextSlide(this)">❯</button>
224
+ </div>
225
+ <h3>${ data . title } </h3>
226
+ <p>Harga: ${ data . harga } </p>
227
+ <p>${ data . deskripsi } </p>
228
+ ` ;
229
+ productList . appendChild ( productElement ) ;
230
+ } ) ;
231
+ } catch ( error ) {
232
+ console . error ( "Error:" , error ) ;
233
+ }
234
+ }
235
+
236
+ loadProducts ( ) ;
237
+ } ) ;
123
238
124
239
function showAlert ( message ) {
125
240
// Buat elemen ion-toast
@@ -134,7 +249,6 @@ <h2>Tambah Produk</h2>
134
249
toast . present ( ) ;
135
250
}
136
251
</ script >
137
- < script type ="module " src ="{{site.baseurl}}/product/add/product.js "> </ script >
138
252
139
253
< style >
140
254
.carousel {
0 commit comments