Skip to content

Commit

Permalink
Merge pull request #13 from TetiZ/Exercises
Browse files Browse the repository at this point in the history
Update api.js add function for quote of the day
  • Loading branch information
TetiZ authored Jan 24, 2024
2 parents 47b5eb6 + f02a4d3 commit c125e1f
Showing 1 changed file with 44 additions and 1 deletion.
45 changes: 44 additions & 1 deletion src/js/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,47 @@ export async function equipment(event) {
}
}

// equipment------------------------------------------------------------
// equipment------------------------------------------------------------


// Функція для отримання цитати з backend'а-----------------------------
async function getQuoteFromBackend() {
try {
const response = await fetch('https://energyflow.b.goit.study/api/quote');
const data = await response.json();
return data;
} catch (error) {
console.error('Error getting quote from backend', error);
return null;
}
}

// Функція для оновлення блоку з цитатою--------------------------------
export async function updateQuoteBlock() {

const storedQuote = localStorage.getItem('quote');
const storedDate = localStorage.getItem('quoteDate');
const currentDate = new Date().toDateString();

if (storedQuote && storedDate === currentDate) {

return JSON.parse(storedQuote);
} else {

const quoteData = await getQuoteFromBackend();

if (quoteData) {

localStorage.setItem('quote', JSON.stringify(quoteData));
localStorage.setItem('quoteDate', currentDate);
return quoteData;
}
}
}

// ось так виглядає імпорт функції після then уже робите потрібні вам операції замість console.log()

// import { updateQuoteBlock } from 'шлях до api файла'

// updateQuoteBlock()
// .then(result => console.log(result));

0 comments on commit c125e1f

Please sign in to comment.