forked from KipsangEmmanuel/Mini-Jumia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin.js
More file actions
54 lines (50 loc) · 1.58 KB
/
admin.js
File metadata and controls
54 lines (50 loc) · 1.58 KB
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
// attach our data from db
function displayProducts(product) {
let tableRow = document.createElement("tr")
tableRow.id = "table-row"
tableRow.innerHTML = `
<th scope="row">${product.id}</th>
<td>${product.title}</td>
<td>${product.description}</td>
<td>${product.image}</td>
<td>${product.price}</td>
<td><button class="btn" id="edit">Edit</button></td>
<td><button class="btn" id="delete">Delete</button></td>
`
// get the id element table-body
document.querySelector('#table-body').append(tableRow)
// add event listener to the edit button
tableRow.querySelector('#edit').addEventListener('click', () => {
// call the function that edit the specific element
})
tableRow.querySelector('#delete').addEventListener('clck', () => {
// funmction that deletes a specific element
})
}
// fetch the data the db
// the endpoint is products
let base_URL = "http://localhost:3000"
function fetchProduct() {
fetch(`${base_URL}/products`)
.then((res) => res.json())
.then(products =>
products.forEach((product) => {
displayProducts(product)
}))
}
fetchProduct()
// function to collect the form data
// GET
function collectFormData() {
let form = document.querySelector('#form')
form.addEventListener('submit', (e) => {
// e.preventDefault()
formData = {
title: e.target.name.value,
image: e.target.image.value,
description: e.target.description.value
}
// postProducts function goes here
})
}
collectFormData();