-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Determination javascript using Gov.UK recommended style
... and without jQuery.
- Loading branch information
Showing
7 changed files
with
112 additions
and
119 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
115 changes: 0 additions & 115 deletions
115
app/webpack/javascripts/modules/case_worker/claims/DeterminationCalculator.js
This file was deleted.
Oops, something went wrong.
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,78 @@ | ||
/** | ||
* Component name | ||
*/ | ||
export class Determination { | ||
/** | ||
* @param {Element} $module - HTML element to use for component | ||
*/ | ||
constructor ($module) { | ||
if (!($module instanceof HTMLElement) || !document.body.classList.contains('govuk-frontend-supported')) { | ||
return this | ||
} | ||
|
||
// this.$module = $module | ||
|
||
// Code goes here | ||
} | ||
|
||
/** | ||
* Initialise component | ||
*/ | ||
init () { | ||
// Check that required elements are present | ||
if (!this.$module) { | ||
return | ||
} | ||
|
||
// Code goes here | ||
const $module = this.$module | ||
|
||
this.$totalExclVat = document.getElementsByClassName('js-total-exc-vat-determination')[0] | ||
this.$totalVat = $module.getElementsByClassName('js-vat-determination')[0] | ||
this.$LgfsVat = $module.getElementsByClassName('js-lgfs-vat-determination')[0] | ||
this.$totalInclVat = $module.getElementsByClassName('js-total-determination')[0] | ||
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 | ||
|
||
this.fields = [ | ||
document.getElementById('claim_assessment_attributes_fees'), | ||
document.getElementById('claim_assessment_attributes_expenses'), | ||
document.getElementById('claim_assessment_attributes_disbursements'), | ||
document.getElementById('claim_assessment_attributes_vat_amount') | ||
].filter(field => field) | ||
this.fields.forEach(element => element.addEventListener('change', () => this.calculateTotalRows())) | ||
} | ||
|
||
async calculateTotalRows () { | ||
const total = this.fields.reduce((n, field) => n + this.parsedValue(field), 0).toFixed(2) | ||
const data = await this.applyVat(total) | ||
this.$totalExclVat.innerHTML = data.net_amount | ||
if (this.$totalVat) { this.$totalVat.innerHTML = data.vat_amount } | ||
this.$totalInclVat.innerHTML = data.total_inc_vat | ||
} | ||
|
||
parsedValue (field) { | ||
const value = parseFloat(field?.value) | ||
if (value) { | ||
return Math.max(0.0, value) | ||
} else { | ||
return 0.0 | ||
} | ||
} | ||
|
||
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()) | ||
|
||
return await response.json() | ||
} | ||
} |
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
import '../stylesheets/govuk-frontend.scss' | ||
|
||
import { Determination } from '../javascripts/modules/determination.js' | ||
|
||
// fonts & images | ||
require.context('govuk-frontend/govuk/assets', true) | ||
|
||
const determinations = document.querySelectorAll('[data-module="govuk-determination"]') | ||
determinations.forEach((determination) => { | ||
new Determination(determination).init() | ||
}) | ||
|
||
require('govuk-frontend').initAll() |
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