From 4d33e3dcbe981d73dcc7b562764ab5d5bf8ec360 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 3 Nov 2021 19:19:20 -0500 Subject: [PATCH] Move some custorder logic to handler; allow force-swap of product selection --- .../static/js/tailbone.buefy.autocomplete.js | 15 +++-- tailbone/templates/custorders/create.mako | 12 +++- tailbone/views/custorders/orders.py | 57 ++----------------- 3 files changed, 24 insertions(+), 60 deletions(-) diff --git a/tailbone/static/js/tailbone.buefy.autocomplete.js b/tailbone/static/js/tailbone.buefy.autocomplete.js index 7969f35a..53c41b40 100644 --- a/tailbone/static/js/tailbone.buefy.autocomplete.js +++ b/tailbone/static/js/tailbone.buefy.autocomplete.js @@ -32,10 +32,17 @@ const TailboneAutocomplete = { // allows for the "label" to display correctly as well initialLabel: String, - // TODO: i am not sure this is needed? but current logic does - // handle it specially, so am leaving for now. if this prop - // is set by the caller, then the `assignedLabel` will *always* - // be shown for the button (when "selection" has been made) + // while the `initialLabel` above is useful for setting the + // *initial* label (of course), it cannot be used to + // arbitrarily update the label during the component's life. + // if you do need to *update* the label after initial page + // load, then you should set `assignedLabel` instead. one + // place this happens is in /custorders/create page, where + // product autocomplete shows some results, and user clicks + // one, but then handler logic can forcibly "swap" the + // selection, causing *different* product data to come back + // from the server, and autocomplete label should be updated + // to match. this feels a bit awkward still but does work.. assignedLabel: String, // simple placeholder text for the input box diff --git a/tailbone/templates/custorders/create.mako b/tailbone/templates/custorders/create.mako index 61c147f5..c6bcbd64 100644 --- a/tailbone/templates/custorders/create.mako +++ b/tailbone/templates/custorders/create.mako @@ -511,7 +511,7 @@ @@ -1368,12 +1368,18 @@ productChanged(uuid) { if (uuid) { - this.productUUID = uuid let params = { action: 'get_product_info', - uuid: this.productUUID, + uuid: uuid, } + // nb. it is possible for the handler to "swap" + // the product selection, i.e. user chooses a "per + // LB" item but the handler only allows selling by + // the "case" item. so we do not assume the uuid + // received above is the correct one, but just use + // whatever came back from handler this.submitBatchData(params, response => { + this.productUUID = response.data.uuid this.productUPC = response.data.upc_pretty this.productKey = response.data.key this.productDisplay = response.data.full_description diff --git a/tailbone/views/custorders/orders.py b/tailbone/views/custorders/orders.py index 1d564f35..ce74bac2 100644 --- a/tailbone/views/custorders/orders.py +++ b/tailbone/views/custorders/orders.py @@ -551,61 +551,12 @@ class CustomerOrderView(MasterView): return self.info_for_product(batch, data, product) def uom_choices_for_product(self, product): - choices = [] - - # Each - if not product or not product.weighed: - unit_name = self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_EACH] - choices.append({'key': self.enum.UNIT_OF_MEASURE_EACH, - 'value': unit_name}) - - # Pound - if not product or product.weighed: - unit_name = self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_POUND] - choices.append({ - 'key': self.enum.UNIT_OF_MEASURE_POUND, - 'value': unit_name, - }) - - # Case - case_text = None - case_size = self.handler.get_case_size_for_product(product) - if case_size is None: - case_text = "{} (× ?? {})".format( - self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_CASE], - unit_name) - elif case_size > 1: - case_text = "{} (× {} {})".format( - self.enum.UNIT_OF_MEASURE[self.enum.UNIT_OF_MEASURE_CASE], - pretty_quantity(case_size), - unit_name) - if case_text: - choices.append({'key': self.enum.UNIT_OF_MEASURE_CASE, - 'value': case_text}) - - return choices + return self.handler.uom_choices_for_product(product) def info_for_product(self, batch, data, product): - app = self.get_rattail_app() - products = app.get_products_handler() - data = { - 'uuid': product.uuid, - 'upc': six.text_type(product.upc), - 'upc_pretty': product.upc.pretty(), - 'unit_price_display': self.get_unit_price_display(product), - 'full_description': product.full_description, - 'url': self.request.route_url('products.view', uuid=product.uuid), - 'image_url': products.get_image_url(product), - 'uom_choices': self.uom_choices_for_product(product), - } - - key = self.rattail_config.product_key() - if key == 'upc': - data['key'] = data['upc_pretty'] - else: - data['key'] = getattr(product, key, data['upc_pretty']) - - return data + info = self.handler.get_product_info(batch, product) + info['url'] = self.request.route_url('products.view', uuid=info['uuid']) + return info def normalize_batch(self, batch): return {