fix for delivery destination not validating

This commit is contained in:
Edin Dazdarevic
2015-06-04 13:37:33 +02:00
parent 99492f137f
commit 763bc329fc

View File

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