Skip to content

Commit

Permalink
Added feature: enable localStorage usage to store the user input
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohab Yaser committed Nov 15, 2023
1 parent d0bbd24 commit f506e72
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 7 deletions.
4 changes: 4 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@
<div class="gen-btn-container">
<button class="gen-btn">Generate</button>
</div>
<div class="storage-btns-container">
<button class="storage-save">Save</button>
<button class="storage-reset">Reset</button>
</div>
<div class="problems-container"></div>
<script src="main.js"></script>
</body>
Expand Down
65 changes: 64 additions & 1 deletion main.js
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,68 @@ function dummy_data() {
// document.getElementsByClassName('problems-container')[0].innerHTML += problem_container;
}

document.getElementsByClassName('storage-reset')[0].addEventListener('click',
() => {
localStorage.clear();
});

document.getElementsByClassName('storage-save')[0].addEventListener('click',
() => {
let tags_btns = document.getElementsByClassName("tag-btn");
let tags = '';
for (let i = 0; i < tags_btns.length; i++) {
let tag = tags_btns[i];
if (tag.style.backgroundColor === green) {
tags += i.toString() + ',';
}
}

tags = tags.slice(0, -1);

let [from, to, problems_cnt] = document.getElementsByClassName("another-class");
from = from.value;
to = to.value;
problems_cnt = problems_cnt.value;

let found_handles = document.getElementsByClassName("accepted-handle tooltip");
let handles = '';

for (let i = 0; i < found_handles.length; i++) {
handles += found_handles[i].innerText + ',';
}

handles = handles.slice(0, -1);

localStorage.setItem('tags', tags);
localStorage.setItem('from', from);
localStorage.setItem('to', to);
localStorage.setItem('problems_cnt', problems_cnt);
localStorage.setItem('handles', handles);
});

function load_from_local_storage() {
let tags_btns = document.getElementsByClassName("tag-btn");
let tags = localStorage.getItem('tags').split(',');

if (tags.length > 1) {
for (let i = 0; i < tags.length; i++) {
tags_btns[+tags[i]].style.backgroundColor = green;
}
}

let [from, to, problems_cnt] = document.getElementsByClassName("another-class");
from.value = localStorage.getItem('from');
to.value = localStorage.getItem('to');
problems_cnt.value = localStorage.getItem('problems_cnt');

let handles = localStorage.getItem('handles').trim().split(',');
console.log(handles);

for (let i = 0; i < handles.length; i++) {
add_handle(handles[i]);
}
}

window.addEventListener("load", () => {
document.getElementsByClassName("tags-container")[0].innerHTML = tags();

Expand All @@ -125,6 +187,7 @@ window.addEventListener("load", () => {
});
}

load_from_local_storage();
// dummy_data();
});

Expand Down Expand Up @@ -189,7 +252,7 @@ document
handles = handles.split(',');
handles = new Set(handles);

handles.forEach(async (handle) => {
handles.forEach(async(handle) => {
handle = handle.trim();
if (handle === "")
Swal.fire({
Expand Down
46 changes: 40 additions & 6 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ body {
max-width: 800px;
min-width: 600px;
margin: auto;
/* margin-top: 100px;*/
/* margin-bottom: 100px;*/
margin-top: 50px;
margin-bottom: 50px;
padding: 8px;
}

Expand Down Expand Up @@ -237,15 +237,16 @@ input {
}

.gen-btn-container {
display: block;
text-align: center;
margin: 10px;
}

.gen-btn {
width: 150px;
width: 180px;
height: 40px;
border: 3px solid;
border-radius: 10px;
border: 0px solid;
border-radius: 7px;
font-family: monospace;
font-weight: bold;
font-size: 20px;
Expand All @@ -268,4 +269,37 @@ input {
border-radius: 4px;
width: 50px;
font-size: 15px;
}
}

.storage-btns-container {
display: block;
text-align: center;
}

.storage-save, .storage-reset {
cursor: pointer;
width: 90px;
font-family: monospace;
height: 30px;
font-size: 18px;
font-weight: bold;
border: 0px solid grey;
border-radius: 7px;
transition: 0.5s;
}

.storage-reset {
background-color: lightcoral;
}

.storage-reset:hover {
background-color: #fd4a4a;
}

.storage-save {
background-color: lightgreen;
}

.storage-save:hover {
background-color: #4fff4f;
}

0 comments on commit f506e72

Please sign in to comment.