From b44f353208c14ad2912bb271063e28afc9d1d761 Mon Sep 17 00:00:00 2001 From: isUnknown Date: Tue, 4 Nov 2025 16:32:53 +0100 Subject: [PATCH] refactor: improve donation tabs code organization MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Extract logic into dedicated functions for better readability - Create handleDonationIntervalChange() as main click handler - Separate concerns: updateLinkInterval, updateAmountDisplay, switchActiveTab - Remove unnecessary comments in favor of clear function names - Update footer to link script.js instead of donation-tabs.js 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .claude/settings.local.json | 11 +++++++ assets/js/donation-tabs.js | 59 ------------------------------------- assets/js/script.js | 51 ++++++++++++++++++++++++++++++++ site/snippets/footer.php | 2 +- site/snippets/header.php | 2 ++ 5 files changed, 65 insertions(+), 60 deletions(-) create mode 100644 .claude/settings.local.json delete mode 100644 assets/js/donation-tabs.js create mode 100644 assets/js/script.js diff --git a/.claude/settings.local.json b/.claude/settings.local.json new file mode 100644 index 0000000..a48b55d --- /dev/null +++ b/.claude/settings.local.json @@ -0,0 +1,11 @@ +{ + "permissions": { + "allow": [ + "Bash(git add:*)", + "Bash(git commit:*)", + "Bash(cat:*)" + ], + "deny": [], + "ask": [] + } +} diff --git a/assets/js/donation-tabs.js b/assets/js/donation-tabs.js deleted file mode 100644 index 6325a54..0000000 --- a/assets/js/donation-tabs.js +++ /dev/null @@ -1,59 +0,0 @@ -/** - * Gestion des onglets de donation (ponctuel / mensuel) - */ -document.addEventListener('DOMContentLoaded', function() { - const tabButtons = document.querySelectorAll('.nav--tabs__btn'); - const donationLinks = document.querySelectorAll('.btn--donation[data-amount]'); - - if (tabButtons.length === 0 || donationLinks.length === 0) return; - - tabButtons.forEach(button => { - button.addEventListener('click', function() { - // Retirer la classe is-selected de tous les boutons - tabButtons.forEach(btn => btn.classList.remove('is-selected')); - - // Ajouter la classe is-selected au bouton cliqué - this.classList.add('is-selected'); - - // Récupérer l'intervalle (o = ponctuel, m = mensuel) - const interval = this.getAttribute('data-interval'); - - // Mettre à jour les liens de donation - donationLinks.forEach(link => { - const amount = link.getAttribute('data-amount'); - const currentHref = link.getAttribute('href'); - - // Remplacer l'intervalle dans l'URL - const newHref = currentHref.replace( - /default_interval=[om]/, - `default_interval=${interval}` - ); - - link.setAttribute('href', newHref); - - // Mettre à jour le texte (ajouter /mois pour mensuel) - const amountSpan = link.querySelector('.amount-value'); - const boldText = link.querySelector('.bold'); - - if (amountSpan && boldText) { - if (interval === 'm') { - boldText.innerHTML = `${amount}€/mois`; - } else { - boldText.innerHTML = `${amount}€`; - } - } - }); - - // Mettre à jour aussi le lien "Montant libre" - const freeAmountLink = document.querySelector('.btn--donation:not([data-amount])'); - if (freeAmountLink) { - const currentHref = freeAmountLink.getAttribute('href'); - const newHref = currentHref.replace( - /default_interval=[om]/, - `default_interval=${interval}` - ); - freeAmountLink.setAttribute('href', newHref); - } - }); - }); -}); diff --git a/assets/js/script.js b/assets/js/script.js new file mode 100644 index 0000000..7e121fe --- /dev/null +++ b/assets/js/script.js @@ -0,0 +1,51 @@ +function updateLinkInterval(link, interval) { + const currentHref = link.getAttribute("href"); + const newHref = currentHref.replace( + /default_interval=[om]/, + `default_interval=${interval}` + ); + link.setAttribute("href", newHref); +} + +function updateAmountDisplay(link, amount, interval) { + const boldText = link.querySelector(".bold"); + if (!boldText) return; + + const suffix = interval === "m" ? "€/mois" : "€"; + boldText.innerHTML = `${amount}${suffix}`; +} + +function switchActiveTab(clickedButton, allButtons) { + allButtons.forEach((btn) => btn.classList.remove("is-selected")); + clickedButton.classList.add("is-selected"); +} + +function handleDonationIntervalChange(interval) { + const donationLinks = document.querySelectorAll(".btn--donation[data-amount]"); + const freeAmountLink = document.querySelector(".btn--donation:not([data-amount])"); + + donationLinks.forEach((link) => { + const amount = link.getAttribute("data-amount"); + updateLinkInterval(link, interval); + updateAmountDisplay(link, amount, interval); + }); + + if (freeAmountLink) { + updateLinkInterval(freeAmountLink, interval); + } +} + +document.addEventListener("DOMContentLoaded", function () { + const tabButtons = document.querySelectorAll(".nav--tabs__btn"); + const donationLinks = document.querySelectorAll(".btn--donation[data-amount]"); + + if (tabButtons.length === 0 || donationLinks.length === 0) return; + + tabButtons.forEach((button) => { + button.addEventListener("click", function () { + const interval = this.getAttribute("data-interval"); + switchActiveTab(this, tabButtons); + handleDonationIntervalChange(interval); + }); + }); +}); diff --git a/site/snippets/footer.php b/site/snippets/footer.php index c6f66be..0e1fcd2 100644 --- a/site/snippets/footer.php +++ b/site/snippets/footer.php @@ -1,3 +1,3 @@ - + \ No newline at end of file diff --git a/site/snippets/header.php b/site/snippets/header.php index 27a8927..0658cac 100644 --- a/site/snippets/header.php +++ b/site/snippets/header.php @@ -11,6 +11,8 @@ title() ?> + +