mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-28 21:07:16 +00:00
Working version of the cart integration
This commit is contained in:
@@ -8,14 +8,16 @@ Darkswarm.factory 'Cart', (CurrentOrder, Variants)->
|
||||
line_item.variant.line_item = line_item
|
||||
Variants.register line_item.variant
|
||||
|
||||
register_variant: (variant)=>
|
||||
@create_line_item(variant) unless @line_items.some (li)->
|
||||
li.variant == variant
|
||||
line_items_present: =>
|
||||
@line_items.filter (li)->
|
||||
li.quantity > 0
|
||||
|
||||
register_variant: (variant)=>
|
||||
exists = @line_items.some (li)-> li.variant == variant
|
||||
@create_line_item(variant) unless exists
|
||||
|
||||
create_line_item: (variant)->
|
||||
li =
|
||||
variant.line_item =
|
||||
variant: variant
|
||||
quantity: 0
|
||||
variant.line_item = li
|
||||
@line_items.push li
|
||||
|
||||
@line_items.push variant.line_item
|
||||
|
||||
@@ -11,9 +11,9 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Car
|
||||
update: =>
|
||||
@loading = true
|
||||
@products = $resource("/shop/products").query (products)=>
|
||||
@extend()
|
||||
@dereference()
|
||||
@registerVariants()
|
||||
@extend() && @dereference()
|
||||
@registerVariants()
|
||||
@registerVariantsWithCart()
|
||||
@loading = false
|
||||
@
|
||||
|
||||
@@ -21,20 +21,26 @@ Darkswarm.factory 'Products', ($resource, Enterprises, Dereferencer, Taxons, Car
|
||||
for product in @products
|
||||
product.supplier = Enterprises.enterprises_by_id[product.supplier.id]
|
||||
Dereferencer.dereference product.taxons, Taxons.taxons_by_id
|
||||
|
||||
|
||||
# May return different objects! If the variant has already been registered
|
||||
# by another service, we fetch those
|
||||
registerVariants: ->
|
||||
for product in @products
|
||||
if product.variants
|
||||
variants = []
|
||||
product.variants = (Variants.register variant for variant in product.variants)
|
||||
product.master = Variants.register master if product.master
|
||||
product.master = Variants.register product.master if product.master
|
||||
|
||||
registerVariantsWithCart: ->
|
||||
for product in @products
|
||||
if product.variants
|
||||
for variant in product.variants
|
||||
Cart.register_variant variant
|
||||
Cart.register_variant product.master if product.master
|
||||
|
||||
extend: ->
|
||||
for product in @products
|
||||
if product.variants?.length > 0
|
||||
prices = (v.price for v in product.variants)
|
||||
product.price = Math.min.apply(null, prices)
|
||||
|
||||
product.hasVariants = product.variants?.length > 0
|
||||
|
||||
# Iterate through variants
|
||||
# Cart.register_variant(v)
|
||||
|
||||
@@ -2,5 +2,5 @@
|
||||
%span.nav-branded
|
||||
%i.ofn-i_027-shopping-cart
|
||||
%span
|
||||
{{ Cart.order.line_items.length }}
|
||||
{{ Cart.line_items_present().length }}
|
||||
items
|
||||
|
||||
@@ -13,6 +13,7 @@
|
||||
"ofn-disable-scroll" => true,
|
||||
max: "{{product.on_demand && 9999 || product.count_on_hand }}",
|
||||
name: "variants[{{product.master.id}}]",
|
||||
"ng-model" => "product.master.line_item.quantity",
|
||||
id: "variants_{{product.master.id}}"}
|
||||
%small x {{ product.master.unit_to_display }}
|
||||
|
||||
@@ -24,6 +25,7 @@
|
||||
"ofn-disable-scroll" => true,
|
||||
max: "{{product.on_demand && 9999 || product.count_on_hand }}",
|
||||
name: "variants[{{product.master.id}}]",
|
||||
"ng-model" => "product.master.line_item.quantity",
|
||||
id: "variants_{{product.master.id}}"}
|
||||
|
||||
.small-3.columns{"bo-if" => "product.group_buy"}
|
||||
@@ -32,6 +34,7 @@
|
||||
placeholder: "max",
|
||||
"ofn-disable-scroll" => true,
|
||||
max: "{{product.on_demand && 9999 || product.count_on_hand }}",
|
||||
"ng-model" => "product.master.line_item.max_quantity",
|
||||
name: "variant_attributes[{{product.master.id}}][max_quantity]"}
|
||||
%small x {{ product.master.unit_to_display }}
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@
|
||||
placeholder: "0",
|
||||
"ofn-disable-scroll" => true,
|
||||
max: "{{variant.on_demand && 9999 || variant.count_on_hand }}",
|
||||
"ng-model" => "variant.line_item.quantity",
|
||||
name: "variants[{{variant.id}}]", id: "variants_{{variant.id}}"}
|
||||
%small x {{ variant.unit_to_display }}
|
||||
|
||||
@@ -26,6 +27,7 @@
|
||||
min: 0,
|
||||
placeholder: "min",
|
||||
"ofn-disable-scroll" => true,
|
||||
"ng-model" => "variant.line_item.quantity",
|
||||
max: "{{variant.on_demand && 9999 || variant.count_on_hand }}",
|
||||
name: "variants[{{variant.id}}]", id: "variants_{{variant.id}}"}
|
||||
|
||||
@@ -34,6 +36,7 @@
|
||||
min: 0,
|
||||
placeholder: "max",
|
||||
"ofn-disable-scroll" => true,
|
||||
"ng-model" => "variant.line_item.max_quantity",
|
||||
max: "{{variant.on_demand && 9999 || variant.count_on_hand }}",
|
||||
name: "variant_attributes[{{variant.id}}][max_quantity]"}
|
||||
%small x {{ variant.unit_to_display }}
|
||||
|
||||
@@ -26,3 +26,7 @@ describe 'Cart service', ->
|
||||
expect(Cart.line_items[1].variant).toBe v2
|
||||
expect(Cart.line_items[1].variant.line_item).toBe Cart.line_items[1]
|
||||
|
||||
it "returns a list of items actually in the cart", ->
|
||||
expect(Cart.line_items_present()).toEqual []
|
||||
order.line_items[0].quantity = 1
|
||||
expect(Cart.line_items_present().length).toEqual 1
|
||||
|
||||
@@ -3,10 +3,11 @@ describe 'Products service', ->
|
||||
Products = null
|
||||
Enterprises = null
|
||||
Variants = null
|
||||
Cart = null
|
||||
CurrentHubMock = {}
|
||||
currentOrder =
|
||||
line_items: []
|
||||
currentOrder = null
|
||||
product = null
|
||||
|
||||
beforeEach ->
|
||||
product =
|
||||
test: "cats"
|
||||
@@ -14,15 +15,20 @@ describe 'Products service', ->
|
||||
id: 9
|
||||
price: 11
|
||||
variants: []
|
||||
currentOrder =
|
||||
line_items: []
|
||||
|
||||
module 'Darkswarm'
|
||||
module ($provide)->
|
||||
$provide.value "CurrentHub", CurrentHubMock
|
||||
$provide.value "currentOrder", currentOrder
|
||||
null
|
||||
|
||||
inject ($injector, _$httpBackend_)->
|
||||
Products = $injector.get("Products")
|
||||
Enterprises = $injector.get("Enterprises")
|
||||
Variants = $injector.get("Variants")
|
||||
Cart = $injector.get("Cart")
|
||||
$httpBackend = _$httpBackend_
|
||||
|
||||
it "Fetches products from the backend on init", ->
|
||||
@@ -37,12 +43,17 @@ describe 'Products service', ->
|
||||
$httpBackend.flush()
|
||||
expect(Products.products[0].supplier).toBe Enterprises.enterprises_by_id["9"]
|
||||
|
||||
it "registers variants with the Variants service", ->
|
||||
spyOn(Variants, "register")
|
||||
it "registers variants with Variants service", ->
|
||||
product.variants = [{id: 1}]
|
||||
$httpBackend.expectGET("/shop/products").respond([product])
|
||||
$httpBackend.flush()
|
||||
expect(Variants.register).toHaveBeenCalledWith product.variants[0]
|
||||
expect(Products.products[0].variants[0]).toBe Variants.variants[1]
|
||||
|
||||
it "registers variants with the Cart", ->
|
||||
product.variants = [{id: 8}]
|
||||
$httpBackend.expectGET("/shop/products").respond([product])
|
||||
$httpBackend.flush()
|
||||
expect(Cart.line_items[0].variant).toBe Products.products[0].variants[0]
|
||||
|
||||
describe "determining the price to display for a product", ->
|
||||
it "displays the product price when the product does not have variants", ->
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
describe 'Variants service', ->
|
||||
Variants = null
|
||||
variant =
|
||||
id: 1
|
||||
variant = null
|
||||
|
||||
beforeEach ->
|
||||
variant =
|
||||
id: 1
|
||||
module 'Darkswarm'
|
||||
inject ($injector)->
|
||||
Variants = $injector.get("Variants")
|
||||
@@ -14,3 +15,7 @@ describe 'Variants service', ->
|
||||
it "will return an existing variant rather than duplicating", ->
|
||||
Variants.register(variant)
|
||||
expect(Variants.register({id: 1})).toBe variant
|
||||
|
||||
it "will return the same object as passed", ->
|
||||
expect(Variants.register(variant)).toBe variant
|
||||
|
||||
|
||||
Reference in New Issue
Block a user