-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from Vendic/bugfix/fix-js-errors
Seperate JS from component to fix alpine errors
- Loading branch information
Showing
3 changed files
with
139 additions
and
122 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
123 changes: 123 additions & 0 deletions
123
view/frontend/templates/vat-id/validation-status-js.phtml
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,123 @@ | ||
<?php declare(strict_types=1); | ||
|
||
/** @var \Magento\Framework\Escaper $escaper */ | ||
/** @var \Magento\Framework\View\Element\Template $block */ | ||
?> | ||
<script> | ||
document.addEventListener('alpine:init', () => { | ||
Alpine.data('initVatButton', () => { | ||
return { | ||
autoSaveTime: '', | ||
vatNumber: '', | ||
countryId: '', | ||
requestMessage: '', | ||
vatIsValid: false, | ||
showFrontendMessage: false, | ||
|
||
get isVatNumberFilled() { | ||
return this.vatNumber.length > 0; | ||
}, | ||
|
||
/** | ||
* Get the country code from the VAT number | ||
* @returns {string} | ||
*/ | ||
get countryCodeFromVatNumber() { | ||
if (this.vatNumber.match(new RegExp('^[A-Z][A-Z]'))) { | ||
return this.vatNumber.substr(0, 2).toUpperCase(); | ||
} | ||
return ''; | ||
}, | ||
|
||
/** | ||
* Reset the validation | ||
* @returns {void} | ||
*/ | ||
resetValidation() { | ||
this.showFrontendMessage = false; | ||
this.requestMessage = ''; | ||
this.vatIsValid = false; | ||
this.refreshPrices(); | ||
}, | ||
|
||
/** | ||
* Close frontend message with timout. | ||
* @param timeout | ||
* @returns {void} | ||
*/ | ||
closeMessage(timeout = 10000) { | ||
setTimeout(() => { | ||
this.showFrontendMessage = false; | ||
this.requestMessage = ''; | ||
}, timeout); | ||
}, | ||
|
||
showInvalidCountryMessage() { | ||
this.vatIsValid = false; | ||
this.requestMessage = '<?= $escaper->escapeJs( | ||
__('The country code from the VAT number does not match the selected country.')->render() | ||
) ?>'; | ||
this.showFrontendMessage = true; | ||
this.refreshPrices(); | ||
}, | ||
|
||
refreshPrices() { | ||
//Trigger update order total summary | ||
setTimeout(() => { | ||
Magewire.emitTo('price-summary.total-segments', 'refresh'); | ||
}, this.autoSaveTime); | ||
}, | ||
|
||
validate(vatNumber) { | ||
this.vatNumber = vatNumber; | ||
this.showFrontendMessage = false; | ||
|
||
if (!this.isVatNumberFilled) { | ||
return; | ||
} | ||
|
||
if (this.countryCodeFromVatNumber !== this.countryId) { | ||
this.showInvalidCountryMessage(); | ||
return; | ||
} | ||
|
||
console.debug(`Start VAT validation ${this.vatNumber} for country ${this.countryId}`); | ||
|
||
const requestUrl = `${BASE_URL}euvat/vatnumber/validation`; | ||
const params = new URLSearchParams({ | ||
form_key: hyva.getFormKey(), | ||
vat_number: this.vatNumber, | ||
handle: 'hyva_front' | ||
}); | ||
|
||
fetch(requestUrl + '?' + params, { | ||
headers: {'X-Requested-With': 'XMLHttpRequest'}, | ||
method: 'GET', | ||
mode: 'cors', | ||
credentials: 'include' | ||
}) | ||
.then(response => { | ||
if (!response.ok) console.warn('GET request failed'); | ||
return response.json(); | ||
}) | ||
.then(data => { | ||
console.debug(data); | ||
if (data.request_message !== undefined) { | ||
this.requestMessage = data.request_message; | ||
} | ||
if (data.vat_is_valid !== undefined) { | ||
this.vatIsValid = data.vat_is_valid; | ||
} | ||
}) | ||
.catch(error => { | ||
console.error('Error:', error); | ||
}) | ||
.finally(() => { | ||
this.showFrontendMessage = true; | ||
this.refreshPrices(); | ||
}); | ||
} | ||
}; | ||
}); | ||
}); | ||
</script> |
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