|
| 1 | +const addFlashcardBtn = document.querySelector(".add-flashcard"), |
| 2 | + addFlashcardContainer = document.querySelector(".add-flashcard-container"), |
| 3 | + closeIcon = document.querySelector(".ri-close-large-line"), |
| 4 | + saveFlashCardBtn = document.querySelector(".add-flashcard-container button"), |
| 5 | + question = document.querySelector("#question"), |
| 6 | + answer = document.querySelector("#answer"), |
| 7 | + flashCardWrapper = document.querySelector(".flashcard-wrapper"), |
| 8 | + categorySelect = document.querySelector("#category-select"); |
| 9 | + |
| 10 | +let editable = false; |
| 11 | +let currentFlashCard = null; |
| 12 | + |
| 13 | +// Show flashcard container |
| 14 | +const showFlashcardContainer = ( |
| 15 | + edit = false, |
| 16 | + questionVal = "", |
| 17 | + answerVal = "", |
| 18 | + categoryVal = "GK" |
| 19 | +) => { |
| 20 | + addFlashcardContainer.querySelector("h4").textContent = edit |
| 21 | + ? "Edit Flashcard" |
| 22 | + : "Save Flashcard"; |
| 23 | + saveFlashCardBtn.textContent = edit ? "Apply Changes" : "Save"; |
| 24 | + editable = edit; |
| 25 | + |
| 26 | + // Reset fields if adding a new flashcard |
| 27 | + question.value = questionVal; |
| 28 | + answer.value = answerVal; |
| 29 | + categorySelect.value = categoryVal; |
| 30 | + |
| 31 | + addFlashcardContainer.style.display = "flex"; |
| 32 | + document.body.style.overflowY = "hidden"; |
| 33 | + question.focus(); |
| 34 | +}; |
| 35 | + |
| 36 | +// Hide the flashcard container |
| 37 | +const hideFlashcardContainer = () => { |
| 38 | + addFlashcardContainer.style.display = "none"; |
| 39 | + document.body.style.overflowY = "auto"; |
| 40 | +}; |
| 41 | + |
| 42 | +// Flashcard category styles |
| 43 | +const flashcardStyles = (category) => |
| 44 | + ({ |
| 45 | + GK: "gk", |
| 46 | + Science: "science", |
| 47 | + History: "history", |
| 48 | + }[category] || "undefined"); |
| 49 | + |
| 50 | +// Flashcard HTML structure generator |
| 51 | +const flashCardGenerator = (questionText, answerText, categoryText) => { |
| 52 | + const bgClass = flashcardStyles(categoryText); |
| 53 | + return `<div |
| 54 | + class="d-flex flex-column gap-1 flashcard-box ${bgClass} p-3 rounded-2" |
| 55 | + > |
| 56 | + <h5 class="fw-medium que-text m-0 pb-2">${questionText}</h5> |
| 57 | + <p class="m-0 pb-1 fw-medium text-secondary-emphasis ans-text">${answerText}</p> |
| 58 | + <div class="d-flex flex-column mt-auto"> |
| 59 | + <button class="btn btn-dark show-hide-btn">Show</button> |
| 60 | + <div class="d-flex justify-content-between align-items-center mt-2"> |
| 61 | + <p class="category-text">${categoryText}</p> |
| 62 | + <div class="d-flex gap-2"> |
| 63 | + <i class="ri-edit-box-line text-primary"></i> |
| 64 | + <i class="ri-delete-bin-line text-danger"></i> |
| 65 | + </div> |
| 66 | + </div> |
| 67 | + </div> |
| 68 | + </div>`; |
| 69 | +}; |
| 70 | + |
| 71 | +// Create a new flashcard |
| 72 | +const createFlashCard = () => { |
| 73 | + flashCardWrapper.insertAdjacentHTML( |
| 74 | + "beforeend", |
| 75 | + flashCardGenerator(question.value, answer.value, categorySelect.value) |
| 76 | + ); |
| 77 | + saveDataToLocalStorage(); |
| 78 | +}; |
| 79 | + |
| 80 | +// Edit an existing flashcard |
| 81 | +const editFlashCard = () => { |
| 82 | + if (currentFlashCard) { |
| 83 | + const questionText = currentFlashCard.querySelector(".que-text"); |
| 84 | + const answerText = currentFlashCard.querySelector(".ans-text"); |
| 85 | + const categoryText = currentFlashCard.querySelector(".category-text"); |
| 86 | + |
| 87 | + questionText.textContent = question.value; |
| 88 | + answerText.textContent = answer.value; |
| 89 | + categoryText.textContent = categorySelect.value; |
| 90 | + |
| 91 | + const bgClass = flashcardStyles(categorySelect.value); |
| 92 | + if (!currentFlashCard.classList.contains(bgClass)) { |
| 93 | + currentFlashCard.classList.remove( |
| 94 | + "gk", |
| 95 | + "science", |
| 96 | + "history", |
| 97 | + "undefined" |
| 98 | + ); |
| 99 | + currentFlashCard.classList.add(bgClass); |
| 100 | + } |
| 101 | + } |
| 102 | + saveDataToLocalStorage(); |
| 103 | + hideFlashcardContainer(); |
| 104 | +}; |
| 105 | + |
| 106 | +// Event listeners |
| 107 | +addFlashcardBtn.addEventListener("click", () => showFlashcardContainer(false)); |
| 108 | + |
| 109 | +closeIcon.addEventListener("click", hideFlashcardContainer); |
| 110 | + |
| 111 | +saveFlashCardBtn.addEventListener("click", () => { |
| 112 | + if (!question.value || !answer.value) { |
| 113 | + return alert("All fields is required"); |
| 114 | + } |
| 115 | + |
| 116 | + editable ? editFlashCard() : createFlashCard(); |
| 117 | + hideFlashcardContainer(); |
| 118 | +}); |
| 119 | + |
| 120 | +flashCardWrapper.addEventListener("click", (e) => { |
| 121 | + const currentBox = e.target.closest(".flashcard-box"); |
| 122 | + const currentQueText = currentBox?.querySelector(".que-text"); |
| 123 | + const currentAnsText = currentBox.querySelector(".ans-text"); |
| 124 | + const currentCategoryText = currentBox.querySelector(".category-text"); |
| 125 | + |
| 126 | + if (e.target.classList.contains("show-hide-btn")) { |
| 127 | + currentAnsText.classList.toggle("show"); |
| 128 | + e.target.textContent = currentAnsText.classList.contains("show") |
| 129 | + ? "Hide" |
| 130 | + : "Show"; |
| 131 | + } |
| 132 | + if (e.target.classList.contains("ri-delete-bin-line")) { |
| 133 | + currentBox.remove(); |
| 134 | + saveDataToLocalStorage(); |
| 135 | + } |
| 136 | + if (e.target.classList.contains("ri-edit-box-line")) { |
| 137 | + currentFlashCard = currentBox; |
| 138 | + showFlashcardContainer( |
| 139 | + true, |
| 140 | + currentQueText.textContent, |
| 141 | + currentAnsText.textContent, |
| 142 | + currentCategoryText.textContent |
| 143 | + ); |
| 144 | + } |
| 145 | +}); |
| 146 | + |
| 147 | +// Save flashcards to localStorage |
| 148 | +const saveDataToLocalStorage = () => { |
| 149 | + const flashcards = Array.from( |
| 150 | + flashCardWrapper.querySelectorAll(".flashcard-box") |
| 151 | + ).map((box) => ({ |
| 152 | + queText: box.querySelector(".que-text").textContent, |
| 153 | + ansText: box.querySelector(".ans-text").textContent, |
| 154 | + categoryText: box.querySelector(".category-text").textContent, |
| 155 | + })); |
| 156 | + localStorage.setItem("flashcards", JSON.stringify(flashcards)); |
| 157 | +}; |
| 158 | + |
| 159 | +// Load flashcards from localStorage |
| 160 | +const loadDataFromLocalStorage = () => { |
| 161 | + const storedFlashCards = JSON.parse(localStorage.getItem("flashcards")) || []; |
| 162 | + storedFlashCards.forEach(({ queText, ansText, categoryText }) => { |
| 163 | + flashCardWrapper.insertAdjacentHTML( |
| 164 | + "beforeend", |
| 165 | + flashCardGenerator(queText, ansText, categoryText) |
| 166 | + ); |
| 167 | + }); |
| 168 | +}; |
| 169 | + |
| 170 | +// Load flashcards on page loads |
| 171 | +window.addEventListener("DOMContentLoaded", loadDataFromLocalStorage); |
0 commit comments