|
| 1 | +// targeting the parent element |
| 2 | +const blogContainer = document.querySelector('.blog__container'); |
| 3 | +const blogModal = document.querySelector(".blog__modal__body"); |
| 4 | +// global |
| 5 | +let globalStore = []; |
| 6 | + |
| 7 | +// ----------------------------------------------------- |
| 8 | +// a function for creating a new card |
| 9 | +const newCard = ({ |
| 10 | + id, |
| 11 | + imageUrl, |
| 12 | + blogTitle, |
| 13 | + blogType, |
| 14 | + blogDescription |
| 15 | +}) => `<div class="col-lg-4 col-md-6" id=${id}> |
| 16 | +<div class="card m-2"> |
| 17 | + <div class="card-header d-flex justify-content-end gap-2"> |
| 18 | + <button type="button" class="btn btn-outline-success" id="${id}" onclick="editCard.apply(this, arguments)"><i class="fas fa-pencil-alt" id="${id}" onclick="editCard.apply(this, arguments)"></i></button> |
| 19 | + <button type="button" class="btn btn-outline-danger" id="${id}" onclick="deleteCard.apply(this, arguments)"><i class="fas fa-trash-alt" id="${id}" onclick="deleteCard.apply(this, arguments)"></i></button> |
| 20 | + </div> |
| 21 | + <img |
| 22 | + src=${imageUrl} |
| 23 | + class="card-img-top" alt="..."> |
| 24 | + <div class="card-body"> |
| 25 | + <h5 class="card-title">${blogTitle}</h5> |
| 26 | + <p class="card-text">${blogDescription}</p> |
| 27 | + <span class="badge bg-primary">${blogType}</span> |
| 28 | + </div> |
| 29 | + <div class="card-footer text-muted"> |
| 30 | + <button type="button" id="${id}" class="btn btn-outline-primary float-end" data-bs-toggle="modal" |
| 31 | + data-bs-target="#showblog" onclick="openBlog.apply(this, arguments)">Open Blog</button> |
| 32 | + </div> |
| 33 | +</div> |
| 34 | +</div>`; |
| 35 | + |
| 36 | +// -------------------------------------------------- |
| 37 | +const loadData = () => { |
| 38 | + |
| 39 | + // access localstorage |
| 40 | + // localStorage.getItem("blog") === localStorage.blog |
| 41 | + const getInitialData = localStorage.blog; // if null, then |
| 42 | + if (!getInitialData) return; |
| 43 | + |
| 44 | + // convert stringified-object to object |
| 45 | + const { |
| 46 | + cards |
| 47 | + } = JSON.parse(getInitialData); |
| 48 | + |
| 49 | + // map around the array to generate HTML card and inject it to DOM |
| 50 | + cards.map((blogObject) => { |
| 51 | + const createNewBlog = newCard(blogObject); |
| 52 | + blogContainer.insertAdjacentHTML("beforeend", createNewBlog); |
| 53 | + globalStore.push(blogObject); |
| 54 | + }); |
| 55 | +}; |
| 56 | + |
| 57 | +const updateLocalStorage = () => { |
| 58 | + localStorage.setItem("blog", JSON.stringify({ |
| 59 | + cards: globalStore |
| 60 | + })) |
| 61 | +} |
| 62 | + |
| 63 | +// function for save changes---------------------------------------- |
| 64 | + |
| 65 | +// create a function which will trigerred on clicking on save changes in the modal |
| 66 | +const saveChanges = () => { |
| 67 | + const blogData = { |
| 68 | + id: `${Date.now()}`, // generating a unique id for each card |
| 69 | + imageUrl: document.getElementById('imageurl').value, |
| 70 | + blogTitle: document.getElementById('title').value, |
| 71 | + blogType: document.getElementById('type').value, |
| 72 | + blogDescription: document.getElementById('description').value |
| 73 | + }; |
| 74 | + |
| 75 | + const createNewBlog = newCard(blogData); |
| 76 | + blogContainer.insertAdjacentHTML("beforeend", createNewBlog); |
| 77 | + |
| 78 | + globalStore.push(blogData); |
| 79 | + |
| 80 | + // API -> add t localStorage |
| 81 | + updateLocalStorage() |
| 82 | + // provide some unique identification, i.e key, here key is "blog", |
| 83 | + |
| 84 | +}; |
| 85 | + |
| 86 | +// function for deleting a card ------------------- |
| 87 | + |
| 88 | +const deleteCard = (event) => { |
| 89 | + // id |
| 90 | + event = window.event; |
| 91 | + const targetID = event.target.id; |
| 92 | + const tagname = event.target.tagName; // BUTTON OR I |
| 93 | + |
| 94 | + // assign the same id of card to button also |
| 95 | + |
| 96 | + // search the globalStore, remove the object which matches with the id |
| 97 | + globalStore = globalStore.filter((blogObject) => blogObject.id !== targetID); |
| 98 | + |
| 99 | + updateLocalStorage(); |
| 100 | + |
| 101 | + // access DOM to remove them |
| 102 | + |
| 103 | + if (tagname === "BUTTON") { |
| 104 | + // task__container |
| 105 | + return blogContainer.removeChild( |
| 106 | + event.target.parentNode.parentNode.parentNode // col-lg-4 |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + // else |
| 111 | + // blog__container |
| 112 | + return blogContainer.removeChild( |
| 113 | + event.target.parentNode.parentNode.parentNode.parentNode // col-lg-4 |
| 114 | + ); |
| 115 | +}; |
| 116 | + |
| 117 | +// function for editing |
| 118 | +const editCard = (event) => { |
| 119 | + |
| 120 | + event = window.event; |
| 121 | + const targetID = event.target.id; |
| 122 | + const tagname = event.target.tagName; |
| 123 | + |
| 124 | + let parentElement; |
| 125 | + if (tagname === "BUTTON") { |
| 126 | + parentElement = event.target.parentNode.parentNode; |
| 127 | + |
| 128 | + } else { |
| 129 | + parentElement = event.target.parentNode.parentNode.parentNode; |
| 130 | + } |
| 131 | + |
| 132 | + let blogTitle = parentElement.childNodes[5].childNodes[1]; |
| 133 | + let blogDescription = parentElement.childNodes[5].childNodes[3]; |
| 134 | + let blogType = parentElement.childNodes[5].childNodes[5]; |
| 135 | + let submitBtn = parentElement.childNodes[7].childNodes[1]; |
| 136 | + // console.log(taskTitle, taskDescription, taskType); |
| 137 | + |
| 138 | + // setAttributes |
| 139 | + blogTitle.setAttribute("contenteditable", "true"); |
| 140 | + |
| 141 | + blogDescription.setAttribute("contenteditable", "true"); |
| 142 | + blogType.setAttribute("contenteditable", "true"); |
| 143 | + submitBtn.setAttribute( |
| 144 | + "onclick", |
| 145 | + "saveEditChanges.apply(this, arguments)" |
| 146 | + ); |
| 147 | + submitBtn.innerHTML = "Save Changes"; |
| 148 | + |
| 149 | + // modal removed |
| 150 | + submitBtn.removeAttribute("data-bs-toggle"); |
| 151 | + submitBtn.removeAttribute("data-bs-target"); |
| 152 | + |
| 153 | +} |
| 154 | + |
| 155 | +const saveEditChanges = (event) => { |
| 156 | + |
| 157 | + event = window.event; |
| 158 | + const targetID = event.target.id; |
| 159 | + const tagname = event.target.tagName; |
| 160 | + |
| 161 | + let parentElement; |
| 162 | + if (tagname === "BUTTON") { |
| 163 | + parentElement = event.target.parentNode.parentNode; |
| 164 | + |
| 165 | + } else { |
| 166 | + parentElement = event.target.parentNode.parentNode.parentNode; |
| 167 | + } |
| 168 | + |
| 169 | + let blogTitle = parentElement.childNodes[5].childNodes[1]; |
| 170 | + let blogDescription = parentElement.childNodes[5].childNodes[3]; |
| 171 | + let blogType = parentElement.childNodes[5].childNodes[5]; |
| 172 | + let submitBtn = parentElement.childNodes[7].childNodes[1]; |
| 173 | + |
| 174 | + const updatedData = { |
| 175 | + |
| 176 | + blogTitle: blogTitle.innerHTML, |
| 177 | + blogDescription: blogDescription.innerHTML, |
| 178 | + blogType: blogType.innerHTML, |
| 179 | + } |
| 180 | + |
| 181 | + // console.log(updatedData); |
| 182 | + globalStore = globalStore.map((blog) => { |
| 183 | + if (blog.id === targetID) { |
| 184 | + return { |
| 185 | + id: blog.id, |
| 186 | + imageUrl: blog.imageUrl, |
| 187 | + blogTitle: updatedData.blogTitle, |
| 188 | + blogType: updatedData.blogType, |
| 189 | + blogDescription: updatedData.blogDescription, |
| 190 | + }; |
| 191 | + } |
| 192 | + return blog; // important statement |
| 193 | + }); |
| 194 | + |
| 195 | + updateLocalStorage(); |
| 196 | + |
| 197 | + blogTitle.setAttribute("contenteditable", "false"); |
| 198 | + |
| 199 | + blogDescription.setAttribute("contenteditable", "false"); |
| 200 | + blogType.setAttribute("contenteditable", "false"); |
| 201 | + |
| 202 | + // modal added |
| 203 | + submitBtn.setAttribute("onclick", "openBlog.apply(this, arguments)"); |
| 204 | + submitBtn.setAttribute("data-bs-toggle", "modal"); |
| 205 | + submitBtn.setAttribute("data-bs-target", "#showblog"); |
| 206 | + |
| 207 | + submitBtn.innerHTML = "Open Blog"; |
| 208 | + |
| 209 | +} |
| 210 | + |
| 211 | +const htmlModalContent = ({ |
| 212 | + id, |
| 213 | + blogTitle, |
| 214 | + blogDescription, |
| 215 | + imageUrl, |
| 216 | + blogType |
| 217 | +}) => { |
| 218 | + const date = new Date(parseInt(id)); |
| 219 | + return ` <div id=${id}> |
| 220 | + <img |
| 221 | + src=${imageUrl} |
| 222 | + alt="bg image" |
| 223 | + class="img-fluid place__holder__image mb-3 p-4" |
| 224 | + /> |
| 225 | + <div class="text-sm text-muted ">Created on ${date.toDateString()}</div> |
| 226 | + <h2 class="my-5 mt-5" style="display:inline;">${blogTitle}</h2> |
| 227 | + <span class="badge bg-primary">${blogType}</span> |
| 228 | + <p class="lead mt-2"> |
| 229 | + ${blogDescription} |
| 230 | + </p></div>`; |
| 231 | +}; |
| 232 | + |
| 233 | +const openBlog = (event) => { |
| 234 | + |
| 235 | + event = window.event; |
| 236 | + const targetID = event.target.id; |
| 237 | + |
| 238 | + const getBlog = globalStore.filter(({ |
| 239 | + id |
| 240 | + }) => id === targetID); |
| 241 | + // console.log(getBlog[0]); |
| 242 | + blogModal.innerHTML = htmlModalContent(getBlog[0]); |
| 243 | +}; |
| 244 | + |
| 245 | + |
0 commit comments