added delivery costs to total / added ribica logo just for fun / added validation check when confirming delivery

This commit is contained in:
Senad Uka
2015-03-13 07:04:44 +01:00
parent e3abc09891
commit 648dee4636
17 changed files with 208 additions and 31 deletions

View File

@@ -8,6 +8,7 @@ var ItemInCartCollection = require('../models/itemInCartCollection');
var ItemCollection = require('../models/itemCollection');
var DeliveryDestination = require('../models/deliveryDestination');
var OrderConfirmation = require('../models/orderConfirmation');
var Place = require('../models/place');
var _ = require('underscore');
@@ -18,6 +19,9 @@ var _itemsForDisplay = new ItemCollection();
_itemsForDisplay.setFromCart(true);
var _deliveryDestination = new DeliveryDestination();
var _deliveryDestinationErrors = {};
var _deliveryCosts = new Place({
postalCode: _deliveryDestination.get('place')
})
var loadCart = function() {
@@ -41,6 +45,7 @@ var loadCart = function() {
_deliveryDestination.fetch({
success: function() {
validateDeliveryDestinationForm();
fetchPlace();
CartActions.dataLoaded();
}
});
@@ -48,6 +53,19 @@ var loadCart = function() {
};
var fetchPlace = function() {
_deliveryCosts = new Place({
postalCode: _deliveryDestination.get('place')
})
_deliveryCosts.fetch({
success: function() {
CartActions.dataLoaded();
}
})
}
var saveCartStateForItem = function(itemId) {
var item = CartStore.getStateFor(itemId);
item.save({
@@ -84,15 +102,23 @@ var takeItemOut = function(itemId) {
var changeDeliveryDestinationProperty = function(property, value) {
_deliveryDestination.set(property, value);
if (property === 'place') {
fetchPlace();
}
validateDeliveryDestinationForm();
};
var confirmOrder = function () {
var confirmOrder = function() {
console.log("confirming");
var oc = new OrderConfirmation({ hamo: 'meho' });
oc.save({b:'b'}, {
success: function () {
var oc = new OrderConfirmation({
hamo: 'meho'
});
oc.save({
b: 'b'
}, {
success: function() {
console.log("done");
NavigationActions.goToThankYou();
}
@@ -102,7 +128,7 @@ var confirmOrder = function () {
var saveDeliveryDestination = function() {
console.log("saving delivery destination");
_deliveryDestination.save(null,{
_deliveryDestination.save(null, {
success: function() {
console.log("saved delivery destination");
confirmOrder();
@@ -110,32 +136,32 @@ var saveDeliveryDestination = function() {
})
};
var validateDeliveryDestinationForm = function () {
var validateDeliveryDestinationForm = function() {
_deliveryDestinationErrors = {};
var nameRegex = /.+\s+.+/i;
if(_deliveryDestination.get('name').search(nameRegex) < 0) {
_deliveryDestinationErrors['name'] = "I prezime i ime su obavezni";
if (_deliveryDestination.get('name').search(nameRegex) < 0) {
_deliveryDestinationErrors['name'] = "I prezime i ime su obavezni";
}
var addressRegex = /.+\s+.+/i;
if(_deliveryDestination.get('address').search(addressRegex) < 0) {
_deliveryDestinationErrors['address'] = "Adresa mora biti ispravna";
if (_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(_deliveryDestination.get('email').search(emailRegex) < 0) {
_deliveryDestinationErrors['email'] = "Email mora biti ispravno upisan";
if (_deliveryDestination.get('email').search(emailRegex) < 0) {
_deliveryDestinationErrors['email'] = "Email mora biti ispravno upisan";
}
var phoneRegex = /[\d\s-]{7,8}/i;
if(_deliveryDestination.get('phone').search(phoneRegex) < 0) {
_deliveryDestinationErrors['phone'] = "Telefon mora biti ispravan";
if (_deliveryDestination.get('phone').search(phoneRegex) < 0) {
_deliveryDestinationErrors['phone'] = "Telefon mora biti ispravan";
}
var placeRegex = /^\s{0,1}\d{5}$/i;
if(_deliveryDestination.get('place').search(placeRegex) < 0) {
_deliveryDestinationErrors['place'] = "Mjesto mora biti izabrano";
if (_deliveryDestination.get('place').search(placeRegex) < 0) {
_deliveryDestinationErrors['place'] = "Mjesto mora biti izabrano";
}
var requiredFields = ["name", "email", "place", 'address', 'phone'];
@@ -143,17 +169,17 @@ var validateDeliveryDestinationForm = function () {
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]] = "*";
_deliveryDestinationErrors[requiredFields[i]] = "*";
}
}
}
var isDeliveryDestinationValid = function () {
return Object.getOwnPropertyNames(_deliveryDestinationErrors).length === 0;
}
// Extend CartStore with EventEmitter to add eventing capabilities
var isDeliveryDestinationValid = function() {
return Object.getOwnPropertyNames(_deliveryDestinationErrors).length === 0;
}
// Extend CartStore with EventEmitter to add eventing capabilities
var CartStore = _.extend({}, EventEmitter.prototype, {
getStateFor: function(itemId) {
@@ -184,8 +210,8 @@ var CartStore = _.extend({}, EventEmitter.prototype, {
itemCounts: states,
deliveryDestination: _deliveryDestination,
deliveryDestinationErrors: _deliveryDestinationErrors,
isDeliveryDestinationValid: isDeliveryDestinationValid()
isDeliveryDestinationValid: isDeliveryDestinationValid(),
deliveryCosts: _deliveryCosts
};
return state;
},
@@ -235,7 +261,9 @@ AppDispatcher.register(function(payload) {
// do nothing - jsut emmit change
break;
case CartConstants.SAVE_CART_STATE_FOR_ITEM:
saveCartStateForItem(action.itemId);
if (isDeliveryDestinationValid()) {
saveCartStateForItem(action.itemId);
}
break;
case CartConstants.CHANGE_DELIVERY_DESTINATION_PROPERTY:
changeDeliveryDestinationProperty(action.propertyName, action.value)