Files
openfoodnetwork/app/assets/javascripts/darkswarm/services/variants.js.coffee
Maikel Linke 4ab48c4b85 Initialise valid quantities when choosing OCs
Line items were initialised with undefined quantities which makes it
impossible to distinguish between the initial unset quantity and a user
entering an invalid quantity. When a user changed order cycles, all
quantities are reset and the UI displayed the quantity modifier buttons
instead of the Add button.

Initialising with the valid quantity 0 helps us to display the Add
button in that situation.
2021-01-22 16:42:21 +11:00

28 lines
799 B
CoffeeScript

Darkswarm.factory 'Variants', ->
new class Variants
variants: {}
clear: ->
@variants = {}
register: (variant)->
@variants[variant.id] ||= @extend variant
extend: (variant)->
variant.extended_name = @extendedVariantName(variant)
variant.line_item ||= @lineItemFor(variant) # line_item may have been initialised in Cart#constructor
variant.line_item.total_price = variant.price_with_fees * variant.line_item.quantity
variant
extendedVariantName: (variant) =>
if variant.product_name == variant.name_to_display
name = variant.product_name
else
name = "#{variant.product_name} - #{variant.name_to_display}"
name
lineItemFor: (variant) ->
variant: variant
quantity: 0
max_quantity: 0