fix for delivery destination not validating
This commit is contained in:
@@ -22,256 +22,257 @@ _itemsForDisplay.setFromCart(true);
|
||||
var _deliveryDestination = new DeliveryDestination();
|
||||
var _deliveryDestinationErrors = {};
|
||||
var _deliveryCosts = new Place({
|
||||
postalCode: _deliveryDestination.get('place')
|
||||
postalCode: _deliveryDestination.get('place')
|
||||
})
|
||||
|
||||
var _cartDataLoadCalled = false;
|
||||
|
||||
var loadCart = function() {
|
||||
|
||||
_itemsInCart.fetch({
|
||||
_itemsInCart.fetch({
|
||||
success: function() {
|
||||
states = {}
|
||||
for (var i = 0; i < _itemsInCart.models.length; i++) {
|
||||
var itemInCart = _itemsInCart.models[i];
|
||||
states[itemInCart.get('item_id')] = itemInCart;
|
||||
}
|
||||
_itemsForDisplay.fetch({
|
||||
success: function() {
|
||||
states = {}
|
||||
for (var i = 0; i < _itemsInCart.models.length; i++) {
|
||||
var itemInCart = _itemsInCart.models[i];
|
||||
states[itemInCart.get('item_id')] = itemInCart;
|
||||
}
|
||||
_itemsForDisplay.fetch({
|
||||
success: function() {
|
||||
CartActions.dataLoaded();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
CartActions.dataLoaded();
|
||||
|
||||
//_deliveryDestination.fetch({
|
||||
//success: function() {
|
||||
//validateDeliveryDestinationForm();
|
||||
//fetchPlace();
|
||||
//CartActions.dataLoaded();
|
||||
//}
|
||||
//});
|
||||
_cartDataLoadCalled = true;
|
||||
_deliveryDestination.fetch({
|
||||
success: function() {
|
||||
validateDeliveryDestinationForm();
|
||||
fetchPlace();
|
||||
CartActions.dataLoaded();
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
_cartDataLoadCalled = true;
|
||||
};
|
||||
|
||||
|
||||
var fetchPlace = function() {
|
||||
_deliveryCosts = new Place({
|
||||
postalCode: _deliveryDestination.get('place')
|
||||
})
|
||||
_deliveryCosts.fetch({
|
||||
success: function() {
|
||||
CartActions.dataLoaded();
|
||||
}
|
||||
})
|
||||
_deliveryCosts = new Place({
|
||||
postalCode: _deliveryDestination.get('place')
|
||||
})
|
||||
_deliveryCosts.fetch({
|
||||
success: function() {
|
||||
CartActions.dataLoaded();
|
||||
}
|
||||
})
|
||||
|
||||
}
|
||||
|
||||
|
||||
var saveCartStateForItem = function(itemId) {
|
||||
var item = CartStore.getStateFor(itemId);
|
||||
item.save({
|
||||
success: function() {
|
||||
loadCart();
|
||||
}
|
||||
});
|
||||
var item = CartStore.getStateFor(itemId);
|
||||
item.save({
|
||||
success: function() {
|
||||
loadCart();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
/* need it for delete - will delete it later
|
||||
*/
|
||||
*/
|
||||
var takeItemOut = function(itemId) {
|
||||
|
||||
var state = states[itemId] || new ItemInCart({
|
||||
item_id: itemId,
|
||||
count: 0
|
||||
})
|
||||
if (state.get('count') > 0) {
|
||||
// state.set('count', state.get('count') - 1);
|
||||
state.set('count', 0);
|
||||
}
|
||||
var state = states[itemId] || new ItemInCart({
|
||||
item_id: itemId,
|
||||
count: 0
|
||||
})
|
||||
if (state.get('count') > 0) {
|
||||
// state.set('count', state.get('count') - 1);
|
||||
state.set('count', 0);
|
||||
}
|
||||
|
||||
states[itemId] = state;
|
||||
saveCartStateForItem(itemId);
|
||||
states[itemId] = state;
|
||||
saveCartStateForItem(itemId);
|
||||
};
|
||||
|
||||
var setItemCount = function(itemId, count) {
|
||||
var state = states[itemId] || new ItemInCart({
|
||||
item_id: itemId,
|
||||
count: 0
|
||||
});
|
||||
var state = states[itemId] || new ItemInCart({
|
||||
item_id: itemId,
|
||||
count: 0
|
||||
});
|
||||
|
||||
if (count === "") {
|
||||
state.set('count', "");
|
||||
CartStore.emitChange();
|
||||
return;
|
||||
}
|
||||
if (count === "") {
|
||||
state.set('count', "");
|
||||
CartStore.emitChange();
|
||||
return;
|
||||
}
|
||||
|
||||
var cnt = parseInt(count);
|
||||
var cnt = parseInt(count);
|
||||
|
||||
if(isNaN(cnt) || cnt <= 0) {
|
||||
cnt = 1;
|
||||
}
|
||||
if (isNaN(cnt) || cnt <= 0) {
|
||||
cnt = 1;
|
||||
}
|
||||
|
||||
state.set('count', cnt);
|
||||
state.set('count', cnt);
|
||||
|
||||
states[itemId] = state;
|
||||
saveCartStateForItem(itemId);
|
||||
states[itemId] = state;
|
||||
saveCartStateForItem(itemId);
|
||||
// CartStore.emitChange();
|
||||
};
|
||||
|
||||
var addNItems = function(item, count) {
|
||||
|
||||
var itemId = item.get('id');
|
||||
var state = states[itemId] || new ItemInCart({
|
||||
item_id: itemId,
|
||||
count: 0
|
||||
})
|
||||
var itemId = item.get('id');
|
||||
var state = states[itemId] || new ItemInCart({
|
||||
item_id: itemId,
|
||||
count: 0
|
||||
})
|
||||
|
||||
_itemsForDisplay.add(item);
|
||||
_itemsForDisplay.add(item);
|
||||
|
||||
state.set('count', state.get('count') + count);
|
||||
state.set('count', state.get('count') + count);
|
||||
|
||||
states[itemId] = state;
|
||||
saveCartStateForItem(itemId);
|
||||
states[itemId] = state;
|
||||
saveCartStateForItem(itemId);
|
||||
}
|
||||
|
||||
var changeDeliveryDestinationProperty = function(property, value) {
|
||||
_deliveryDestination.set(property, value);
|
||||
_deliveryDestination.set(property, value);
|
||||
|
||||
if (property === 'place') {
|
||||
fetchPlace();
|
||||
}
|
||||
validateDeliveryDestinationForm();
|
||||
if (property === 'place') {
|
||||
fetchPlace();
|
||||
}
|
||||
validateDeliveryDestinationForm();
|
||||
};
|
||||
|
||||
|
||||
var confirmOrder = function() {
|
||||
|
||||
var oc = new OrderConfirmation({
|
||||
hamo: 'meho'
|
||||
});
|
||||
oc.save({
|
||||
b: 'b'
|
||||
}, {
|
||||
success: function() {
|
||||
var oc = new OrderConfirmation({
|
||||
hamo: 'meho'
|
||||
});
|
||||
oc.save({
|
||||
b: 'b'
|
||||
}, {
|
||||
success: function() {
|
||||
|
||||
NavigationActions.goToThankYou();
|
||||
loadCart();
|
||||
}
|
||||
});
|
||||
NavigationActions.goToThankYou();
|
||||
loadCart();
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
var saveDeliveryDestination = function() {
|
||||
_deliveryDestination.save(null, {
|
||||
success: function() {
|
||||
_deliveryDestination.save(null, {
|
||||
success: function() {
|
||||
|
||||
confirmOrder();
|
||||
}
|
||||
})
|
||||
confirmOrder();
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
var validateDeliveryDestinationForm = function() {
|
||||
_deliveryDestinationErrors = {};
|
||||
_deliveryDestinationErrors = {};
|
||||
|
||||
var nameRegex = /.+\s+.+/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('name')).search(nameRegex) < 0) {
|
||||
_deliveryDestinationErrors['name'] = "I prezime i ime su obavezni";
|
||||
}
|
||||
var nameRegex = /.+\s+.+/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('name')).search(nameRegex) < 0) {
|
||||
_deliveryDestinationErrors['name'] = "I prezime i ime su obavezni";
|
||||
}
|
||||
|
||||
var addressRegex = /.+\s+.+/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('address')).search(addressRegex) < 0) {
|
||||
_deliveryDestinationErrors['address'] = "Adresa mora biti ispravna";
|
||||
}
|
||||
var addressRegex = /.+\s+.+/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('address')).search(addressRegex) < 0) {
|
||||
_deliveryDestinationErrors['address'] = "Adresa mora biti ispravna";
|
||||
}
|
||||
|
||||
var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('email')).search(emailRegex) < 0) {
|
||||
_deliveryDestinationErrors['email'] = "Email mora biti ispravno upisan";
|
||||
}
|
||||
var emailRegex = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,6}$/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('email')).search(emailRegex) < 0) {
|
||||
_deliveryDestinationErrors['email'] = "Email mora biti ispravno upisan";
|
||||
}
|
||||
|
||||
var phoneRegex = /[\d\s-]{8,12}/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('phone')).search(phoneRegex) < 0) {
|
||||
_deliveryDestinationErrors['phone'] = "Telefon mora biti ispravan";
|
||||
}
|
||||
var phoneRegex = /[\d\s-]{8,12}/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('phone')).search(phoneRegex) < 0) {
|
||||
_deliveryDestinationErrors['phone'] = "Telefon mora biti ispravan";
|
||||
}
|
||||
|
||||
var placeRegex = /^\s{0,1}\d{5}$/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('place')).search(placeRegex) < 0){
|
||||
_deliveryDestinationErrors['place'] = "Mjesto mora biti izabrano";
|
||||
}
|
||||
var placeRegex = /^\s{0,1}\d{5}$/i;
|
||||
if (Validation.safeString(_deliveryDestination.get('place')).search(placeRegex) < 0) {
|
||||
_deliveryDestinationErrors['place'] = "Mjesto mora biti izabrano";
|
||||
}
|
||||
|
||||
var requiredFields = ["name", "email", "place", 'address', 'phone'];
|
||||
for (var i in requiredFields) {
|
||||
var value = _deliveryDestination.get(requiredFields[i]);
|
||||
if (value === undefined || value === null || value === "") {
|
||||
// if it's required there will be a star there
|
||||
_deliveryDestinationErrors[requiredFields[i]] = "*";
|
||||
}
|
||||
var requiredFields = ["name", "email", "place", 'address', 'phone'];
|
||||
for (var i in requiredFields) {
|
||||
var value = _deliveryDestination.get(requiredFields[i]);
|
||||
if (value === undefined || value === null || value === "") {
|
||||
// if it's required there will be a star there
|
||||
_deliveryDestinationErrors[requiredFields[i]] = "*";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
var isDeliveryDestinationValid = function() {
|
||||
return Object.getOwnPropertyNames(_deliveryDestinationErrors).length === 0;
|
||||
}
|
||||
// Extend CartStore with EventEmitter to add eventing capabilities
|
||||
return Object.getOwnPropertyNames(_deliveryDestinationErrors).length === 0;
|
||||
}
|
||||
// Extend CartStore with EventEmitter to add eventing capabilities
|
||||
var CartStore = _.extend({}, EventEmitter.prototype, {
|
||||
|
||||
dataStartedLoading: function() {
|
||||
return _cartDataLoadCalled;
|
||||
},
|
||||
dataStartedLoading: function() {
|
||||
return _cartDataLoadCalled;
|
||||
},
|
||||
|
||||
getStateFor: function(itemId) {
|
||||
getStateFor: function(itemId) {
|
||||
|
||||
var state = states[itemId] || new ItemInCart({
|
||||
item_id: itemId,
|
||||
count: 0
|
||||
})
|
||||
return state
|
||||
},
|
||||
var state = states[itemId] || new ItemInCart({
|
||||
item_id: itemId,
|
||||
count: 0
|
||||
})
|
||||
return state
|
||||
},
|
||||
|
||||
getWholeCartState: function() {
|
||||
getWholeCartState: function() {
|
||||
|
||||
var numberOfItems = 0;
|
||||
var numberOfItems = 0;
|
||||
|
||||
for (key in states) {
|
||||
if (states.hasOwnProperty(key)) {
|
||||
var value = states[key];
|
||||
if (value.get('count') > 0) {
|
||||
numberOfItems += value.get('count');
|
||||
}
|
||||
}
|
||||
};
|
||||
for (key in states) {
|
||||
if (states.hasOwnProperty(key)) {
|
||||
var value = states[key];
|
||||
if (value.get('count') > 0) {
|
||||
numberOfItems += value.get('count');
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var state = {
|
||||
count: numberOfItems,
|
||||
items: _itemsForDisplay,
|
||||
itemCounts: states,
|
||||
deliveryDestination: _deliveryDestination,
|
||||
deliveryDestinationErrors: _deliveryDestinationErrors,
|
||||
isDeliveryDestinationValid: isDeliveryDestinationValid(),
|
||||
deliveryCosts: _deliveryCosts,
|
||||
destinationValid: isDeliveryDestinationValid()
|
||||
};
|
||||
return state;
|
||||
},
|
||||
var state = {
|
||||
count: numberOfItems,
|
||||
items: _itemsForDisplay,
|
||||
itemCounts: states,
|
||||
deliveryDestination: _deliveryDestination,
|
||||
deliveryDestinationErrors: _deliveryDestinationErrors,
|
||||
isDeliveryDestinationValid: isDeliveryDestinationValid(),
|
||||
deliveryCosts: _deliveryCosts,
|
||||
destinationValid: isDeliveryDestinationValid()
|
||||
};
|
||||
return state;
|
||||
},
|
||||
|
||||
// Emit Change event
|
||||
emitChange: function() {
|
||||
this.emit('change');
|
||||
},
|
||||
// Emit Change event
|
||||
emitChange: function() {
|
||||
this.emit('change');
|
||||
},
|
||||
|
||||
// Add change listener
|
||||
addChangeListener: function(callback) {
|
||||
this.on('change', callback);
|
||||
},
|
||||
// Add change listener
|
||||
addChangeListener: function(callback) {
|
||||
this.on('change', callback);
|
||||
},
|
||||
|
||||
// Remove change listener
|
||||
removeChangeListener: function(callback) {
|
||||
this.removeListener('change', callback);
|
||||
},
|
||||
// Remove change listener
|
||||
removeChangeListener: function(callback) {
|
||||
this.removeListener('change', callback);
|
||||
},
|
||||
|
||||
isDeliveryDestinationValid: isDeliveryDestinationValid
|
||||
isDeliveryDestinationValid: isDeliveryDestinationValid
|
||||
|
||||
|
||||
|
||||
@@ -280,43 +281,43 @@ var CartStore = _.extend({}, EventEmitter.prototype, {
|
||||
|
||||
// Register callback with AppDispatcher
|
||||
AppDispatcher.register(function(payload) {
|
||||
var action = payload.action;
|
||||
var text;
|
||||
var action = payload.action;
|
||||
var text;
|
||||
|
||||
switch (action.actionType) {
|
||||
case CartConstants.LOAD_CART_CONTENTS:
|
||||
loadCart();
|
||||
break;
|
||||
case CartConstants.TAKE_ITEM_OUT:
|
||||
takeItemOut(action.itemId);
|
||||
break;
|
||||
case CartConstants.CART_DATA_LOADED:
|
||||
// do nothing - just emmit change
|
||||
break;
|
||||
case CartConstants.SAVE_CART_STATE_FOR_ITEM:
|
||||
if (isDeliveryDestinationValid()) {
|
||||
saveCartStateForItem(action.itemId);
|
||||
}
|
||||
break;
|
||||
case CartConstants.CHANGE_DELIVERY_DESTINATION_PROPERTY:
|
||||
changeDeliveryDestinationProperty(action.propertyName, action.value)
|
||||
break;
|
||||
case CartConstants.CONFIRM_DELIVERY:
|
||||
saveDeliveryDestination();
|
||||
break;
|
||||
case CartConstants.ADD_N_ITEMS:
|
||||
addNItems(action.item, action.count);
|
||||
break;
|
||||
case CartConstants.SET_ITEM_COUNT:
|
||||
setItemCount(action.itemId, action.count);
|
||||
break;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
switch (action.actionType) {
|
||||
case CartConstants.LOAD_CART_CONTENTS:
|
||||
loadCart();
|
||||
break;
|
||||
case CartConstants.TAKE_ITEM_OUT:
|
||||
takeItemOut(action.itemId);
|
||||
break;
|
||||
case CartConstants.CART_DATA_LOADED:
|
||||
// do nothing - just emmit change
|
||||
break;
|
||||
case CartConstants.SAVE_CART_STATE_FOR_ITEM:
|
||||
if (isDeliveryDestinationValid()) {
|
||||
saveCartStateForItem(action.itemId);
|
||||
}
|
||||
break;
|
||||
case CartConstants.CHANGE_DELIVERY_DESTINATION_PROPERTY:
|
||||
changeDeliveryDestinationProperty(action.propertyName, action.value)
|
||||
break;
|
||||
case CartConstants.CONFIRM_DELIVERY:
|
||||
saveDeliveryDestination();
|
||||
break;
|
||||
case CartConstants.ADD_N_ITEMS:
|
||||
addNItems(action.item, action.count);
|
||||
break;
|
||||
case CartConstants.SET_ITEM_COUNT:
|
||||
setItemCount(action.itemId, action.count);
|
||||
break;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
|
||||
// If action was responded to, emit change event
|
||||
CartStore.emitChange();
|
||||
return true;
|
||||
// If action was responded to, emit change event
|
||||
CartStore.emitChange();
|
||||
return true;
|
||||
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user