Files
old-ribica/front-ui/app/stores/cartStore.js

303 lines
8.3 KiB
JavaScript

var AppDispatcher = require('../dispatcher/appDispatcher');
var EventEmitter = require('events').EventEmitter;
var CartConstants = require('../constants/cartConstants');
var CartActions = require('../actions/cartActions');
var NavigationActions = require('../actions/navigationActions');
var ItemInCart = require('../models/itemInCart');
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 Validation = require('../utils/validation');
var _ = require('underscore');
var states = {}
var _itemsInCart = new ItemInCartCollection();
var _itemsForDisplay = new ItemCollection();
_itemsForDisplay.setFromCart(true);
var _deliveryDestination = new DeliveryDestination();
var _deliveryDestinationErrors = {};
var _deliveryCosts = new Place({
postalCode: _deliveryDestination.get('place')
})
var loadCart = function() {
_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;
}
CartActions.dataLoaded();
}
});
_itemsForDisplay.fetch({
success: function() {
CartActions.dataLoaded();
}
});
_deliveryDestination.fetch({
success: function() {
validateDeliveryDestinationForm();
fetchPlace();
CartActions.dataLoaded();
}
});
};
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({
success: function() {
CartActions.dataLoaded();
}
});
};
var addItem = function(itemId) {
var state = states[itemId] || new ItemInCart({
item_id: itemId,
count: 0
})
state.set('count', state.get('count') + 1);
states[itemId] = state;
saveCartStateForItem(itemId);
};
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);
}
states[itemId] = state;
saveCartStateForItem(itemId);
};
var setItemCount = function(itemId, count) {
var state = states[itemId] || new ItemInCart({
item_id: itemId,
count: 0
})
console.log("Old state", state.get('count'));
state.set('count', state.get('count') + count);
console.log("New state", state.get('count'));
states[itemId] = state;
saveCartStateForItem(itemId);
}
var changeDeliveryDestinationProperty = function(property, value) {
_deliveryDestination.set(property, value);
if (property === 'place') {
fetchPlace();
}
validateDeliveryDestinationForm();
};
var confirmOrder = function() {
console.log("confirming");
var oc = new OrderConfirmation({
hamo: 'meho'
});
oc.save({
b: 'b'
}, {
success: function() {
console.log("done");
NavigationActions.goToThankYou();
loadCart();
}
});
};
var saveDeliveryDestination = function() {
console.log("saving delivery destination");
_deliveryDestination.save(null, {
success: function() {
console.log("saved delivery destination");
confirmOrder();
}
})
};
var validateDeliveryDestinationForm = function() {
_deliveryDestinationErrors = {};
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 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 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 isDeliveryDestinationValid = function() {
return Object.getOwnPropertyNames(_deliveryDestinationErrors).length === 0;
}
// Extend CartStore with EventEmitter to add eventing capabilities
var CartStore = _.extend({}, EventEmitter.prototype, {
getStateFor: function(itemId) {
var state = states[itemId] || new ItemInCart({
item_id: itemId,
count: 0
})
return state
},
getWholeCartState: function() {
var numberOfItems = 0;
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;
},
// Emit Change event
emitChange: function() {
console.log("Emitting cart change!");
this.emit('change');
},
// Add change listener
addChangeListener: function(callback) {
this.on('change', callback);
},
// Remove change listener
removeChangeListener: function(callback) {
this.removeListener('change', callback);
},
isDeliveryDestinationValid: isDeliveryDestinationValid
});
// Register callback with AppDispatcher
AppDispatcher.register(function(payload) {
var action = payload.action;
var text;
switch (action.actionType) {
case CartConstants.LOAD_CART_CONTENTS:
loadCart();
break;
case CartConstants.ADD_ITEM:
addItem(action.itemId);
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.SET_ITEM_COUNT:
setItemCount(action.itemId, action.count);
break;
default:
return true;
}
// If action was responded to, emit change event
CartStore.emitChange();
return true;
});
module.exports = CartStore;