From da0810382b64681815e6d3011b7214bdc73151af Mon Sep 17 00:00:00 2001 From: stveep Date: Mon, 21 Dec 2015 22:19:00 +0000 Subject: [PATCH] Sorting by most orders, currency symbol, hide until clicked, reformatting table headers --- .../distributor_node_controller.js.coffee | 6 ++-- .../darkswarm/services/orders.js.coffee | 18 ++--------- app/helpers/injection_helper.rb | 13 +++----- .../api/orders_by_distributor_serializer.rb | 2 +- app/views/producers/_skinny.html.haml | 10 +++--- app/views/spree/users/_fat.html.haml | 31 +++++++++---------- app/views/spree/users/_skinny.html.haml | 16 ++++------ app/views/spree/users/show.html.haml | 11 +++---- 8 files changed, 41 insertions(+), 66 deletions(-) diff --git a/app/assets/javascripts/darkswarm/controllers/distributor_node_controller.js.coffee b/app/assets/javascripts/darkswarm/controllers/distributor_node_controller.js.coffee index 7c373326f2..2c81833e2e 100644 --- a/app/assets/javascripts/darkswarm/controllers/distributor_node_controller.js.coffee +++ b/app/assets/javascripts/darkswarm/controllers/distributor_node_controller.js.coffee @@ -1,9 +1,9 @@ Darkswarm.controller "DistributorNodeCtrl", ($scope, HashNavigation, $anchorScroll) -> $scope.toggle = -> - HashNavigation.toggle $scope.distributor_id - + HashNavigation.toggle $scope.distributor.id + $scope.open = -> - HashNavigation.active($scope.distributor_id) + HashNavigation.active($scope.distributor.id) if $scope.open() $anchorScroll() diff --git a/app/assets/javascripts/darkswarm/services/orders.js.coffee b/app/assets/javascripts/darkswarm/services/orders.js.coffee index 549e4f8a38..3cb65278aa 100644 --- a/app/assets/javascripts/darkswarm/services/orders.js.coffee +++ b/app/assets/javascripts/darkswarm/services/orders.js.coffee @@ -1,19 +1,7 @@ -Darkswarm.factory 'Orders', (orders, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo, $rootScope)-> +Darkswarm.factory 'Orders', (orders_by_distributor, currencyConfig, CurrentHub, Taxons, Dereferencer, visibleFilter, Matcher, Geo, $rootScope)-> new class Orders - orders_by_distributor: {} - distributor_names_by_id: {} constructor: -> # Populate Orders.orders from json in page. - @orders = orders - # Organise orders by distributor. - for order in orders - if order.distributor? - if @orders_by_distributor[order.distributor.id]? then @orders_by_distributor[order.distributor.id].push order else @orders_by_distributor[order.distributor.id] = [order] - if !@distributor_names_by_id[order.distributor.id] then @distributor_names_by_id[order.distributor.id] = {name: order.distributor.name, balance: 0} - for id in Object.keys(@distributor_names_by_id) - @distributor_names_by_id[id].balance = @orders_by_distributor[id].reduce(((x,y) -> - x + Number(y.outstanding_balance)), 0).toFixed(2) - console.log @distributor_names_by_id - - + @orders_by_distributor = orders_by_distributor + @currency_symbol = currencyConfig.symbol # Sorting by most orders (most recent/frequent?) diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index e5c311b7a3..28d896e6a0 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -51,16 +51,11 @@ module InjectionHelper render partial: "json/injection_ams", locals: {name: 'enterpriseAttributes', json: "#{@enterprise_attributes.to_json}"} end - def inject_orders_for_user - # Convert ActiveRecord::Relation to array for serialization - inject_json_ams "orders", spree_current_user.orders.where(state: :complete).to_a, Api::OrderSerializer - end - def inject_orders_by_distributor - #render partial: "json/injection_ams", locals: {name:"orders_by_distributor", json: spree_current_user.orders_by_distributor} - inject_json_ams "orders_by_distributor", - Enterprise.includes(:distributed_orders).where(id: spree_current_user.enterprises_ordered_from).to_a, - Api::OrdersByDistributorSerializer + # Convert ActiveRecord::Relation to array for serialization + data_array = Enterprise.includes(:distributed_orders).where(enterprises: {id: spree_current_user.enterprises_ordered_from }, spree_orders: {state: :complete, user_id: spree_current_user.id}).to_a + data_array.sort!{|a,b| b.distributed_orders.length <=> a.distributed_orders.length} # Better to do in SQL/Angular? + inject_json_ams "orders_by_distributor", data_array, Api::OrdersByDistributorSerializer end def inject_json(name, partial, opts = {}) diff --git a/app/serializers/api/orders_by_distributor_serializer.rb b/app/serializers/api/orders_by_distributor_serializer.rb index 35315a5cc9..a333ff6ff4 100644 --- a/app/serializers/api/orders_by_distributor_serializer.rb +++ b/app/serializers/api/orders_by_distributor_serializer.rb @@ -1,5 +1,5 @@ class Api::OrdersByDistributorSerializer < ActiveModel::Serializer - attributes :name, :balance, :distributed_orders + attributes :name, :id, :balance, :distributed_orders has_many :distributed_orders, serializer: Api::OrderSerializer def balance diff --git a/app/views/producers/_skinny.html.haml b/app/views/producers/_skinny.html.haml index 11860d35d6..cf066be05a 100644 --- a/app/views/producers/_skinny.html.haml +++ b/app/views/producers/_skinny.html.haml @@ -1,20 +1,20 @@ .row.active_table_row{"ng-click" => "toggle($event)", "ng-class" => "{'closed' : !open(), 'is_distributor' : producer.is_distributor}"} .columns.small-12.medium-4.large-4.skinny-head %span{"bo-if" => "producer.is_distributor" } - %a.is_distributor{"bo-href" => "producer.path" } + %a.is_distributor{"bo-href" => "producer.path" } %i{bo: {class: "producer.producer_icon_font"}} - %span.margin-top + %span.margin-top %strong{"bo-text" => "producer.name"} %span.producer-name{"bo-if" => "!producer.is_distributor" } %i{bo: {class: "producer.producer_icon_font"}} - %span.margin-top + %span.margin-top %strong{"bo-text" => "producer.name"} - + .columns.small-6.medium-3.large-3 %span.margin-top{"bo-text" => "producer.address.city"} .columns.small-4.medium-3.large-4 %span.margin-top{"bo-bind" => "producer.address.state_name | uppercase"} .columns.small-2.medium-2.large-1.text-right - %span.margin-top + %span.margin-top %i{"ng-class" => "{'ofn-i_005-caret-down' : !open(), 'ofn-i_006-caret-up' : open()}"} diff --git a/app/views/spree/users/_fat.html.haml b/app/views/spree/users/_fat.html.haml index d3e2030408..a447ceb859 100644 --- a/app/views/spree/users/_fat.html.haml +++ b/app/views/spree/users/_fat.html.haml @@ -1,20 +1,17 @@ -.row.active_table_row{"ng-click" => "toggle($event)", "ng-class" => "{'closed' : !open()}"} - .container{"bo-text" => "orders[0].distributor.name"} - %span {{orders[0].total_money.currency_symbol}} {{Orders.distributor_names_by_id[distributor_id].balance}} +.row.active_table_row{"ng-if" => "open()","ng-class" => "{'open' : !ofn-i_032-closed-sign()}"} .columns.small-12.medium-7.large-7.fat %table - %th - %tr - %td Transaction - %td Date - %td Payment Status - %td Shipping Status - %td Value - %td Balance - %tr{"ng-repeat" => "order in orders"} - %td {{order.id}} - %td {{order.completed_at}} - %td {{order.payment_state}} - %td {{order.shipment_state}} + %tr + %th Transaction + %th Date + %th Payment Status + %th Shipping Status + %th Value + %th Balance + %tr{"ng-repeat" => "order in distributor.distributed_orders"} + %td{"bo-text" => "order.id"} + %td{"bo-text" => "order.completed_at"} + %td{"bo-text" => "order.payment_state"} + %td{"bo-text" => "order.shipment_state"} %td {{order.total_money.currency_symbol}} {{order.total_money.amount}} - %td {{order.balance_money.currency_symbol}} {{order.balance_money.amount}} + %td{"ng-class" => "{'credit' : order.outstanding_balance < 0, 'debit' : order.outstanding_balance > 0, 'paid' : order.outstanding_balance == 0}"} {{order.balance_money.currency_symbol}} {{order.balance_money.amount}} diff --git a/app/views/spree/users/_skinny.html.haml b/app/views/spree/users/_skinny.html.haml index b608bfcf98..7f3e0e5135 100644 --- a/app/views/spree/users/_skinny.html.haml +++ b/app/views/spree/users/_skinny.html.haml @@ -1,14 +1,10 @@ --# Add extra highlighting etc (credit/debit?) here .row.active_table_row{"ng-click" => "toggle($event)", "ng-class" => "{'closed' : !open()}"} .columns.small-12.medium-4.large-4.skinny-head %span.margin-top - %strong{"bo-text" => "distributor"} + %strong{"bo-text" => "distributor.name"} - -# - .columns.small-6.medium-3.large-3 - %span.margin-top{"bo-text" => "producer.address.city"} - .columns.small-4.medium-3.large-4 - %span.margin-top{"bo-bind" => "producer.address.state_name | uppercase"} - .columns.small-2.medium-2.large-1.text-right - %span.margin-top - %i{"ng-class" => "{'ofn-i_005-caret-down' : !open(), 'ofn-i_006-caret-up' : open()}"} + .columns.small-6.medium-3.large-3 + %span.margin-top{"bo-text" => "'Balance: ' + Orders.currency_symbol + ' ' + distributor.balance", "ng-class" => "{'credit' : order.outstanding_balance < 0, 'debit' : order.outstanding_balance > 0, 'paid' : order.outstanding_balance == 0}"} + .columns.small-2.medium-2.large-1.text-right + %span.margin-top + %i{"ng-class" => "{'ofn-i_005-caret-down' : !open(), 'ofn-i_006-caret-up' : open()}"} diff --git a/app/views/spree/users/show.html.haml b/app/views/spree/users/show.html.haml index a9e82ebfce..d0a7ed495c 100644 --- a/app/views/spree/users/show.html.haml +++ b/app/views/spree/users/show.html.haml @@ -1,4 +1,3 @@ -= inject_orders_for_user = inject_orders_by_distributor .row.pad-top @@ -10,15 +9,15 @@ %dd = @user.email (#{link_to t(:edit), spree.edit_account_path}) - -# Add back ng-cloak below - .orders{"ng-controller" => "OrdersCtrl"} + %h3 My Orders + .orders{"ng-controller" => "OrdersCtrl", "ng-cloak" => true} .row{bindonce: true} .small-12.columns .active_table - %distributor.active_table_node.row.animate-repeat{"ng-repeat" => "(distributor_id, orders) in Orders.orders_by_distributor", + %distributor.active_table_node.row.animate-repeat{"ng-repeat" => "(key, distributor) in Orders.orders_by_distributor", "ng-controller" => "DistributorNodeCtrl", "ng-class" => "{'closed' : !open(), 'open' : open(), 'inactive' : !distributor.active}", - id: "{{distributor_id}}"} + id: "{{distributor.id}}"} .small-12.columns + = render partial: "spree/users/skinny" = render partial: "spree/users/fat" - -# TODO: Skinny partial