-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
156 lines (124 loc) · 4.88 KB
/
script.js
File metadata and controls
156 lines (124 loc) · 4.88 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
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
import { getUserIds, getData, setData, clearData} from "./storage.js";
import { sortBookmarks } from "./logic.js";
//---------------------- Users dropdown ---------------------------
const userSelect = document.getElementById("user-select");
let currentUserId = "";
// load users into dropdown
function loadUsers() {
const users = getUserIds();
//creating the dropdown options
for (let i = 0; i < users.length; i++) {
const option = document.createElement("option");
option.value = users[i];
option.textContent = "User " + users[i];
userSelect.appendChild(option);
}
//for first window load = when no user is selected
currentUserId = users[0];
renderBookmarks(currentUserId);
}
window.onload = loadUsers;
//to identify which user has been selected using (change)
userSelect.addEventListener("change", function(){
currentUserId = userSelect.value;
renderBookmarks(currentUserId);
});
//---------------------- Form ---------------------------
const myForm = document.getElementById("bookmark-form");
// listen for form submit
myForm.addEventListener("submit", (e)=>{
e.preventDefault();
// get the data from user entry and creating timestamp, like button and copy button for each one
const url = document.getElementById("url").value;
const title = document.getElementById("title").value;
const description = document.getElementById("description").value;
const createdAt = new Date().toISOString(); // this for the sort
const likes = 0;
const bookmark = {
url:url,
title:title,
description:description,
createdAt: createdAt,
likes: likes,
}
addBookmark(bookmark);
myForm.reset();
renderBookmarks(currentUserId);
}
);
//---------------------- Bookmarks ---------------------------
// addBookmark() will add the new bookmark to the old ones then send them to saveBookmark()
function addBookmark (bookmark){
const currentBookmarks= getData(currentUserId) || [];
currentBookmarks.push(bookmark);
saveBookmark(currentBookmarks);
}
//save the bookmarks into local storage
function saveBookmark (data){
setData(currentUserId, data);
}
// render/display the bookmarks for selected user from the local storage
function renderBookmarks (currentUserId){
const bookmarks= getData(currentUserId);
const noBookmarkText = document.getElementById("no-bookmarks-message");
const bookmarkList = document.getElementById("bookmark-list");
bookmarkList.innerHTML= ""; // to clear the display when changing the users, so only displaying the selected user's bookmarks
if (!bookmarks || bookmarks.length === 0) {
noBookmarkText.hidden = false;
return;
}
noBookmarkText.hidden = true;
const sortedBookmarks = sortBookmarks(bookmarks);
//loop through bookmarks to create the containers for the bookmarks on web
//and create all bookmark details inside container
for (const b of sortedBookmarks){
const bookmarkcontainer = document.createElement("div");
bookmarkList.appendChild(bookmarkcontainer);
bookmarkcontainer.className="bookmark-background";
const titleLink = document.createElement("a");
titleLink.href= b.url;
titleLink.textContent= b.title;
titleLink.target= "_blank";
bookmarkcontainer.appendChild(titleLink);
const description = document.createElement("p");
description.textContent= b.description;
bookmarkcontainer.appendChild(description);
const time = document.createElement("p");
time.textContent= "Created at: "
time.textContent += new Date(b.createdAt).toLocaleString();
bookmarkcontainer.appendChild(time);
const copyButton = document.createElement("button");
copyButton.type = "button";
copyButton.textContent = "Copy to clipboard";
copyButton.addEventListener("click", function () {
navigator.clipboard.writeText(b.url).then(() => {
copyButton.textContent = "Copied!";
setTimeout(() => {
copyButton.textContent = "Copy to clipboard";
}, 1000);
});
});
bookmarkcontainer.appendChild(copyButton);
const likeButton = document.createElement("button");
likeButton.type= "button";
likeButton.textContent= "Like " + (b.likes || 0);
bookmarkcontainer.appendChild(likeButton);
likeButton.addEventListener("click", function () {
const stored = getData(currentUserId) || [];
for (let i = 0; i < stored.length; i++) {
if (stored[i].createdAt === b.createdAt) {
stored[i].likes += 1;
break;
}
}
setData(currentUserId, stored);
renderBookmarks(currentUserId);
});
}
}
// ---------- Clear all the bookmarks for selected user ------------------
const clearButton = document.getElementById("clear-data-btn");
clearButton.addEventListener("click", ()=> {
clearData(currentUserId);
renderBookmarks(currentUserId);
});