-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
184 lines (105 loc) · 3.37 KB
/
script.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// get forms
const form = document.forms[0];
const inputElem = form.elements.namedItem('name');
// get container list friends
const listFriends = document.getElementById('list-friends');
//// Functions
function saveFriend(event) {
// prevent
event.preventDefault();
// get value form input user
const nameFriend = inputElem.value;
let friend = {
name: nameFriend,
};
// Store
addStore(friend);
// Add to ui
addToUi()
// log
console.log(inputElem);
}
function addToUi() {
// get data from localStorage
const getNames = JSON.parse(localStorage.getItem('friends'));
if(listFriends.innerHTML == '' && localStorage.getItem('friends') === null) {
const pictEmpty = document.createElement('div');
pictEmpty.className = 'row';
pictEmpty.innerHTML = `
<div class="col-12 col-sm-8 col-md-7 col-lg-5 mx-auto">
<img src="wait.svg" class="img-fluid">
</div>`;
listFriends.append(pictEmpty);
} else if(listFriends.innerHTML == '' && getNames.length === 0) {
const pictEmpty = document.createElement('div');
pictEmpty.className = 'row';
pictEmpty.innerHTML = `
<div class="col-12 col-sm-8 col-md-7 col-lg-5 mx-auto">
<img src="wait.svg" class="img-fluid">
</div>`;
listFriends.append(pictEmpty);
} else {
// gunanya untuk reset ketika manggil ulang
listFriends.innerHTML = '';
getNames.forEach((item, i) => {
// create elem
let newli = document.createElement('li');
// add class
newli.className = 'list-group-item text-capitalize d-flex justify-content-between align-items-center';
newli.innerHTML = `${item.name} <button type="button" class="btn btn-danger btn-sm ms-5" id="btn-hapus">Hapus</button>`;
listFriends.append(newli);
})
}
console.log(listFriends.innerHTML);
}
function addStore(data) {
if(localStorage.getItem('friends') === null) {
let friends = [];
friends.push(data);
localStorage.setItem('friends', JSON.stringify(friends));
}else {
let friends = JSON.parse(localStorage.getItem('friends'));
friends.push(data);
localStorage.setItem('friends', JSON.stringify(friends));
}
}
function deleteFriends(event) {
if(event.target.id === 'btn-hapus') {
let data = event.target.parentElement.firstChild.textContent.trim();
deleteFriend(data);
// call ui
addToUi();
}
// console.log();
}
function deleteFriend(data) {
// get data from localStorage
const getNames = JSON.parse(localStorage.getItem('friends'));
getNames.forEach((item, i) => {
if(data == item.name) {
getNames.splice(i, 1);
localStorage.setItem("friends", JSON.stringify(getNames));
}
})
// call add ui
addToUi();
// log
// console.log(getNames);
}
function removeDisabled(event) {
const btnAdd = document.getElementById('btn-add');
if(form.elements.namedItem('name').value === "") {
btnAdd.disabled = true;
} else {
btnAdd.disabled = false;
btnAdd.style.cursor = "";
document.querySelector('.button-field').style.cursor = "";
}
// log
// console.log(document.querySelector('.button-field'));
}
/// events
document.addEventListener("DOMContentLoaded", addToUi);
form.addEventListener('submit', saveFriend);
inputElem.addEventListener('keyup', removeDisabled);
listFriends.addEventListener('click', deleteFriends)