-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
129 lines (101 loc) · 3.25 KB
/
main.js
File metadata and controls
129 lines (101 loc) · 3.25 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
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
window.addEventListener('load', init);
let apiUrl = 'index.php';
let apiUrl2 = 'https://source.unsplash.com/1600x900/?';
function init() {
getFood();
}
function getFood() {
fetch(apiUrl)
.then((response) => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
})
.then(Succes)
.catch(Error);
}
function Succes(data) {
let section = document.getElementById('list');
for (let food of data) {
let $food = document.createElement('div');
$food.classList.add('food');
$food.innerHTML = '<h1>' + food.name + '<h1>';
section.appendChild($food);
let div = document.createElement('div');
$food.appendChild(div);
let img = document.createElement('img');
let url = apiUrl2 + food.name;
img.src = url;
div.appendChild(img);
let $favourite = document.createElement('div');
$favourite.classList.add('favourite');
$food.appendChild($favourite);
let $detail = document.createElement('div');
$detail.classList.add('recipe');
$detail.addEventListener('click', addItem);
$detail.id = food.id;
$detail.innerHTML = "Show Recipe";
$food.appendChild($detail);
}
if (typeof window.localStorage === "undefined") {
console.error('Local storage is not available in your browser');
return;
}
favourite();
}
function Error(data) {
console.log(data)
}
function addItem(e) {
let geheel = document.getElementById('details');
let tijdelijk = document.getElementById('java');
if (tijdelijk){
geheel.removeChild(tijdelijk);
}
let section = document.createElement('section');
section.id = 'java';
geheel.appendChild(section);
let clickedItem = e.target.id;
let url = apiUrl +'?id=' + clickedItem;
fetch(url)
.then((response) => {
if (!response.ok) {
throw new Error(response.statusText);
}
return response.json();
})
.then(Succes2)
.catch(Error);
}
function Succes2(data){
let section = document.getElementById('java')
section.innerHTML = '<h1>Recipe</h1>' + data.recipe + '<h1>Tags</h1>' + data.tags;
}
function favourite() {
let clickedItem = document.getElementsByClassName('favourite');
let food = document.getElementsByClassName('food');
for (let i = 0; i < food.length; i++) {
let border = food.item(i);
let click = clickedItem.item(i);
if (localStorage.getItem(i) !== null) {
let border2 = food.item(i);
border2.classList.add('yellow');
}
click.addEventListener('click', function (){
border.classList.toggle('yellow');
if (border.classList.contains('yellow')){
localStorage.setItem(i, i);
click.innerHTML = "Delete Favourite";
}else{
localStorage.removeItem(i);
click.innerHTML = "Favourite";
}
})
if (border.classList.contains('yellow')){
click.innerHTML = "Delete Favourite"
}else{
click.innerHTML = "Favourite"
}
}
}