/
index.js
134 lines (111 loc) · 4.58 KB
/
index.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
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
var foodChoiceInput = document.getElementById("user-food-choice");
var displayFoodChoice = document.getElementById("display-food-choice");
var errorMessage = document.getElementById("form-error");
var foodChosen = document.getElementById("food-choice");
var lTable = document.getElementById("loginTable");
//Variable to detect how many times a user has generated a random food choice
var foodCount = 0;
//Array containing initial food choices
var foodChoices = ["Yam and egg", "Jollof rice", "Bread and egg", "Cereal", "Indomie", "Beans", "Efo riro", "Ofada rice and stew"];
/*
saving data in case user refreshes or leaves
page
*/
let obj = localStorage.getItem('localFoodChoices') ? JSON.parse(localStorage.getItem('localFoodChoices')) : [];
//This shows the default values first
localStorage.setItem('localFoodChoices', JSON.stringify(foodChoices));
localStorage.setItem('localFoodChoices', JSON.stringify(obj));
// Display the choices in initial food choices
obj.map(food => updateFoodChoices(food));
lTable.style.visibility = "hidden"
// Function to display added food choices
function updateFoodChoices(food)
{
let listItem = document.createElement("li");
let btn = document.createElement("button");
let icon = document.createElement("i");
icon.classList.add("fas");
icon.classList.add("fa-minus");
btn.appendChild(icon);
listItem.textContent = food;
listItem.appendChild(btn);
btn.addEventListener("click", deleteFoodChoice);
displayFoodChoice.appendChild(listItem);
}
function addFoodChoice()
{
if (foodChoiceInput.value === "") {
errorMessage.innerHTML = "Input cannot be empty"
} else if (foodChoices.includes(foodChoiceInput.value)) {
errorMessage.innerHTML = "Cannot include duplicate item"
} else {
errorMessage.innerHTML = "";
foodCount = 0;
let food = foodChoiceInput.value;
updateFoodChoices(food);
foodChoiceInput.value="";
foodChoices.push(food);
obj.push(food);
localStorage.setItem('localFoodChoices', JSON.stringify(obj));
}
}
// Allow food choice to be added if Enter key is pressed in the input
foodChoiceInput.onkeydown = function(event){
if (event.which == 13 || event.keyCode == 13) {
addFoodChoice()
return false;
}
return true;
};
// function deleteFoodChoice(e) {
// var index = foodChoices.indexOf(e.innerText);
// if (index >= 0) {
// foodChoices.splice(index, 1);
// displayFoodChoice.removeChild(displayFoodChoice.children[index]);
// }
// }
function deleteFoodChoice(e)
{
console.log("clicked");
//Chrome was misbehaving when i use only d first one
let food = e.target.parentElement.textContent || e.target.parentElement.parentElement.textContent ;
console.log(e.target.parentElement.parentElement);
console.log(food);
let index = obj.indexOf(food);
if( index > -1)
{
//No need for this anymore
// foodChoices.splice(index, 1);
obj.splice(index, 1);
console.log("removed");
displayFoodChoice.removeChild(displayFoodChoice.children[index]);
localStorage.setItem('localFoodChoices', JSON.stringify(obj));
}
}
function generateRandomFood() {
if (obj.length === 0) {
errorMessage.innerHTML = "Can't choose if there's nothing to choose from"
} else if (obj.length === 1) {
errorMessage.innerHTML = "It's not very random if you only have one option"
} else {
foodCount++;
// Condition to ensure user can only generate random food once
if (foodCount <= 1) {
lTable.style.visibility = "visible";
errorMessage.innerHTML = "";
var randomIndex = Math.floor(Math.random()*obj.length);
foodChosen.innerHTML = obj[randomIndex];
renderSuggestedLinks();
} else {
errorMessage.innerHTML = "Sorry, no takebacks. It wouldn't be very random if you could just keep clicking. Enjoy your meal!"
}
}
}
function renderSuggestedLinks() {
var recipesLink = document.getElementById('recipes-link');
var restaurants = document.getElementById('restaurants-link');
recipesLink.innerHTML = `${foodChosen.innerText} recipes`;
recipesLink.href = `https://www.google.com/search?q=${foodChosen.innerText.toLowerCase().replace(/[^a-zA-Z]/g,"+")}+recipes`;
restaurants.innerHTML = `Places that have ${foodChosen.innerText} near you`;
restaurants.href = `https://www.google.com/search?q=${foodChosen.innerText.toLowerCase().replace(/[^a-zA-Z]/g,"+")}+near+me`;
}