-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinventory.js
98 lines (84 loc) · 3.47 KB
/
inventory.js
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
'use strict'
function renderIngrTable() {
const tbodyEl = document.querySelector('tbody');
for(let i = 0; i < Ingredient.userInventory.length; i += 1){
const trEl = document.createElement('tr');
tbodyEl.appendChild(trEl);
const tdElemRemove = document.createElement('td');
trEl.appendChild(tdElemRemove);
const btnElem = document.createElement('button');
tdElemRemove.appendChild(btnElem);
btnElem.id = `remove-${i}`;
btnElem.textContent = "X";
const tdElemName = document.createElement('td')
trEl.appendChild(tdElemName);
tdElemName.textContent = Ingredient.userInventory[i].name;
const tdElemType = document.createElement('td')
trEl.appendChild(tdElemType);
tdElemType.textContent = Ingredient.userInventory[i].type;
}
}
function clearIngrTable() {
const tbodyEl = document.querySelector('tbody');
while(tbodyEl.firstChild){
tbodyEl.removeChild(tbodyEl.firstChild);
}
}
//call getLocalStorage('ingredients')
//write to DOM a table of ingredients and type from userInventory
function inventoryEventListeners() {
//add an event listener to the add new + button and to the - button next to each ingredient in list
document.getElementById('add-inventory').addEventListener("click", toggleFormDisplay);
document.getElementById('inventory-table').addEventListener('click', confirmRemove);
}
function toggleFormDisplay() {
const form = document.querySelector(".inventory-form");
form.classList.toggle('hidden');
form.addEventListener('submit', addIngredient);
}
function addIngredient(event) {
event.preventDefault();
let name = document.querySelector('#bottle-name').value
let type = document.querySelector('#bottle-type').value
let ingredient = new Ingredient(name, type);
Ingredient.userInventory.push(ingredient);
Ingredient.userPlusBasicIngr.push(ingredient);
alphabetize(Ingredient.userInventory);
saveToLocalStorage('Ingredient');
clearIngrTable();
renderIngrTable();
//hides and clears form
toggleFormDisplay();
document.querySelector('#bottle-name').value = '';
document.querySelector('#bottle-type').value = '';
}
function confirmRemove(event){
let id = event.target.id; //remove-0
let idText = id.slice(0, 6);//remove
Ingredient.removalIndex = id.slice(7, id.length); //0 //Ingredient.removalIndex
let removePrompt = document.getElementById('confirm-remove');
let ingrToRemove = document.getElementById('ingr-to-remove');
if(idText === 'remove'){
ingrToRemove.textContent = `Please confirm removal of Ingredient: ${Ingredient.userInventory[Ingredient.removalIndex].name}`
removePrompt.classList.remove('hidden');
removePrompt.addEventListener('click', inventoryRemoval);
}
}
function inventoryRemoval(promptEvent){
let removePrompt = document.getElementById('confirm-remove');
if (promptEvent.target.id === 'rmv'){
// removeIngredient(id);
Ingredient.userInventory.splice(Ingredient.removalIndex, 1);
removePrompt.classList.add('hidden');
removePrompt.removeEventListener('click', inventoryRemoval);
saveToLocalStorage('Ingredient');
clearIngrTable();
renderIngrTable();
} else if (promptEvent.target.id === 'cnl'){
removePrompt.classList.add('hidden');
}
}
//Function Execution Order:
loadObjects();
renderIngrTable();
inventoryEventListeners();