forked from YoFirmy/notes_app
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterface.js
More file actions
71 lines (63 loc) · 2.06 KB
/
interface.js
File metadata and controls
71 lines (63 loc) · 2.06 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
"use strict";
document.addEventListener("DOMContentLoaded", () => {
let notesArray = localStorage.getItem('notes')
? JSON.parse(localStorage.getItem('notes'))
: [];
localStorage.setItem('notes', JSON.stringify(notesArray));
const data = JSON.parse(localStorage.getItem('notes'));
data.forEach((item) => {
createNewDiv(item);
})
function createNewDiv(note) {
const div = document.createElement('div');
div.className = 'note-div';
div.innerText = note.title + "...";
document.getElementById('right-side').appendChild(div);
listenForClick(div, note)
};
function listenForClick(div, note) {
div.addEventListener('click', () => {
div.classList.toggle('hide');
if (div.innerText == note.text){
setTimeout(function() {
div.innerText = note.title + '...';
div.classList.toggle('hide');
}, 700);
} else {
setTimeout(function() {
div.innerText = note.text;
div.classList.toggle('hide');
}, 700);
};
});
};
function createNoteFromText() {
const text = document.querySelector('#text-input').value
try {
if(text == "") throw 'Cannot create empty note.';
fetch("https://makers-emojify.herokuapp.com/", {
method: "POST",
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({text: text})
}).then((response) => {
return response.json()
}).then((json) => {
const note = new Note(json.emojified_text);
notesArray.push(note);
localStorage.setItem('notes', JSON.stringify(notesArray));
createNewDiv(note);
document.querySelector('#text-input').value = "";
document.querySelector('#text-input').placeholder = "";
})
} catch(error) {
document.getElementById('text-input').placeholder = error
}
};
document.querySelector('#create').addEventListener('click', () => {
createNoteFromText();
});
document.querySelector('#clear').addEventListener('click', () => {
localStorage.clear();
location.reload();
})
});