Skip to content

Commit

Permalink
Use form validation for address picker. See #1574.
Browse files Browse the repository at this point in the history
  • Loading branch information
alexsegura committed Sep 1, 2020
1 parent 896f3ab commit b8c26f1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
3 changes: 2 additions & 1 deletion js/app/cart/components/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class Cart extends Component {
geohash={ '' }
key={ this.props.streetAddress }
onAddressSelected={ (value, address) => this.props.changeAddress(address) }
disabled={ this.props.isCollectionOnly || this.props.takeaway } />
disabled={ this.props.isCollectionOnly || this.props.takeaway }
required />
{ this.props.isCollectionEnabled && (
<div className="text-center">
<Takeaway
Expand Down
18 changes: 3 additions & 15 deletions js/app/forms/delivery.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import _ from 'lodash'
import AddressBook from '../delivery/AddressBook'
import DateTimePicker from '../widgets/DateTimePicker'
import TagsInput from '../widgets/TagsInput'
import i18n from '../i18n'
import { validateForm } from '../utils/address'

class DeliveryForm {
disable() {
Expand Down Expand Up @@ -340,21 +340,9 @@ export default function(name, options) {
const lngInput = document.querySelector(`#${name}_${type}_address [data-address-prop="longitude"]`)
const streetAddrInput = document.querySelector(`#${name}_${type}_address_newAddress_streetAddress`)

if (searchInput.validity.valid) {
if (_.isEmpty(latInput.value) || _.isEmpty(lngInput.value)
|| (searchInput.value !== streetAddrInput.value)) {
e.preventDefault();
searchInput.setCustomValidity(i18n.t('PLEASE_SELECT_ADDRESS'))
if (HTMLInputElement.prototype.reportValidity) {
searchInput.reportValidity()
}

return true
}
}

return false
const isValid = validateForm(e, searchInput, latInput, lngInput, streetAddrInput)

return !isValid
})

}, false)
Expand Down
11 changes: 11 additions & 0 deletions js/app/restaurant/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import i18n, { getCountry } from '../i18n'
import { createStoreFromPreloadedState } from '../cart/redux/store'
import { addItem, addItemWithOptions, queueAddItem } from '../cart/redux/actions'
import Cart from '../cart/components/Cart'
import { validateForm } from '../utils/address'

require('gasparesganga-jquery-loading-overlay')

Expand Down Expand Up @@ -300,6 +301,16 @@ window.initMap = function() {
}
}

$(container).closest('form').on('submit', function (e) {

const searchInput = document.querySelector('#cart input[type="search"]')
const latInput = document.querySelector('#cart_shippingAddress_latitude')
const lngInput = document.querySelector('#cart_shippingAddress_longitude')
const streetAddrInput = document.querySelector('#cart_shippingAddress_streetAddress')

validateForm(e, searchInput, latInput, lngInput, streetAddrInput)
})

const state = {
cart,
restaurant,
Expand Down
25 changes: 25 additions & 0 deletions js/app/utils/address.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import _ from 'lodash'
import i18n from '../i18n'

export const validateForm = (e, searchInput, latInput, lngInput, streetAddrInput) => {

if (!searchInput.validity.valid) {
return
}

const isValidLatLng = !_.isEmpty(latInput.value) && !_.isEmpty(lngInput.value)
const isStreetAddressTouched = searchInput.value !== streetAddrInput.value

if (isStreetAddressTouched || !isValidLatLng) {
e.preventDefault();

searchInput.setCustomValidity(i18n.t('PLEASE_SELECT_ADDRESS'))
if (HTMLInputElement.prototype.reportValidity) {
searchInput.reportValidity()
}

return false
}

return true
}

0 comments on commit b8c26f1

Please sign in to comment.