-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Govuk js style #5855
base: master
Are you sure you want to change the base?
Govuk js style #5855
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/** | ||
* Determination | ||
* | ||
* Calculate totals for the assessment summary on case_workers/claims/<claim-id> | ||
* | ||
* | ||
* <div data-module="govuk-determination" data-apply-vat="true" data-vat-url="/vat.json" data-submitted-date="2023-06-27" data-scheme="agfs"> | ||
* <!-- Calculated values --> | ||
* <span class="js-total-exc-vat-determination">...</span> | ||
* <span class="js-vat-determination"...</span> | ||
* <span class="js-total-determination">...</span> | ||
* | ||
* <!-- Fields used for the calculation --> | ||
* <input id="claim_assessment_attributes_fees" /> | ||
* <input id="claim_assessment_attributes_expenses" /> | ||
* <input id="claim_assessment_attributes_disbursements" /> | ||
* | ||
* <!-- Field for LGFS claims --> | ||
* <input class="js-lgfs-vat-determination"id=" claim_assessment_attributes_vat_amount">£0.00</span> | ||
* </div> | ||
*/ | ||
|
||
export class Determination { | ||
/** | ||
* @param {Element} $module - HTML element to use for component | ||
*/ | ||
constructor ($module) { | ||
if (($module instanceof window.HTMLElement) && document.body.classList.contains('govuk-frontend-supported')) { | ||
this.$module = $module | ||
} | ||
} | ||
|
||
/** | ||
* Initialise component | ||
*/ | ||
init () { | ||
if (!this.$module) { return } | ||
|
||
const $module = this.$module | ||
|
||
this.$totalExclVat = $module.querySelector('.js-total-exc-vat-determination') | ||
this.$totalVat = $module.querySelector('.js-vat-determination') | ||
this.$LgfsVat = $module.querySelector('.js-lgfs-vat-determination') | ||
this.$totalInclVat = $module.querySelector('.js-total-determination') | ||
this.scheme = $module.dataset.scheme | ||
// Should this be this.applyVat? | ||
this.ajaxVat = $module.dataset.applyVat | ||
this.vatUrl = $module.dataset.vatUrl | ||
this.vatDate = $module.dataset.submittedDate | ||
|
||
const fieldIds = [ | ||
'fees', | ||
'expenses', | ||
'disbursements', | ||
'vat_amount' | ||
] | ||
|
||
this.fields = fieldIds.map(id => document.querySelector(`#claim_assessment_attributes_${id}`)).filter(field => field) | ||
this.fields.forEach(element => element.addEventListener('change', () => { | ||
this.calculateTotalRows() | ||
return true | ||
})) | ||
} | ||
|
||
calculateTotalRows () { | ||
const total = this.fields.reduce((n, field) => n + (parseFloat(field?.value) || 0.0), 0).toFixed(2) | ||
return this.applyVat(total).then(data => { | ||
this.$totalExclVat.innerHTML = data.net_amount | ||
if (this.$totalVat) { this.$totalVat.innerHTML = data.vat_amount } | ||
this.$totalInclVat.innerHTML = data.total_inc_vat | ||
}) | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can use async await here. Makes async function calculateTotalRows() {
const total = this.fields.reduce((n, field) => n + (parseFloat(field?.value) || 0.0), 0).toFixed(2);
const { net_amount, vat_amount, total_inc_vat } = await this.applyVat(total);
this.$totalExclVat.innerHTML = net_amount;
if (this.$totalVat) { this.$totalVat.innerHTML = vat_amount; }
this.$totalInclVat.innerHTML = total_inc_vat;
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tried making this into an async function and now SonarCloud is flagging a bug at the point at which it is being called: "Promise returned in function argument where a void return was expected." I remember when I tried this first I was getting similar issues and couldn't work out how to resolve it so I opted for it not being async. Is there a way to fix this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Try adding |
||
async applyVat (netAmount) { | ||
const params = new URLSearchParams({ | ||
scheme: this.scheme, | ||
lgfs_vat_amount: this.$LgfsVat?.value, | ||
date: this.vatDate, | ||
apply_vat: this.ajaxVat, | ||
net_amount: netAmount | ||
}) | ||
const response = await fetch(this.vatUrl + '?' + params.toString()) | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a suggestion but for reference, we can also remove string concatenation by using backtick. const response = await fetch(`${this.vatUrl}?${queryString}`); |
||
return await response.json() | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can try