|
| 1 | +import $ from 'jquery'; |
| 2 | +import { productsService } from '../../services/productsService'; |
| 3 | +import { productsFormValidation } from './validation/formProductValidationService'; |
| 4 | + |
| 5 | +let storedProducts = []; |
| 6 | +let $selBrands, $selProducts, $formProducts, $txtNif, $txtDiscount; |
| 7 | + |
| 8 | +class App { |
| 9 | + constructor() { |
| 10 | + this.brands = []; |
| 11 | + |
| 12 | + this.onBrandSelect = this.onBrandSelect.bind(this); |
| 13 | + this.onFieldChange = this.onFieldChange.bind(this); |
| 14 | + this.onSubmit = this.onSubmit.bind(this); |
| 15 | + |
| 16 | + $(this.init.bind(this)); |
| 17 | + } |
| 18 | + |
| 19 | + init() { |
| 20 | + this.loadBrands(); |
| 21 | + this.initializeSelectors(); |
| 22 | + this.setEventHandlers(); |
| 23 | + } |
| 24 | + |
| 25 | + initializeSelectors() { |
| 26 | + $selBrands = $('#selBrands'); |
| 27 | + $selProducts = $('#selProducts'); |
| 28 | + $txtNif = $('#txtNif'); |
| 29 | + $txtDiscount = $('#txtDiscount'); |
| 30 | + $formProducts = $('#formProducts'); |
| 31 | + } |
| 32 | + |
| 33 | + loadBrands() { |
| 34 | + productsService.fetchBrands() |
| 35 | + .then(fetchedProducts => { |
| 36 | + this.brands = fetchedProducts; |
| 37 | + this.loadSelect($selBrands, this.brands); |
| 38 | + }); |
| 39 | + } |
| 40 | + |
| 41 | + loadSelect($select, items = []) { |
| 42 | + $select.children().not(':first').remove(); |
| 43 | + const options = items |
| 44 | + .map(item => `<option value="${item.id}">${item.description}</option>`) |
| 45 | + .reduce((opts, option) => { |
| 46 | + return `${opts}${option}`; |
| 47 | + }, ''); |
| 48 | + $select.append(options); |
| 49 | + } |
| 50 | + |
| 51 | + setEventHandlers() { |
| 52 | + $selBrands.change(this.onBrandSelect); |
| 53 | + $txtNif.keyup(this.onFieldChange); |
| 54 | + $txtDiscount.keyup(this.onFieldChange); |
| 55 | + $txtDiscount.on('input', this.onDiscountType); |
| 56 | + $formProducts.submit(this.onSubmit); |
| 57 | + } |
| 58 | + |
| 59 | + onBrandSelect(event) { |
| 60 | + this.onFieldChange(event); |
| 61 | + const product = Number(event.currentTarget.value) || null; |
| 62 | + const isValidProduct = (product !== null); |
| 63 | + if (!isValidProduct) { |
| 64 | + $selProducts.val(''); |
| 65 | + } |
| 66 | + this.toggleAvailability($selProducts, isValidProduct); |
| 67 | + this.loadProductsByBrand(product); |
| 68 | + } |
| 69 | + |
| 70 | + toggleAvailability($element, isEnabled) { |
| 71 | + if (isEnabled) { |
| 72 | + $element.removeAttr('disabled'); |
| 73 | + } else { |
| 74 | + $element.attr('disabled', 'disabled'); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + loadProductsByBrand(brand) { |
| 79 | + if (brand !== null) { |
| 80 | + const products = this.getProductsByBrand(brand); |
| 81 | + this.loadSelect($selProducts, products); |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + getProductsByBrand(id) { |
| 86 | + const brand = this.brands.filter(brand => brand.id === id); |
| 87 | + return brand[0] && brand[0].products; |
| 88 | + } |
| 89 | + |
| 90 | + onSubmit(event) { |
| 91 | + event.preventDefault(); |
| 92 | + const $form = $(event.currentTarget); |
| 93 | + const vm = this.getModel($form); |
| 94 | + productsFormValidation |
| 95 | + .validateForm(vm) |
| 96 | + .then(validationResult => { |
| 97 | + validationResult.fieldErrors.forEach(this.handleFieldValidationResult($form)); |
| 98 | + if (validationResult.succeeded) { |
| 99 | + console.log('Form is sent'); |
| 100 | + } |
| 101 | + }) |
| 102 | + .catch(error => { |
| 103 | + console.log('Error validating form', error); |
| 104 | + }); |
| 105 | + } |
| 106 | + |
| 107 | + getModel($form) { |
| 108 | + return $form |
| 109 | + .serializeArray() |
| 110 | + |
| 111 | + // from { name: <prop>, value: <val> } to [<prop>, <val>] } |
| 112 | + .map(field => ({ [field.name]: field.value })) |
| 113 | + |
| 114 | + // reduce all in an object |
| 115 | + .reduce((vm, field) => ({ ...vm, ...field }), {}); |
| 116 | + } |
| 117 | + |
| 118 | + handleFieldValidationResult($form) { |
| 119 | + return (fieldValidationResult) => { |
| 120 | + const field = $form.get(0)[fieldValidationResult.key]; |
| 121 | + this.toggleErrorMessage(fieldValidationResult, $(field)); |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + onFieldChange(event) { |
| 126 | + const $field = $(event.currentTarget); |
| 127 | + productsFormValidation |
| 128 | + .validateField(null, $field.attr('name'), $field.val()) |
| 129 | + .then(validationResult => { |
| 130 | + this.toggleErrorMessage(validationResult, $field); |
| 131 | + }) |
| 132 | + .catch(error => { |
| 133 | + console.log('Error validating field', error); |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + toggleErrorMessage(validationResult, $element) { |
| 138 | + const $parent = $element.closest('div.form-group'); |
| 139 | + if (validationResult.succeeded) { |
| 140 | + $parent.removeClass('has-error').find('span.help-block').html(''); |
| 141 | + } else { |
| 142 | + $parent.addClass('has-error').find('span.help-block').html(validationResult.errorMessage); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + onDiscountType(event) { |
| 147 | + const $element = $(event.currentTarget); |
| 148 | + $element.val($element.val().toUpperCase()); |
| 149 | + } |
| 150 | +} |
| 151 | + |
| 152 | +export { |
| 153 | + App |
| 154 | +}; |
0 commit comments