From c31865788ed93893d0c22399690d3aeb96e3d032 Mon Sep 17 00:00:00 2001 From: Maxim Lebedev Date: Thu, 14 Dec 2023 00:01:40 +0600 Subject: [PATCH] :bento: Added script --- web/static/scripts.js | 83 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 web/static/scripts.js diff --git a/web/static/scripts.js b/web/static/scripts.js new file mode 100644 index 0000000..c6b94a1 --- /dev/null +++ b/web/static/scripts.js @@ -0,0 +1,83 @@ +const amount = document.querySelector('input[name="amount" i]'); +const period = document.querySelector('select[name="period" i]'); +const inRussia = document.querySelector('input[name="inRussia" i]'); +const buttons = document.querySelectorAll('.js-button'); + +let form = { + amount: 0.00, + period: 0, + inRussia: false, +}; + +amount?.addEventListener('input', () => { + let value = +amount.value; + if (value < 0) { + value = 0; + } + + form.amount = value * 100; + + updateButtons(); +}); + +period.addEventListener('change', () => { + form.period = +period.value; + + updateButtons(); +}); + +inRussia.addEventListener('change', () => { + form.inRussia = inRussia.checked; + + updateButtons(); +}); + +async function updateButtons() { + buttons.forEach(el => { + let inAmount = (!el.dataset.amountMin || el.dataset.amountMin <= form.amount) && + (!el.dataset.amountMax || form.amount <= +el.dataset.amountMax); + let inPeriod = (!el.dataset.periodMin || +el.dataset.periodMin <= form.period) && + (!el.dataset.periodMax || form.period <= +el.dataset.periodMax); + let inRegion = Boolean(el.dataset.inRussia) == form.inRussia; + console.debug('button', el.textContext, inRegion); + + if (form.amount == 0.0 || inAmount && inPeriod && inRegion) { + el.href = el.dataset.pattern. + replace('%f', form.amount / 100). + replace('%d', form.amount). + replace('%p', getPeriodString(form.period)); + } else if (el.hasAttribute('href')) { + el.removeAttribute('href'); + } + }); +} + +function getPeriodString(period) { + switch (period) { + default: + return 'weekly'; + case 7: + return 'weekly'; + case 30: + return 'monthly'; + case 365: + return 'yearly'; + } +} + +function setup() { + let value = +amount.value; + if (value < 0) { + value = 0; + } + + form = { + amount: value * 100, + period: +period.value, + inRussia: inRussia.checked, + }; + + updateButtons(); +} + +setup();