-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
222 additions
and
31 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
const category = 'inspirational'; | ||
const apiKey = '0kYRIiEg1U21YoGtDo5eQQ==5UQuSZMfQwaOH6sT'; | ||
const url = `https://api.api-ninjas.com/v1/quotes?category=${category}`; | ||
const cacheKey = 'quotesCache'; | ||
const cacheExpiryKey = 'quotesCacheExpiry'; | ||
const oneDay = 24 * 60 * 60 * 1000; // One day in milliseconds | ||
|
||
function fetchQuotes() { | ||
fetch(url, { | ||
method: 'GET', | ||
headers: { | ||
'X-Api-Key': apiKey, | ||
'Content-Type': 'application/json' | ||
} | ||
}) | ||
.then(response => { | ||
if (!response.ok) { | ||
return response.text().then(text => { throw new Error(text) }); | ||
} | ||
return response.json(); | ||
}) | ||
.then(result => { | ||
// Save the result to localStorage | ||
localStorage.setItem(cacheKey, JSON.stringify(result)); | ||
// Save the current timestamp to localStorage | ||
localStorage.setItem(cacheExpiryKey, Date.now().toString()); | ||
}) | ||
.catch(error => { | ||
console.error('Error:', error); | ||
}); | ||
} | ||
|
||
|
||
function getCachedQuotes() { | ||
const cachedData = localStorage.getItem(cacheKey); | ||
const cacheExpiry = localStorage.getItem(cacheExpiryKey); | ||
|
||
if (cachedData && cacheExpiry) { | ||
const expiryTime = parseInt(cacheExpiry, 10); | ||
if (Date.now() - expiryTime < oneDay) { | ||
// Cache is still valid | ||
console.log('Using cached data:', JSON.parse(cachedData)); | ||
document.getElementById('text').innerHTML = JSON.parse(cachedData)[0].quote; | ||
document.getElementById('author').innerHTML = JSON.parse(cachedData)[0].author; | ||
return; | ||
} | ||
} | ||
|
||
// Cache is expired or doesn't exist, fetch new data | ||
fetchQuotes(); | ||
|
||
cachedData = localStorage.getItem(cacheKey); | ||
console.log('Using cached data:', JSON.parse(cachedData)); | ||
document.getElementById('text').innerHTML = JSON.parse(cachedData)[0].quote; | ||
} | ||
|
||
// Call the function to get quotes | ||
getCachedQuotes(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters