Adds class comments back.

This commit is contained in:
Llewellyn van der Merwe 2023-11-12 19:41:44 +02:00
parent 6faf139293
commit 5bbd1ed1f6
Signed by: Llewellyn
GPG Key ID: A9201372263741E7

9
dist/js/getBible.js vendored
View File

@ -6,6 +6,7 @@ class GetBibleTooltip {
this.findAndFetchScriptureReferences(); this.findAndFetchScriptureReferences();
} }
// Find elements with the 'getBible' class and fetch their references individually
findAndFetchScriptureReferences() { findAndFetchScriptureReferences() {
const elements = document.querySelectorAll('.getBible'); const elements = document.querySelectorAll('.getBible');
elements.forEach(element => { elements.forEach(element => {
@ -40,10 +41,12 @@ class GetBibleTooltip {
}); });
} }
// Function to generate a unique key for each scripture
generateStorageKey(reference, translation) { generateStorageKey(reference, translation) {
return `getBible-${translation}-${reference}`; return `getBible-${translation}-${reference}`;
} }
// Function to check local storage
async checkLocalStorage(reference, translation) { async checkLocalStorage(reference, translation) {
const key = this.generateStorageKey(reference, translation); // Corrected this line const key = this.generateStorageKey(reference, translation); // Corrected this line
const storedItem = localStorage.getItem(key); const storedItem = localStorage.getItem(key);
@ -57,6 +60,7 @@ class GetBibleTooltip {
return null; return null;
} }
// Function to store data in local storage
storeInLocalStorage(reference, translation, data) { storeInLocalStorage(reference, translation, data) {
const key = this.generateStorageKey(reference, translation); // Corrected this line const key = this.generateStorageKey(reference, translation); // Corrected this line
const item = { const item = {
@ -66,12 +70,14 @@ class GetBibleTooltip {
localStorage.setItem(key, JSON.stringify(item)); localStorage.setItem(key, JSON.stringify(item));
} }
// Dynamic fetchScripture function
async fetchScripture(reference, translation) { async fetchScripture(reference, translation) {
try { try {
const localStorageData = await this.checkLocalStorage(reference, translation); // Corrected this line const localStorageData = await this.checkLocalStorage(reference, translation); // Corrected this line
if (localStorageData !== null) { if (localStorageData !== null) {
return localStorageData; return localStorageData;
} }
// Fetch from API if not in local storage
const response = await fetch(`${this.apiEndpoint}${encodeURIComponent(translation)}/${encodeURIComponent(reference)}`); const response = await fetch(`${this.apiEndpoint}${encodeURIComponent(translation)}/${encodeURIComponent(reference)}`);
if (response.ok) { if (response.ok) {
const data = await response.json(); const data = await response.json();
@ -83,9 +89,12 @@ class GetBibleTooltip {
throw new Error(errorMessage); throw new Error(errorMessage);
} }
} catch (error) { } catch (error) {
// If the response is not JSON or another error occurs, throw the default error message
if (error instanceof SyntaxError) { if (error instanceof SyntaxError) {
// This indicates a problem with JSON parsing, meaning the response was not JSON
throw new Error('Failed to fetch scripture'); throw new Error('Failed to fetch scripture');
} else { } else {
// Re-throw the error that we constructed from the JSON response
throw error; throw error;
} }
} }