Created
July 3, 2025 10:51
-
-
Save e-dolan/a643ae2ff8013b69b3e3de8c4889c4ea to your computer and use it in GitHub Desktop.
DL Media - Coding Task
This file contains hidden or 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
| <product-form> | |
| // rest of product form functionality | |
| <button | |
| id="btn-ATC_Button" | |
| type="submit" | |
| class="btn-Button_Primary" | |
| data-inventory="{{ product.variants.first.inventory_quantity }}" | |
| {% if product.variants.first.inventory_quantity == 0 %}disabled{% endif %}> | |
| {% if product.variants.first.inventory_quantity == 0 %} | |
| Out of Stock | |
| {% else %} | |
| Add to Cart | |
| {% endif %} | |
| </button> | |
| </product-form> | |
| <script src="{{ 'product-form.js' | asset_url }}" type="module"></script> |
This file contains hidden or 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
| class ProductForm extends HTMLElement { | |
| constructor () { | |
| super() | |
| this.atcButton = this.querySelector('#btn-ATC_Button') | |
| } | |
| connectedCallback () { | |
| if (this.atcButton) { | |
| this.updateButtonState() | |
| } | |
| } | |
| // Other product form functionality goes here | |
| updateButtonState () { | |
| const inventory = parseInt(this.atcButton.getAttribute('data-inventory'), 10) | |
| // Remove any previous stock classes | |
| this.atcButton.classList.remove('low-stock', 'in-stock', 'out-of-stock') | |
| if (inventory === 0) { | |
| this.atcButton.classList.add('out-of-stock') | |
| this.atcButton.disabled = true | |
| this.atcButton.textContent = 'Out of Stock' | |
| } else if (inventory < 10) { | |
| this.atcButton.classList.add('low-stock') | |
| this.atcButton.disabled = false | |
| this.atcButton.textContent = 'Limited Stock Remaining - Add to Cart' | |
| } else { | |
| this.atcButton.classList.add('in-stock') | |
| this.atcButton.disabled = false | |
| this.atcButton.textContent = 'Add to Cart' | |
| } | |
| } | |
| } | |
| if (!customElements.get('product-form')) { | |
| customElements.define('product-form', ProductForm) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment