diff options
| author | Jules Laplace <jules@okfoc.us> | 2015-11-23 01:45:53 -0500 |
|---|---|---|
| committer | Jules Laplace <jules@okfoc.us> | 2015-11-23 01:45:53 -0500 |
| commit | 7d9950316368d825510588ded16e5194b4840931 (patch) | |
| tree | 294fa8647e451eae0d741cfeb88d2173b20fd940 /StoneIsland/www/js | |
| parent | 6b37f28e2dba8f191ff5df194e5215399b696faa (diff) | |
add to cart after signin..
Diffstat (limited to 'StoneIsland/www/js')
| -rw-r--r-- | StoneIsland/www/js/index.js | 2 | ||||
| -rw-r--r-- | StoneIsland/www/js/lib/_router.js | 3 | ||||
| -rw-r--r-- | StoneIsland/www/js/lib/account/AccountView.js | 35 | ||||
| -rw-r--r-- | StoneIsland/www/js/lib/auth/LoginView.js | 4 | ||||
| -rw-r--r-- | StoneIsland/www/js/lib/auth/SignupView.js | 13 | ||||
| -rw-r--r-- | StoneIsland/www/js/lib/cart/CartSummary.js | 19 | ||||
| -rw-r--r-- | StoneIsland/www/js/lib/cart/CartView.js | 3 | ||||
| -rw-r--r-- | StoneIsland/www/js/lib/nav/HeaderView.js | 13 | ||||
| -rw-r--r-- | StoneIsland/www/js/lib/products/ProductView.js | 25 | ||||
| -rw-r--r-- | StoneIsland/www/js/sdk/_sdk.js | 2 | ||||
| -rw-r--r-- | StoneIsland/www/js/sdk/account.js | 4 | ||||
| -rw-r--r-- | StoneIsland/www/js/sdk/auth.js | 33 | ||||
| -rw-r--r-- | StoneIsland/www/js/sdk/cart.js | 1 | ||||
| -rw-r--r-- | StoneIsland/www/js/vendor/promise.js | 34 | ||||
| -rw-r--r-- | StoneIsland/www/js/vendor/util.js | 1 |
15 files changed, 145 insertions, 47 deletions
diff --git a/StoneIsland/www/js/index.js b/StoneIsland/www/js/index.js index 44146e65..ed8d9e5a 100644 --- a/StoneIsland/www/js/index.js +++ b/StoneIsland/www/js/index.js @@ -68,7 +68,7 @@ var app = (function(){ app.view = null app.router = new SiteRouter () - app.router.route() + app.account.connect( app.router.route.bind(app.router) ) } return app diff --git a/StoneIsland/www/js/lib/_router.js b/StoneIsland/www/js/lib/_router.js index bbc6bb5f..b70d9be8 100644 --- a/StoneIsland/www/js/lib/_router.js +++ b/StoneIsland/www/js/lib/_router.js @@ -59,6 +59,9 @@ var SiteRouter = Router.extend({ default_view: function(name){ var fn = function(){ console.log(name) + if (app.view != app.login && app.view != app.signin) { + app.last_view = app.view + } if (app.view && app.view.hide) { app.view.hide() } diff --git a/StoneIsland/www/js/lib/account/AccountView.js b/StoneIsland/www/js/lib/account/AccountView.js index c72a2a40..c6f68e88 100644 --- a/StoneIsland/www/js/lib/account/AccountView.js +++ b/StoneIsland/www/js/lib/account/AccountView.js @@ -1,15 +1,18 @@ var AccountView = View.extend({ initialize: function(){ - auth.init(this.ready.bind(this)) }, - ready: function(){ + connect: function(cb){ + auth.init(this.ready.bind(this, cb)) + }, + + ready: function(cb){ if (auth.logged_in()) { - this.logged_in() + this.logged_in(cb) } else { - this.logged_out() + this.logged_out(cb) } }, @@ -19,27 +22,41 @@ var AccountView = View.extend({ console.log("no addresses") return } - data.AddressBook.forEach(function(address){ - if (addressBookItem.isDefault) { + // console.log(data.AddressBook) + data.AddressBook.addressBookItem.forEach(function(item){ + if (item.isDefault) { // populate app.shipping.address } - else if (addressBookItem.isBillingDefault) { + else if (item.isBillingDefault) { // populate app.billing.address } }) }, - logged_in: function(){ + logged_in: function(cb){ sdk.address.list({ success: this.populateAddresses.bind(this) }) $("#nav .login").hide() $("#nav .account, #nav .logout").show() + if (app.last_view && app.last_view != app.login && app.last_view != app.signin && app.last_view != app.logout) { + app.view && app.view.hide && app.view.hide() + app.view = app.last_view + app.view.show() + } + else { + cb && cb() + } + if (auth.deferred_product) { + auth.add_deferred_product_to_cart() + } }, - logged_out: function(){ + logged_out: function(cb){ $("#nav .login").show() $("#nav .account, #nav .logout").hide() + $("#nav").removeClass("account") + cb && cb() }, }) diff --git a/StoneIsland/www/js/lib/auth/LoginView.js b/StoneIsland/www/js/lib/auth/LoginView.js index b088190f..b761b5e8 100644 --- a/StoneIsland/www/js/lib/auth/LoginView.js +++ b/StoneIsland/www/js/lib/auth/LoginView.js @@ -37,7 +37,7 @@ var LoginView = FormView.extend({ success: function(data){ // console.log(data) - app.account.logged_in() + app.account.logged_in(function(){ app.router.go("store") }) }, error: function(data){ @@ -46,7 +46,7 @@ var LoginView = FormView.extend({ }, cancel: function(){ - auth.defer_add_to_cart = null + auth.deferred_product = null }, }) diff --git a/StoneIsland/www/js/lib/auth/SignupView.js b/StoneIsland/www/js/lib/auth/SignupView.js index 3293b3aa..fe0e28db 100644 --- a/StoneIsland/www/js/lib/auth/SignupView.js +++ b/StoneIsland/www/js/lib/auth/SignupView.js @@ -86,7 +86,7 @@ var SignupView = FormView.extend({ auth.user.name = this.last_data.Name auth.user.surname = this.last_data.Surname auth.user.email = this.last_data.Email - app.account.logged_in() + app.account.logged_in(function(){ app.router.go("store") }) }, error: function(data){ @@ -97,15 +97,4 @@ var SignupView = FormView.extend({ auth.deferred_product = null }, -/* - var new_user_data = { - "Email": "testit.account" + Math.floor(Math.random() * 10000000) + "@yoox.com", - "Password": "TestPassword", - "Gender": "M", - "Name": "TestName", - "Surname": "TestSurname", - "DataProfiling": true, - } -*/ - })
\ No newline at end of file diff --git a/StoneIsland/www/js/lib/cart/CartSummary.js b/StoneIsland/www/js/lib/cart/CartSummary.js index 268f6ee0..77c40b52 100644 --- a/StoneIsland/www/js/lib/cart/CartSummary.js +++ b/StoneIsland/www/js/lib/cart/CartSummary.js @@ -7,6 +7,8 @@ var CartSummary = ScrollableView.extend({ events: { }, + data: null, + initialize: function(opt){ this.parent = opt.parent this.$loader = this.$(".loader") @@ -18,8 +20,16 @@ var CartSummary = ScrollableView.extend({ show: function(){ document.body.className = "cart" app.cart.el.className = "summary" + if (auth.has_cart()) { + this.load() + } + else { + this.empty() + } + }, + + load: function(){ this.$loader.show() - sdk.cart.get_status({ success: this.populate.bind(this), error: this.empty.bind(this), @@ -27,12 +37,16 @@ var CartSummary = ScrollableView.extend({ }, populate: function(data){ + this.data = data + this.$loader.hide() app.footer.show("SHIPPING >", "CANCEL") console.log(data) - this.parent.$itemcount.html("1 ITEM") + app.header.set_cart_count(data.Cart.Items.length) + console.log("HEY") + this.parent.$itemcount.html(pluralize(data.Cart.Items.length, "ITEM", "S")) this.$el.removeClass("empty").addClass("full") this.deferScrollToTop() @@ -41,6 +55,7 @@ var CartSummary = ScrollableView.extend({ empty: function(){ this.$loader.hide() app.footer.hide() + app.header.set_cart_count(0) this.parent.$itemcount.html("0 ITEMS") this.$el.addClass("empty").removeClass("full") }, diff --git a/StoneIsland/www/js/lib/cart/CartView.js b/StoneIsland/www/js/lib/cart/CartView.js index fdad3a07..465ffe50 100644 --- a/StoneIsland/www/js/lib/cart/CartView.js +++ b/StoneIsland/www/js/lib/cart/CartView.js @@ -17,6 +17,9 @@ var CartView = View.extend({ this.$itemcount = this.$(".itemcount") }, + + load: function(){ + }, show: function(){ document.body.className = "cart" diff --git a/StoneIsland/www/js/lib/nav/HeaderView.js b/StoneIsland/www/js/lib/nav/HeaderView.js index 4425917d..7563be2d 100644 --- a/StoneIsland/www/js/lib/nav/HeaderView.js +++ b/StoneIsland/www/js/lib/nav/HeaderView.js @@ -11,6 +11,7 @@ var HeaderView = View.extend({ initialize: function(){ this.$burger = this.$(".burger") this.$cart = this.$(".cart") + this.$cart_count = this.$(".cart_count") }, set_back: function(state){ @@ -38,5 +39,17 @@ var HeaderView = View.extend({ cart: function(){ app.router.go("cart") }, + + count: 0, + set_cart_count: function(n){ + this.count = n + this.$cart_count.html(n) + }, + increment_cart_count: function(){ + this.$cart_count.html( ++this.count ) + }, + decrement_cart_count: function(){ + this.$cart_count.html( --this.count ) + }, })
\ No newline at end of file diff --git a/StoneIsland/www/js/lib/products/ProductView.js b/StoneIsland/www/js/lib/products/ProductView.js index d1a9005f..69c6e407 100644 --- a/StoneIsland/www/js/lib/products/ProductView.js +++ b/StoneIsland/www/js/lib/products/ProductView.js @@ -190,22 +190,35 @@ var ProductView = ScrollableView.extend({ }.bind(this)) }, + // ADD TO CART save: function(){ - // add to cart + auth.deferred_product = { Size: this.size, Code10: this.code } if ( ! auth.logged_in() ) { - auth.deferred_product = { size: this.size, code: this.code } + app.router.go("account/login") + app.last_view = app.cart } else if ( ! auth.has_cart() ) { - auth.deferred_product = { size: this.size, code: this.code } - auth.create_cart(auth.defer_add_to_cart) + auth.create_cart(auth.add_deferred_product_to_cart) } else { - // ADD TO CART! + auth.add_deferred_product_to_cart() } }, + // BUY NOW cancel: function(){ - // buy now + auth.deferred_product = { Size: this.size, Code10: this.code } + if ( ! auth.logged_in() ) { + app.go("account/login") + app.last_view = app.cart + } + else if ( ! auth.has_cart() ) { + auth.create_cart(auth.add_deferred_product_to_cart) + } + else { + app.go("cart") + auth.add_deferred_product_to_cart() + } }, back: function(){ diff --git a/StoneIsland/www/js/sdk/_sdk.js b/StoneIsland/www/js/sdk/_sdk.js index d50a99e0..27773310 100644 --- a/StoneIsland/www/js/sdk/_sdk.js +++ b/StoneIsland/www/js/sdk/_sdk.js @@ -35,4 +35,4 @@ var sdk = (function(){ }) return sdk -})() +})()
\ No newline at end of file diff --git a/StoneIsland/www/js/sdk/account.js b/StoneIsland/www/js/sdk/account.js index 4121bf9a..3b48b0d9 100644 --- a/StoneIsland/www/js/sdk/account.js +++ b/StoneIsland/www/js/sdk/account.js @@ -36,8 +36,8 @@ sdk.account = (function(){ data: JSON.stringify( opt.data ), success: function(data){ auth.user = data['UserFull'] - auth.user_id = data['UserFull']['idUser'] - auth.access_token = data['UserFull']['AccessToken'] + user_id = data['UserFull']['idUser'] + access_token = data['UserFull']['AccessToken'] auth.set_user(user_id, access_token) diff --git a/StoneIsland/www/js/sdk/auth.js b/StoneIsland/www/js/sdk/auth.js index 66dd81e8..65505d81 100644 --- a/StoneIsland/www/js/sdk/auth.js +++ b/StoneIsland/www/js/sdk/auth.js @@ -24,7 +24,9 @@ var auth = sdk.auth = (function(){ // android: cordova.file.externalRootDirectory api auth.init = function(fn){ + console.log("AUTH INIT") auth.get_user(function(){ + console.log("cool") if (auth.logged_in()) { auth.get_cart() } @@ -71,34 +73,41 @@ var auth = sdk.auth = (function(){ cb && cb() } auth.create_cart = function(cb){ - if (auth.has_cart) { return cb() } - sdk.cart.initialize().done(function(){ - sdk.cart.set_user().done(function(){ - auth.set_cart(sdk.cart.id, sdk.cart.token, function(){ - cb && cb() + if (auth.has_cart()) { return cb() } + sdk.cart.initialize({ + success: function(data){ + sdk.cart.set_user({ + success: function(){ + auth.set_cart(sdk.cart.id, sdk.cart.token, function(){ + cb && cb() + }) + } }) - }) + } }) } - auth.defer_add_to_cart = function(){ + auth.add_deferred_product_to_cart = function(){ // auth.deferred_product sdk.cart.add_item({ - data: { - Code10: auth.deferred_product.code, - Size: auth.deferred_product.size, + data: auth.deferred_product, + success: function(){ + console.log("ADDED") } }) + auth.deferred_product = null + app.header.increment_cart_count() } + auth.log_out = function(){ auth.clear_user() auth.clear_cart() auth.view_logged_out() } auth.logged_in = function(){ - return (auth.user_id !== -1) + return (auth.user_id !== -1) && (auth.user_id !== "undefined") } auth.has_cart = function(){ - return (cart.id !== -1) + return (sdk.cart.id !== -1) && (sdk.cart.id !== "undefined") } return auth diff --git a/StoneIsland/www/js/sdk/cart.js b/StoneIsland/www/js/sdk/cart.js index 52b31a14..3f53d9d5 100644 --- a/StoneIsland/www/js/sdk/cart.js +++ b/StoneIsland/www/js/sdk/cart.js @@ -18,6 +18,7 @@ sdk.cart = (function(){ success: function(data){ cart.id = data["CartSession"]["CartId"] cart.token = data["CartSession"]["CartToken"] + auth.set_cart( cart.id, cart.token ) opt.success(data) }, error: opt.error, diff --git a/StoneIsland/www/js/vendor/promise.js b/StoneIsland/www/js/vendor/promise.js new file mode 100644 index 00000000..f458ab06 --- /dev/null +++ b/StoneIsland/www/js/vendor/promise.js @@ -0,0 +1,34 @@ +var promise = function(fn, data){ + var my_cb, my_res, error_cb, my_error + data = data || {} + data.success = function(res){ + my_res = res + if (my_cb) { + my_cb(res) + } + } + data.error = function(res){ + my_error = res + if (error_cb) { + error_cb(res) + } + else { + console.log('error!') + console.log(res) + } + } + fn(data) + var p = { + then: function(cb){ + if (my_res) cb(my_res) + else my_cb = cb + return p + }, + error: function(cb){ + if (my_error) cb(my_error) + else error_cb = cb + return p + } + } + return p +} diff --git a/StoneIsland/www/js/vendor/util.js b/StoneIsland/www/js/vendor/util.js index fede4761..6523d50e 100644 --- a/StoneIsland/www/js/vendor/util.js +++ b/StoneIsland/www/js/vendor/util.js @@ -27,6 +27,7 @@ function parse_rgba_string (s) { return s.match(/(\d+)/g).slice(0,3) } function title_case (str) { return str.replace(/\w\S*/g, function(txt){return txt.charAt(0).toUpperCase() + txt.substr(1).toLowerCase();}); } +function pluralize (n,s,ss) { return n + " " + s + ( n == 1 ? "" : (ss || "s") ) } var E = Math.E var PI = Math.PI |
