-
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
4 changed files
with
105 additions
and
15 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// Сгенерено нейросеткой, куча косячностей | ||
|
||
// Функция для обновления значения счетчика | ||
function updateCounter(linkNumber, responseData) { | ||
const count = responseData.count || 0; // Если параметр "count" отсутствует, установить значение по умолчанию 0 | ||
// Найти блок счетчика по id и обновить его содержимое | ||
const counterElement = document.getElementById(`piece-${linkNumber}-counter`); | ||
if (counterElement) { | ||
counterElement.innerText = `${count}`; | ||
} | ||
} | ||
|
||
// Функция для обработки клика по ссылке | ||
function handleLinkClick(linkNumber) { | ||
const url = 'https://api.counterapi.dev/v1/vyatkin-test2/piece-' + linkNumber + '/up'; | ||
fetch(url, { method: 'GET' }) | ||
.then(response => response.json()) | ||
.then(data => { | ||
const count = data.count || 0; // Если параметр "count" отсутствует, установить значение по умолчанию 0 | ||
// Обновить значение счетчика после успешного запроса | ||
updateCounter(linkNumber, { count }); | ||
}) | ||
.catch(error => console.error('Error updating counter:', error)); | ||
} | ||
|
||
|
||
// Инициализация значений счетчиков после загрузки DOM дерева | ||
document.addEventListener('DOMContentLoaded', function () { | ||
// Автоматическое определение номеров ссылок на основе имеющихся элементов на странице | ||
const linkButtons = document.querySelectorAll('.piece-card__button'); | ||
const linkNumbers = []; | ||
|
||
linkButtons.forEach(button => { | ||
const linkNumberMatch = button.id.match(/piece-(\d+)-button/); | ||
if (linkNumberMatch) { | ||
const linkNumber = linkNumberMatch[1]; | ||
linkNumbers.push(linkNumber); | ||
} | ||
}); | ||
|
||
// Повешение обработчика событий клика на каждую ссылку | ||
linkButtons.forEach(button => { | ||
const linkNumberMatch = button.id.match(/piece-(\d+)-button/); | ||
if (linkNumberMatch) { | ||
const linkNumber = linkNumberMatch[1]; | ||
button.addEventListener('click', () => handleLinkClick(linkNumber)); | ||
} | ||
}); | ||
|
||
// Загрузите начальные значения счетчиков с паузой в 150 мс между запросами | ||
linkNumbers.reduce((chain, linkNumber) => { | ||
return chain.then(() => { | ||
return new Promise(resolve => { | ||
setTimeout(() => { | ||
const url = `https://api.counterapi.dev/v1/vyatkin-test2/piece-${linkNumber}/`; | ||
fetch(url) | ||
.then(response => { | ||
if (response.ok) { | ||
return response.json(); | ||
} else if (response.status === 400 || response.status === 404) { | ||
// В случае "400 Bad request", отправляем команду повышения значения на 1 | ||
return fetch(`https://api.counterapi.dev/v1/vyatkin-test2/piece-${linkNumber}/up`, { method: 'GET' }) | ||
.then(response => response.json()); | ||
} else { | ||
throw new Error('Failed to fetch initial counter value'); | ||
} | ||
}) | ||
.then(data => { | ||
// Обновить значение счетчика после успешной загрузки | ||
updateCounter(linkNumber, data); | ||
resolve(); | ||
}) | ||
.catch(error => { | ||
console.error('Error fetching initial counter value:', error); | ||
resolve(); | ||
}); | ||
}, 50); | ||
}); | ||
}); | ||
}, Promise.resolve()); | ||
}); |
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