logout and login cart state is working now - except a little bug with icon on login

This commit is contained in:
Senad Uka
2015-03-19 06:47:41 +01:00
parent 1d16951fcf
commit 4b019d6b9e
4 changed files with 126 additions and 58 deletions

View File

@@ -27,20 +27,20 @@ post '/cart/item', &update_cart_item
# gets list of items in cart without count
get '/cart/item/display' do
get '/cart/item/display' do
cart = Cart.just_find(anonymous_id, logged_in_user_id)
item_ids = cart.item_in_carts.map do |x|
x.item_id
x.item_id
end
items = []
items = Item.find(item_ids) if cart.item_in_carts.length > 0
items = Item.find(item_ids) if cart.item_in_carts.length > 0
prepare_items_for_mass_display(items)
end
get '/cart/delivery_destination' do
get '/cart/delivery_destination' do
cart = Cart.just_find(anonymous_id, logged_in_user_id)
cart.delivery_destination.to_json(:except => [:created_at, :email_verification_code, :phone_verification_code])
end
end
update_delivery_destination = ->() {
cart = Cart.just_find(anonymous_id, logged_in_user_id)
@@ -54,7 +54,7 @@ put '/cart/delivery_destination', &update_delivery_destination
post '/cart/delivery_destination', &update_delivery_destination
post '/cart/confirmation' do
post '/cart/confirmation' do
anonymous = anonymous_id
cart = Cart.just_find(anonymous, logged_in_user_id)
if cart.item_in_carts.length > 0
@@ -65,4 +65,4 @@ post '/cart/confirmation' do
# in order for next call of Cart#just_find to be ready
Cart.find_or_create(anonymous, logged_in_user_id)
"OK".to_json
end
end

View File

@@ -23,11 +23,11 @@ ActiveRecord::Schema.define(version: 20150318174814) do
t.datetime "updated_at", null: false
t.string "anonymous_id_string"
t.integer "delivery_destination_id"
t.boolean "confirmed", default: false
t.boolean "packed", default: false
t.boolean "canceled_on_check", default: false
t.boolean "canceled_on_delivery", default: false
t.boolean "delivered", default: false
t.boolean "confirmed"
t.boolean "packed"
t.boolean "canceled_on_check"
t.boolean "canceled_on_delivery"
t.boolean "delivered"
t.text "internal_note"
end
@@ -94,6 +94,7 @@ ActiveRecord::Schema.define(version: 20150318174814) do
t.datetime "updated_at", null: false
t.string "tags"
t.json "traits"
t.integer "supplier_id"
t.decimal "weight", precision: 5, scale: 3
t.integer "special_offer_id"
end
@@ -145,6 +146,19 @@ ActiveRecord::Schema.define(version: 20150318174814) do
t.integer "order"
end
create_table "suppliers", force: :cascade do |t|
t.string "name"
t.string "address"
t.string "postal_code"
t.string "town"
t.string "phone"
t.string "contact_person"
t.string "email"
t.text "note"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
create_table "units", force: :cascade do |t|
t.string "name"
t.string "short_name", limit: 4

View File

@@ -1,14 +1,50 @@
class Cart < ActiveRecord::Base
has_many :item_in_carts, -> { order "created_at" }
belongs_to :delivery_destination
def self.find_or_create(anonymous_id, user_id)
cart = Cart.where(user_id: user_id).where(ordered: false).first
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
safe_user_id = (user_id > 0) ? user_id : nil
cart ||= Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
cart.delivery_destination ||= DeliveryDestination.create_from_last(anonymous_id, safe_user_id);
cart.save!
cart = nil
ActiveRecord::Base.transaction do
logged_in_user_cart = Cart.where(user_id: user_id).where(ordered: false).first
anonymous_user_cart = Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
safe_user_id = (user_id > 0) ? user_id : nil
if safe_user_id.nil?
cart = anonymous_user_cart || Cart.create!(anonymous_id_string: anonymous_id, user_id: safe_user_id, ordered: false )
else
cart = logged_in_user_cart || Cart.create!(anonymous_id_string: nil, user_id: safe_user_id, ordered: false )
logged_in_user_cart = cart
end
# add the contents of anonymous cart
# to the logged in one on login
if logged_in_user_cart and anonymous_user_cart
logged_in_items = logged_in_user_cart.item_in_carts
anonymous_user_cart.item_in_carts.each do |a_item|
item_exists = false
logged_in_items.each do |l_item|
if a_item.item_id == l_item.item_id
l_item.count += a_item.count
l_item.save!
a_item.destroy
item_exists = true
break
end
end
unless item_exists
a_item.cart_id = logged_in_user_cart.id
a_item.save!
end
end
end
cart.delivery_destination ||= DeliveryDestination.create_from_last(anonymous_id, safe_user_id);
cart.save!
end
return cart
end
@@ -17,4 +53,4 @@ class Cart < ActiveRecord::Base
cart ||= Cart.where(anonymous_id_string: anonymous_id).where(ordered: false).first
return cart
end
end
end

View File

@@ -2,7 +2,8 @@ var AppDispatcher = require('../dispatcher/appDispatcher');
var EventEmitter = require('events').EventEmitter;
var UserConstants = require('../constants/userConstants');
var _ = require('underscore');
var NavigationActions = require('../actions/navigationActions');
var CartActions = require('../actions/cartActions');
var InitializationActions = require('../actions/initializationActions');
var _registrationState = {};
var _loginState = {};
@@ -29,6 +30,8 @@ var handleLoginSuccess = function(user) {
user: user
};
refreshCart();
};
var handleLoginFailure = function(error) {
@@ -40,12 +43,12 @@ var handleLoginFailure = function(error) {
};
var handleCheckLoginArrived = function(user, error) {
if(user) {
if (user) {
_loginState = {
loggedIn: true,
user: user
};
} else{
} else {
_loginState = {
loggedIn: false
};
@@ -57,70 +60,85 @@ var handleLogoutDone = function() {
_loginState = {
loggedIn: false
}
refreshCart();
};
var refreshCart = function() {
setTimeout(function() {
// needed for cart reset
InitializationActions.initialize();
setTimeout(function() {
// reload the items
CartActions.load();
}, 0);
}, 0);
}
// Extend SectionStore with EventEmitter to add eventing capabilities
var UserStore = _.extend({}, EventEmitter.prototype, {
getRegistrationState: function() {
//return _categoryDetails;
return _registrationState;
//return _categoryDetails;
return _registrationState;
},
getLoginState: function() {
return _loginState;
return _loginState;
},
// Emit Change event
emitChange: function() {
console.log("Emmiting Section change!");
this.emit('change');
console.log("Emmiting Section change!");
this.emit('change');
},
// Add change listener
addChangeListener: function(callback) {
this.on('change', callback);
this.on('change', callback);
},
// Remove change listener
removeChangeListener: function(callback) {
this.removeListener('change', callback);
this.removeListener('change', callback);
}
});
// Register callback with AppDispatcher
AppDispatcher.register(function(payload) {
var action = payload.action;
var text;
var action = payload.action;
var text;
switch(action.actionType) {
switch (action.actionType) {
case UserConstants.REGISTRATION_SUCCESS:
handleRegistrationSuccess(action.user);
break;
case UserConstants.REGISTRATION_FAILURE:
handleRegistrationFailure(action.error);
break;
case UserConstants.LOGIN_SUCCESS:
handleLoginSuccess(action.user);
break;
case UserConstants.LOGIN_FAILURE:
handleLoginFailure(action.error);
break;
case UserConstants.CHECK_LOGIN_ARRIVED:
handleCheckLoginArrived(action.user, action.error);
break;
case UserConstants.USER_LOGOUT_DONE:
handleLogoutDone();
break;
default:
return true;
}
case UserConstants.REGISTRATION_SUCCESS:
handleRegistrationSuccess(action.user);
break;
case UserConstants.REGISTRATION_FAILURE:
handleRegistrationFailure(action.error);
break;
case UserConstants.LOGIN_SUCCESS:
handleLoginSuccess(action.user);
break;
case UserConstants.LOGIN_FAILURE:
handleLoginFailure(action.error);
break;
case UserConstants.CHECK_LOGIN_ARRIVED:
handleCheckLoginArrived(action.user, action.error);
break;
case UserConstants.USER_LOGOUT_DONE:
handleLogoutDone();
break;
default:
return true;
}
// If action was responded to, emit change event
UserStore.emitChange();
return true;
// If action was responded to, emit change event
UserStore.emitChange();
return true;
});
module.exports = UserStore;
module.exports = UserStore;