fixed problem with multiple carts and a whole load of problems with crazy delivery destination logic / if I didn't write it i would have thought it was written by a drug abuser during high
This commit is contained in:
11
front-ui/app/actions/initializationActions.js
Normal file
11
front-ui/app/actions/initializationActions.js
Normal file
@@ -0,0 +1,11 @@
|
||||
var AppDispatcher = require('../dispatcher/appDispatcher');
|
||||
var InitializationConstants = require('../constants/initializationConstants');
|
||||
|
||||
var IntializationActions = {
|
||||
initialize: function() {
|
||||
AppDispatcher.handleAction({
|
||||
actionType : InitializationConstants.INITIALIZE,
|
||||
});
|
||||
}
|
||||
};
|
||||
module.exports = IntializationActions;
|
||||
@@ -3,14 +3,42 @@ var React = require('react'),
|
||||
Router = require('react-router'),
|
||||
Link = Router.Link,
|
||||
RouteHandler = Router.RouteHandler,
|
||||
LoginStatus = require('./shared/loginStatus');
|
||||
LoginStatus = require('./shared/loginStatus'),
|
||||
InitializationStore = require('../stores/initializationStore'),
|
||||
InitializationActions = require('../actions/initializationActions');
|
||||
|
||||
var CartIcon = require('./cart/cartIcon');
|
||||
|
||||
var RootApp = React.createClass({
|
||||
|
||||
// Add change listeners to stores
|
||||
componentDidMount: function() {
|
||||
InitializationStore.addChangeListener(this._onChange);
|
||||
InitializationActions.initialize();
|
||||
},
|
||||
|
||||
|
||||
getInitialState: function() {
|
||||
return InitializationStore.getState();
|
||||
},
|
||||
|
||||
|
||||
_onChange: function () {
|
||||
if (this.isMounted()) {
|
||||
this.setState(InitializationStore.getState());
|
||||
}
|
||||
},
|
||||
|
||||
componentWillUnmount: function () {
|
||||
InitializationStore.removeChangeListener(this._onChange);
|
||||
},
|
||||
|
||||
render: function() {
|
||||
|
||||
if (!this.state.isEverythingReadyToStartTheShow) {
|
||||
return (<div>loading...</div>);
|
||||
}
|
||||
|
||||
return (
|
||||
<div className="container">
|
||||
<div className='page-header'>
|
||||
|
||||
6
front-ui/app/constants/initializationConstants.js
Normal file
6
front-ui/app/constants/initializationConstants.js
Normal file
@@ -0,0 +1,6 @@
|
||||
var keyMirror = require('react/lib/keyMirror');
|
||||
|
||||
// Define action constants
|
||||
module.exports = keyMirror({
|
||||
INITIALIZE: null
|
||||
});
|
||||
22
front-ui/app/models/cart.js
Normal file
22
front-ui/app/models/cart.js
Normal file
@@ -0,0 +1,22 @@
|
||||
var Backbone = require('backbone');
|
||||
var Globals = require('../globals');
|
||||
|
||||
var Cart = Backbone.Model.extend({
|
||||
|
||||
initialize: function() {
|
||||
$.ajaxPrefilter(
|
||||
function(options, originalOptions, jqXHR) {
|
||||
options.xhrFields = {
|
||||
withCredentials: true
|
||||
}
|
||||
}
|
||||
);
|
||||
},
|
||||
urlRoot : Globals.ApiUrl + '/cart',
|
||||
defaults : {
|
||||
'yes': 'yes'
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
module.exports = Cart;
|
||||
@@ -9,6 +9,7 @@ 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');
|
||||
|
||||
@@ -141,27 +142,27 @@ var validateDeliveryDestinationForm = function() {
|
||||
_deliveryDestinationErrors = {};
|
||||
|
||||
var nameRegex = /.+\s+.+/i;
|
||||
if (_deliveryDestination.get('name').search(nameRegex) < 0) {
|
||||
if (Validation.safeString(_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) {
|
||||
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 (_deliveryDestination.get('email').search(emailRegex) < 0) {
|
||||
if (Validation.safeString(_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) {
|
||||
if (Validation.safeString(_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) {
|
||||
if (Validation.safeString(_deliveryDestination.get('place')).search(placeRegex) < 0){
|
||||
_deliveryDestinationErrors['place'] = "Mjesto mora biti izabrano";
|
||||
}
|
||||
|
||||
|
||||
69
front-ui/app/stores/initializationStore.js
Normal file
69
front-ui/app/stores/initializationStore.js
Normal file
@@ -0,0 +1,69 @@
|
||||
var AppDispatcher = require('../dispatcher/appDispatcher');
|
||||
var EventEmitter = require('events').EventEmitter;
|
||||
var Cart = require('../models/cart');
|
||||
|
||||
var InitializationConstants = require('../constants/initializationConstants')
|
||||
var _ = require('underscore');
|
||||
|
||||
|
||||
var state = {
|
||||
isEverythingReadyToStartTheShow: false
|
||||
}
|
||||
|
||||
var initializeTheShop = function () {
|
||||
// for now only guarantee that cart is created
|
||||
var cart = new Cart();
|
||||
cart.save(null, {
|
||||
success: function () {
|
||||
state.isEverythingReadyToStartTheShow = true;
|
||||
InitializationStore.emitChange();
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Extend ItemStore with EventEmitter to add eventing capabilities
|
||||
var InitializationStore = _.extend({}, EventEmitter.prototype, {
|
||||
|
||||
getState: function() {
|
||||
return state;
|
||||
},
|
||||
|
||||
// Emit Change event
|
||||
emitChange: function() {
|
||||
console.log("InitializationStore change!");
|
||||
this.emit('change');
|
||||
},
|
||||
|
||||
// Add change listener
|
||||
addChangeListener: function(callback) {
|
||||
this.on('change', callback);
|
||||
},
|
||||
|
||||
// Remove change listener
|
||||
removeChangeListener: function(callback) {
|
||||
this.removeListener('change', callback);
|
||||
}
|
||||
|
||||
});
|
||||
|
||||
|
||||
// Register callback with AppDispatcher
|
||||
InitializationStore.dispatchToken = AppDispatcher.register(function(payload) {
|
||||
var action = payload.action;
|
||||
|
||||
switch (action.actionType) {
|
||||
|
||||
case InitializationConstants.INITIALIZE:
|
||||
initializeTheShop();
|
||||
break;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
|
||||
// If action was responded to, emit change event
|
||||
InitializationStore.emitChange();
|
||||
return true;
|
||||
|
||||
});
|
||||
|
||||
module.exports = InitializationStore;
|
||||
@@ -1,17 +1,28 @@
|
||||
var Validations = {
|
||||
_emailRe: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
||||
isValidEmail: function(value) {
|
||||
return this._emailRe.test(value);
|
||||
var _ = require('underscore');
|
||||
|
||||
},
|
||||
var Validations = {
|
||||
_emailRe: /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,
|
||||
isValidEmail: function(value) {
|
||||
return this._emailRe.test(value);
|
||||
|
||||
},
|
||||
isValidRequired: function(value) {
|
||||
if(value === undefined || value === "") {
|
||||
if (value === undefined || value === "") {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
|
||||
},
|
||||
|
||||
safeString: function(item) {
|
||||
if (!_.isString(item)) {
|
||||
return "";
|
||||
} else {
|
||||
return item;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
module.exports = Validations;
|
||||
module.exports = Validations;
|
||||
Reference in New Issue
Block a user