Skip to content

Commit

Permalink
fixup! [SLWP] Add js to add message dependent on item count
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Oct 10, 2023
1 parent 7be482f commit ca639d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 38 deletions.
11 changes: 1 addition & 10 deletions templates/web/base/waste/bulky/items.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,7 @@
[% IF property %]
[% INCLUDE 'waste/_address_display.html' %]
[% END %]
[% IF cobrand.moniker == 'sutton' || cobrand.moniker == 'kingston' %]
[% SET cfg = c.cobrand.wasteworks_config %]
<div
data-base-price="[% cfg.base_price %]"
data-band1-price="[% cfg.band1_price %]"
data-band1-max="[% cfg.band1_max %]"
data-items-per-collection-max="[% cfg.items_per_collection_max %]"
id="band_pricing_data">
</div>
[% END %]

<div class="govuk-warning-text due" style="max-width:550px">
<div class="govuk-warning-text__img">
<span class="govuk-warning-text__icon" aria-hidden="true">i</span>
Expand Down
57 changes: 29 additions & 28 deletions web/js/waste.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,16 @@ $(function() {
}
}

function numberOfItems() {
var count = 0;
$('.govuk-select[name^="item_"] option:selected').each(function(i, e) {
if ($(this).val()) {
count++;
}
});
return count;
}

function updateTotal() {
var totalId = $('#js-bulky-total');
var pricing = totalId.data('pricing');
Expand All @@ -97,12 +107,7 @@ $(function() {
});
totalId.text((total / 100).toFixed(2));
} else if (pricing.strategy === 'banded') {
var count = 0;
$('.govuk-select[name^="item_"] option:selected').each(function(i, e) {
if ($(this).val()) {
count++;
}
});
var count = numberOfItems();
for (var i=0; i<pricing.bands.length; i++) {
if (count <= pricing.bands[i].max) {
total = pricing.bands[i].price;
Expand All @@ -126,32 +131,28 @@ $(function() {
}

function display_band_pricing() {
var band_pricing = $('#band_pricing_data');
var totalId = $('#js-bulky-total');
var pricing = totalId.data('pricing') || {};

if (!band_pricing) {
if (pricing.strategy !== 'banded') {
return;
}

var base_price = pricing.bands[1].price / 100;
var base_max = pricing.bands[1].max;
var band1_max = pricing.bands[0].max;
var count = numberOfItems();
var message = count + ' ' + (count === 1 ? 'item' : 'items') + ' selected - ';
message += 'you can add up to ' + (base_max - count) + ' more ' + (base_max - count === 1 ? 'item' : 'items') + '.';
if (count && count < band1_max) {
message += ' Adding another item will not increase the cost';
} else if (count && count === band1_max) {
message += ' Adding another item will increase the cost to £' + base_price.toFixed(2);
} else if (count && count < base_max) {
} else {
var base_price = band_pricing.data('base-price') / 100;
var base_max = band_pricing.data('items-per-collection-max');
var band1_max = band_pricing.data('band1-max');
var message = '';
var current_selection = $('.bulky-item-wrapper:visible').length;
if (current_selection && current_selection < band1_max) {
message = current_selection + ' ' + (current_selection === 1 ? 'item' : 'items') + ' selected - you can add up to ' +
(base_max - current_selection) + ' more items. Adding another item will not increase the cost';
} else if (current_selection && current_selection === band1_max) {
message = current_selection + ' items selected - you can add up to ' +
(base_max - current_selection) + ' more items. Adding another item will increase the cost to £' +
base_price.toFixed(2);
} else if (current_selection && (current_selection > band1_max && current_selection <= base_max)) {
message = current_selection + ' items selected - you can add up to ' +
(base_max - current_selection) + ' more items.';
}
else {
message = '';
}
$('#band-pricing-info').text(message);
message = '';
}
$('#band-pricing-info').text(message);
}

$(function() {
Expand Down

0 comments on commit ca639d5

Please sign in to comment.