Skip to content

Commit 3af2281

Browse files
committed
Blob to Base64
1 parent b1e94e5 commit 3af2281

File tree

1 file changed

+27
-20
lines changed

1 file changed

+27
-20
lines changed

TodoList/script.js

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,37 +58,44 @@ modalBtn.addEventListener('click', (e)=>{
5858
e.preventDefault();
5959

6060
const imgInput = document.getElementById('imageInput').files[0];
61-
const imgFile = URL.createObjectURL(imgInput);
6261

6362
const title = document.getElementById("title").value.trim();
6463
const description = document.getElementById("description").value.trim();
6564

66-
if(formValid(imgFile, title, description)){
67-
const id = Date.now();
65+
const reader = new FileReader();
66+
reader.onload = (e)=>{
6867

69-
addItem(id, imgFile, title, description);
68+
imgFile = e.target.result;
69+
70+
if(formValid(imgFile, title, description)){
71+
const id = Date.now();
7072

71-
let storageData = JSON.parse(localStorage.getItem("content"))
72-
if(!storageData) {
73-
storageData = []
74-
}
73+
addItem(id, imgFile, title, description);
74+
75+
let storageData = JSON.parse(localStorage.getItem("content"))
76+
if(!storageData) {
77+
storageData = []
78+
}
7579

76-
let itemData = { id , imgFile, title, description }
80+
let itemData = { id , imgFile, title, description }
7781

78-
storageData.push(itemData)
82+
storageData.push(itemData)
7983

80-
localStorage.setItem("content", JSON.stringify(storageData));
84+
localStorage.setItem("content", JSON.stringify(storageData));
8185

82-
document.getElementById("imageInput").value = "";
83-
document.getElementById("image-preview").src = "";
84-
document.querySelector('.modal__form').reset();
86+
document.getElementById("imageInput").value = "";
87+
document.getElementById("image-preview").src = "";
88+
document.querySelector('.modal__form').reset();
8589

86-
if(modalContainer.classList.contains("show-modal")){
87-
modalContainer.classList.remove('show-modal');
88-
modalContainer.classList.add('hide');
89-
}
90-
90+
if(modalContainer.classList.contains("show-modal")){
91+
modalContainer.classList.remove('show-modal');
92+
modalContainer.classList.add('hide');
93+
}
94+
95+
}
9196
}
97+
98+
reader.readAsDataURL(imgInput);
9299
})
93100

94101
function addItem(id, imgFile, title, description){
@@ -129,7 +136,7 @@ function removeItem(){
129136
let storageData = JSON.parse(localStorage.getItem("content")) || [];
130137
const itemId = item.dataset.id;
131138

132-
const index = localStorage.findIndex(item => item.id === itemId);
139+
const index = storageData.findIndex(item => item.id === itemId);
133140
console.log(index)
134141

135142
if(index !== -1){

0 commit comments

Comments
 (0)